Skip to content

Instantly share code, notes, and snippets.

@blvdmitry
Created October 31, 2018 12:42
Show Gist options
  • Save blvdmitry/5a3e687d0318550ecfaf87de824763fe to your computer and use it in GitHub Desktop.
Save blvdmitry/5a3e687d0318550ecfaf87de824763fe to your computer and use it in GitHub Desktop.
const properties: Array<ControlApi> = [{
type: 'text',
propertyName: 'textProperty',
defaultValue: 'some text',
}, {
type: 'number',
propertyName: 'numberProperty',
defaultValue: 2,
}, {
type: 'boolean',
propertyName: 'booleanProperty',
defaultValue: true,
}, {
type: 'enum',
propertyName: 'enumProperty',
options: [
{ text: 'Option 1', value: '1' },
{ text: 'Option 2', value: '2' },
],
defaultValue: '2',
}, {
type: 'object',
propertyName: 'objectProperty',
controls: [{
type: 'text',
propertyName: 'textProperty',
}]
}, {
type: 'array',
propertyName: 'arrayProperty',
item: {
type: 'text',
propertyName: 'label',
}
}, {
type: 'array',
propertyName: 'arrayProperty2',
item: {
type: 'object',
propertyName: 'label2',
controls: [{
type: 'text',
propertyName: 'textProperty',
}]
}
}];
interface BaseControlApi {
label?: string;
propertyName: string;
required?: boolean;
}
interface GenericControlApi<TType, TValue> extends BaseControlApi {
type: TType;
defaultValue?: TValue;
}
type GenericControlProps<TControlApi extends GenericControlApi<ControlType, ControlValue>> = TControlApi & {
onChange: (params: GenericChangeParams<TControlApi['defaultValue']>) => void;
};
interface GenericChangeParams<T> {
name: string;
value: T;
}
// General
type ControlType = TextType | NumberType | BooleanType | EnumType | ObjectType | ArrayType;
type ControlValue = TextValue | NumberValue | BooleanValue | EnumValue | ObjectValue | ArrayValue;
type ControlApi =
TextControlApi | NumberControlApi | BooleanControlApi | EnumControlApi | ObjectControlApi | ArrayControlApi;
type Props = TextControlProps | NumberControlProps | BooleanControlProps | EnumControlProps | ObjectControlProps;
// Text
type TextType = 'text';
type TextValue = string;
interface TextControlApi extends GenericControlApi<TextType, TextValue> {}
interface TextControlProps extends GenericControlProps<TextControlApi> {}
// Number
type NumberType = 'number';
type NumberValue = number;
interface NumberControlApi extends GenericControlApi<NumberType, NumberValue> {}
interface NumberControlProps extends GenericControlProps<NumberControlApi> {}
// Boolean
type BooleanType = 'boolean';
type BooleanValue = boolean;
interface BooleanControlApi extends GenericControlApi<BooleanType, BooleanValue> {}
interface BooleanControlProps extends GenericControlProps<BooleanControlApi> {}
// Enum
type EnumType = 'enum';
type EnumValue = string;
interface EnumControlApi extends GenericControlApi<EnumType, EnumValue> {
options: Array<{ text: string, value: EnumValue }>;
}
interface EnumControlProps extends GenericControlProps<EnumControlApi> {}
// Object
type ObjectType = 'object';
type ObjectValue = { [name: string]: ControlValue };
interface ObjectControlApi extends GenericControlApi<ObjectType, ObjectValue> {
controls: Array<ControlApi>;
}
interface ObjectControlProps extends GenericControlProps<ObjectControlApi> {}
// Array
type ArrayType = 'array';
type ArrayValue = Array<TextValue | NumberValue | BooleanValue | EnumValue | ObjectValue>;
interface ArrayControlApi extends GenericControlApi<ArrayType, ArrayValue> {
item: ControlApi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment