Rename stringLiteral.opts
to valuesAndLabels
so it is not confused with `opts` from all other prop configs. https://github.com/theatre-js/theatre/pull/118#discussion_r846632293
This commit is contained in:
parent
65f9f1c850
commit
86cdbdb48c
2 changed files with 13 additions and 11 deletions
|
@ -511,30 +511,32 @@ function _ensureString(s: unknown): string | undefined {
|
|||
* @returns A stringLiteral prop type
|
||||
*
|
||||
*/
|
||||
export function stringLiteral<Opts extends {[key in string]: string}>(
|
||||
export function stringLiteral<
|
||||
ValuesAndLabels extends {[key in string]: string},
|
||||
>(
|
||||
/**
|
||||
* Default value (a string that equals one of the options)
|
||||
*/
|
||||
defaultValue: Extract<keyof Opts, string>,
|
||||
defaultValue: Extract<keyof ValuesAndLabels, string>,
|
||||
/**
|
||||
* The options. Use the `"value": "Label"` format.
|
||||
*
|
||||
* An object like `{[value]: Label}`. Example: `{r: "Red", "g": "Green"}`
|
||||
*/
|
||||
options: Opts,
|
||||
valuesAndLabels: ValuesAndLabels,
|
||||
/**
|
||||
* opts.as Determines if editor is shown as a menu or a switch. Either 'menu' or 'switch'. Default: 'menu'
|
||||
*/
|
||||
opts: {
|
||||
as?: 'menu' | 'switch'
|
||||
label?: string
|
||||
interpolate?: Interpolator<Extract<keyof Opts, string>>
|
||||
interpolate?: Interpolator<Extract<keyof ValuesAndLabels, string>>
|
||||
} = {},
|
||||
): PropTypeConfig_StringLiteral<Extract<keyof Opts, string>> {
|
||||
): PropTypeConfig_StringLiteral<Extract<keyof ValuesAndLabels, string>> {
|
||||
return {
|
||||
type: 'stringLiteral',
|
||||
default: defaultValue,
|
||||
options: {...options},
|
||||
valuesAndLabels: {...valuesAndLabels},
|
||||
[propTypeSymbol]: 'TheatrePropType',
|
||||
valueType: null as $IntentionalAny,
|
||||
as: opts.as ?? 'menu',
|
||||
|
@ -542,9 +544,9 @@ export function stringLiteral<Opts extends {[key in string]: string}>(
|
|||
interpolate: opts.interpolate ?? leftInterpolate,
|
||||
deserializeAndSanitize(
|
||||
json: unknown,
|
||||
): undefined | Extract<keyof Opts, string> {
|
||||
): undefined | Extract<keyof ValuesAndLabels, string> {
|
||||
if (typeof json !== 'string') return undefined
|
||||
if (Object.prototype.hasOwnProperty.call(options, json)) {
|
||||
if (Object.prototype.hasOwnProperty.call(valuesAndLabels, json)) {
|
||||
return json as $IntentionalAny
|
||||
} else {
|
||||
return undefined
|
||||
|
@ -665,7 +667,7 @@ export interface PropTypeConfig_String
|
|||
|
||||
export interface PropTypeConfig_StringLiteral<T extends string>
|
||||
extends ISimplePropType<'stringLiteral', T> {
|
||||
options: Record<T, string>
|
||||
valuesAndLabels: Record<T, string>
|
||||
as: 'menu' | 'switch'
|
||||
}
|
||||
|
||||
|
|
|
@ -31,13 +31,13 @@ const StringLiteralPropEditor: React.FC<{
|
|||
<BasicSelect
|
||||
value={stuff.value}
|
||||
onChange={onChange}
|
||||
options={propConfig.options}
|
||||
options={propConfig.valuesAndLabels}
|
||||
/>
|
||||
) : (
|
||||
<BasicSwitch
|
||||
value={stuff.value}
|
||||
onChange={onChange}
|
||||
options={propConfig.options}
|
||||
options={propConfig.valuesAndLabels}
|
||||
/>
|
||||
)}
|
||||
</SingleRowPropEditor>
|
||||
|
|
Loading…
Reference in a new issue