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:
Aria Minaei 2022-04-10 19:57:13 +02:00
parent 65f9f1c850
commit 86cdbdb48c
2 changed files with 13 additions and 11 deletions

View file

@ -511,30 +511,32 @@ function _ensureString(s: unknown): string | undefined {
* @returns A stringLiteral prop type * @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) * 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. * The options. Use the `"value": "Label"` format.
* *
* An object like `{[value]: Label}`. Example: `{r: "Red", "g": "Green"}` * 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 Determines if editor is shown as a menu or a switch. Either 'menu' or 'switch'. Default: 'menu'
*/ */
opts: { opts: {
as?: 'menu' | 'switch' as?: 'menu' | 'switch'
label?: string 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 { return {
type: 'stringLiteral', type: 'stringLiteral',
default: defaultValue, default: defaultValue,
options: {...options}, valuesAndLabels: {...valuesAndLabels},
[propTypeSymbol]: 'TheatrePropType', [propTypeSymbol]: 'TheatrePropType',
valueType: null as $IntentionalAny, valueType: null as $IntentionalAny,
as: opts.as ?? 'menu', as: opts.as ?? 'menu',
@ -542,9 +544,9 @@ export function stringLiteral<Opts extends {[key in string]: string}>(
interpolate: opts.interpolate ?? leftInterpolate, interpolate: opts.interpolate ?? leftInterpolate,
deserializeAndSanitize( deserializeAndSanitize(
json: unknown, json: unknown,
): undefined | Extract<keyof Opts, string> { ): undefined | Extract<keyof ValuesAndLabels, string> {
if (typeof json !== 'string') return undefined if (typeof json !== 'string') return undefined
if (Object.prototype.hasOwnProperty.call(options, json)) { if (Object.prototype.hasOwnProperty.call(valuesAndLabels, json)) {
return json as $IntentionalAny return json as $IntentionalAny
} else { } else {
return undefined return undefined
@ -665,7 +667,7 @@ export interface PropTypeConfig_String
export interface PropTypeConfig_StringLiteral<T extends string> export interface PropTypeConfig_StringLiteral<T extends string>
extends ISimplePropType<'stringLiteral', T> { extends ISimplePropType<'stringLiteral', T> {
options: Record<T, string> valuesAndLabels: Record<T, string>
as: 'menu' | 'switch' as: 'menu' | 'switch'
} }

View file

@ -31,13 +31,13 @@ const StringLiteralPropEditor: React.FC<{
<BasicSelect <BasicSelect
value={stuff.value} value={stuff.value}
onChange={onChange} onChange={onChange}
options={propConfig.options} options={propConfig.valuesAndLabels}
/> />
) : ( ) : (
<BasicSwitch <BasicSwitch
value={stuff.value} value={stuff.value}
onChange={onChange} onChange={onChange}
options={propConfig.options} options={propConfig.valuesAndLabels}
/> />
)} )}
</SingleRowPropEditor> </SingleRowPropEditor>