Made signature of types.stringLiteral() a bit more ergonomic

This commit is contained in:
Aria Minaei 2021-07-06 16:50:43 +02:00
parent deb1ad8793
commit 97ce94d52a
3 changed files with 24 additions and 23 deletions

View file

@ -293,20 +293,21 @@ const editorSheetObjectConfig = types.compound({
showAxes: types.boolean(true), showAxes: types.boolean(true),
showGrid: types.boolean(true), showGrid: types.boolean(true),
showOverlayIcons: types.boolean(false), showOverlayIcons: types.boolean(false),
transformControlsMode: types.stringLiteral<TransformControlsMode>( transformControlsMode: types.stringLiteral('translate', {
'translate', translate: 'Translate',
['translate', 'rotate', 'scale'], rotate: 'Rotate',
), scale: 'Scale',
transformControlsSpace: types.stringLiteral<TransformControlsSpace>('world', [ }),
'local', transformControlsSpace: types.stringLiteral('world', {
'world', local: 'Local',
]), world: 'World',
viewportShading: types.stringLiteral<ViewportShading>('rendered', [ }),
'flat', viewportShading: types.stringLiteral('rendered', {
'rendered', flat: 'Flat',
'solid', rendered: 'Rendered',
'wireframe', solid: 'Solid',
]), wireframe: 'Wireframe',
}),
}) })
export const bindToCanvas: BindFunction = ({ export const bindToCanvas: BindFunction = ({

View file

@ -61,17 +61,17 @@ export interface PropTypeConfig_StringLiteral<T extends string>
extends IBasePropType<T> { extends IBasePropType<T> {
type: 'stringLiteral' type: 'stringLiteral'
default: T default: T
options: ReadonlyArray<T> options: Record<T, string>
} }
export function stringLiteral<T extends string>( export function stringLiteral<Opts extends {[key in string]: string}>(
defaultValue: T, defaultValue: Extract<keyof Opts, string>,
options: ReadonlyArray<T>, options: Opts,
): PropTypeConfig_StringLiteral<T> { ): PropTypeConfig_StringLiteral<Extract<keyof Opts, string>> {
return { return {
type: 'stringLiteral', type: 'stringLiteral',
default: defaultValue, default: defaultValue,
options: [...options], options: {...options},
[s]: 'TheatrePropType', [s]: 'TheatrePropType',
valueType: null as $IntentionalAny, valueType: null as $IntentionalAny,
} }

View file

@ -77,9 +77,9 @@ const StringLiteralPropEditor: React.FC<{
{stuff.controlIndicators} {stuff.controlIndicators}
<Body> <Body>
<select value={stuff.value} onChange={onChange}> <select value={stuff.value} onChange={onChange}>
{propConfig.options.map((opt, i) => ( {Object.keys(propConfig.options).map((key, i) => (
<option key={'option-' + i} value={opt}> <option key={'option-' + i} value={key}>
{opt} {propConfig.options[key]}
</option> </option>
))} ))}
</select> </select>