import { FC, useCallback, useMemo, useState } from 'react'; import { Story } from '@storybook/react'; import SchemaForm, { FormProps } from './SchemaForm'; import { useField } from 'react-final-form'; import { FieldProps, Schema } from './types'; import TextArea from '../TextArea'; import { action } from '@storybook/addon-actions'; import '../../../styles/schema-form.css'; import { FieldComponent } from './context'; export default { title: 'Components/SchemaForm', component: SchemaForm, }; const Template: Story = (props) => { document.body.classList.remove('sb-main-padded'); const [value, setValue] = useState<{ values: any }>(props.initialValues); return (
        {value && JSON.stringify(value, null, '  ')}
      
); }; export const Default = Template.bind({}); Default.args = { schema: { type: 'object', title: 'Title', description: 'This is a description', properties: { string: { type: 'string', title: 'some string', description: 'type some caracters', }, number: { type: 'number', }, boolean: { type: 'boolean', description: 'A switch', }, object: { type: 'object', properties: { foo: { type: 'string', }, bar: { type: 'number', }, }, }, array: { type: 'array', items: { type: 'string', add: 'Add an item', remove: 'Remove this', }, }, objectsArray: { type: 'array', items: { type: 'object', properties: { fooArray: { type: 'string', }, barArray: { type: 'number', }, }, additionalProperties: true, }, }, enum: { type: 'string', title: 'with enum', description: 'list of choices', enum: [1, 2, 3], enumNames: ['One', 'Two', 'Three'], }, free: { title: 'Set whatever you want', }, }, additionalProperties: { type: 'string', add: 'Add an object with a string', remove: 'Sorry, remove that', }, }, }; export const TextOnly = Template.bind({}); TextOnly.args = { schema: { type: 'string', title: 'Juste a single string value', }, }; export const WithHidden = Template.bind({}); WithHidden.args = { initialValues: { bar: 42, }, schema: { type: 'object', properties: { foo: { type: 'string', hidden: true, }, bar: { type: 'string', }, }, }, }; export const WithOneOf = Template.bind({}); WithOneOf.args = { initialValues: { foo: true, }, schema: { type: 'object', oneOf: [ { properties: { foo: { type: 'string', }, }, }, { properties: { foo: { type: 'boolean', }, }, }, ], }, }; export const WithOneOfWithValues = Template.bind({}); WithOneOfWithValues.args = { initialValues: { type: 'boolean', foo: true, }, schema: { type: 'object', title: 'Type', description: 'Choose a type, it will set the type value and display a specific form', properties: { value: { type: 'string', }, }, oneOf: [ { properties: { foo: { type: 'string', }, }, }, { properties: { foo: { type: 'boolean', }, }, }, ], 'ui:options': { oneOf: { options: [ { label: 'String', index: 0, value: { type: 'string', }, }, { label: 'Boolean', index: 1, value: { type: 'boolean', }, }, ], }, }, }, }; export const WithOneOfWithArray = Template.bind({}); WithOneOfWithArray.args = { schema: { type: 'array', items: { type: 'object', oneOf: [ { properties: { foo: { type: 'string', }, }, }, { properties: { bar: { type: 'string', }, }, }, ], }, }, }; export const WithOneOfWithMergedProperties = Template.bind({}); WithOneOfWithMergedProperties.args = { schema: { type: 'object', properties: { label: { type: 'string', }, }, oneOf: [ { properties: { url: { type: 'string', }, }, }, { properties: { event: { type: 'string', }, }, }, ], }, }; export const WithNestedOneOf = Template.bind({}); WithNestedOneOf.args = { schema: { type: 'object', properties: { foo: { type: 'string', }, }, oneOf: [ { properties: { bar: { type: 'string', }, }, }, { properties: { bar: { type: 'boolean', }, }, oneOf: [ { properties: { babar: { type: 'string', }, }, }, { properties: { fofoo: { type: 'string', }, }, }, ], }, ], }, }; export const WithSortedOneOf = Template.bind({}); WithSortedOneOf.args = { schema: { type: 'object', properties: { foo: { type: 'string', }, bar: { oneOf: [ { title: 'As string', value: 'string', properties: { value: { type: 'string', title: 'String value', }, }, }, { title: 'As number', value: 'number', properties: { value: { type: 'number', title: 'Number value', }, currency: { type: 'string', title: 'Currency', enum: ['€', '$', '¥'], }, }, }, ], }, }, } as Schema, initialValues: { bar: 'number', value: 42, currency: '$', }, }; export const GridLayout = Template.bind({}); GridLayout.args = { schema: { type: 'object', title: 'With Grid layout', properties: { foo: { type: 'string', }, bar: { type: 'string', }, lorem: { type: 'string', 'ui:widget': 'textarea', }, ipsum: { type: 'string', }, }, 'ui:options': { grid: [[['foo', 'lorem'], ['ipsum']]], }, }, }; const CustomUIWidget = ({ schema, name }: FieldProps) => { const field = useField(name); return (
{schema.title}
{schema.description}
); }; export const UIWidgets = Template.bind({}); UIWidgets.args = { schema: { type: 'object', title: 'All available ui:widgets', properties: { upload: { type: 'string', title: 'upload', description: 'Get a file and store it as data url. Only available for string types.', 'ui:widget': 'upload', }, textarea: { type: 'string', title: 'textarea', description: 'Display a textarea. Only available for string types.', 'ui:widget': 'textarea', 'ui:options': { textarea: { rows: 3, }, }, }, select: { type: 'string', title: 'select', description: 'Display a Select.', 'ui:widget': 'select', 'ui:options': { select: { options: [ { label: 'Foo', value: 'foo', }, { label: 'Bar', value: 'bar', }, ], }, }, }, date: { type: 'string', title: 'date', description: 'Display a date picker. Only available for string types.', 'ui:widget': 'date', }, color: { type: 'string', title: 'color', description: 'Display a color picker. Only available for string types.', 'ui:widget': 'color', }, radio: { type: 'string', title: 'radio', description: 'Display radio buttons instead of select', 'ui:widget': 'radio', enum: [1, 2, 4], enumNames: ['One', 'Two', 'Three'], }, slider: { type: 'number', title: 'Slider', description: 'Display a slider with static values', 'ui:widget': 'slider', 'ui:options': { slider: { steps: [ { label: '7 ans', description: '
Mensualités n€/mois
bla bla
', value: 7, }, { label: '10 ans', description: '
Mensualités n€/mois
bla bla
', value: 10, }, { label: '12 ans', description: '
Mensualités n€/mois
bla bla
', value: 12, }, { label: '15 ans', description: '
Mensualités n€/mois
bla bla
', value: 15, }, { label: '20 ans', description: '
Mensualités n€/mois
bla bla
', value: 20, }, { label: '25 ans', description: '
Mensualités n€/mois
bla bla
', value: 25, }, ], }, }, }, slider2: { type: 'number', title: 'Slider', description: 'Display a slider with static values', 'ui:widget': 'slider', 'ui:options': { slider: { steps: [ { label: '7 ans', description: '
Mensualités n€/mois
bla bla
', value: 0, }, { label: '10 ans', description: '
Mensualités n€/mois
bla bla
', value: 1, }, { label: '12 ans', description: '
Mensualités n€/mois
bla bla
', value: 2, }, { label: '15 ans', description: '
Mensualités n€/mois
bla bla
', value: 3, }, { label: '20 ans', description: '
Mensualités n€/mois
bla bla
', value: 4, }, { label: '25 ans', description: '
Mensualités n€/mois
bla bla
', value: 5, }, ], }, }, }, custom: { type: 'string', title: 'React Component', description: 'You can display a custom react component by passing it to ui:widget. The type is not important but need to be set.', 'ui:widget': CustomUIWidget, 'ui:options': { textarea: { autoSize: true, }, }, }, }, }, }; export const Localized = Template.bind({}); Localized.args = { schema: { type: 'object', title: 'With Localized Strings', properties: { localizedString: { type: 'localized:string', title: 'Localized string', description: 'Localized string description', }, localizedTextarea: { type: 'localized:string', title: 'Localized string', description: 'Localized string description', 'ui:widget': 'textarea', }, localizedNumber: { type: 'localized:number', title: 'Localized number', description: 'Localized number description', }, localizedBoolean: { type: 'localized:boolean', title: 'Localized boolean', description: 'Localized boolean description', }, }, }, }; export const WithCustomFieldContainer = Template.bind({}); const FieldContainer: FC = ({ name, children }) => { const field = useField(name); const [value, setValue] = useState(''); const [displayRaw, setDisplayRaw] = useState(false); const toggle = useCallback(() => { setDisplayRaw(!displayRaw); if (!displayRaw) { setValue( typeof field.input.value === 'string' ? field.input.value : JSON.stringify(field.input.value, null, ' ') ); } }, [displayRaw, field.input.value]); const onChange = useCallback( (value: string) => { setValue(value); try { field.input.onChange(JSON.parse(value)); } catch { field.input.onChange(value); } }, [field.input] ); return (
{displayRaw && (