-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageCustomForm.tsx
134 lines (123 loc) · 4.44 KB
/
PageCustomForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React from 'react'
import Container from '@mui/material/Container'
import Box from '@mui/material/Box'
import Grid from '@mui/material/Grid'
import Typography from '@mui/material/Typography'
import { Nav } from '../components/Layout'
import {
createOrderedMap, createStore, JsonSchema,
onChangeHandler, StoreSchemaType, storeUpdater,
UIStoreProvider, StoreKeys, ObjectGroup,
PluginStack, applyPluginStack,
} from '@ui-schema/ui-schema'
import { List, OrderedMap } from 'immutable'
import { StringRenderer } from '@ui-schema/ds-material/Widgets/TextField'
import { WidgetCountrySelect } from '../components/Widgets/WidgetCountrySelect'
import { DataDebug } from '../components/DataDebug'
const schema = createOrderedMap({
type: 'object',
properties: {
name: {
type: 'string',
},
favorites: {
type: 'array',
widget: 'SelectChips',
items: {
oneOf: [
{
const: 'Chips',
}, {
const: 'Crisps',
}, {
const: 'Tacos',
}, {
const: 'Nachos',
}, {
const: 'Cheese',
},
],
},
},
country: {
type: 'string',
widget: 'CountrySelect',
},
},
} as JsonSchema)
const WidgetTextField = applyPluginStack(StringRenderer)
const CountrySelect = applyPluginStack(WidgetCountrySelect)
const CustomFormContent: React.FC<{
storeKeys?: StoreKeys
schema: StoreSchemaType
showValidity?: boolean
}> = ({storeKeys = List(), schema, showValidity}) => {
const [objectSchema, setObjectSchema] = React.useState<StoreSchemaType>(() => schema)
return <ObjectGroup
storeKeys={storeKeys}
schema={schema} parentSchema={undefined}
onSchema={setObjectSchema}
>
<Grid container dir={'columns'} spacing={4}>
<WidgetTextField
level={1}
storeKeys={storeKeys.push('name') as StoreKeys}
schema={objectSchema.getIn(['properties', 'name']) as unknown as StoreSchemaType}
parentSchema={objectSchema}
// using `applyPluginStack`, this free-form widget is fully typed
// with the actual props of the widget component
multiline={false}
/>
<PluginStack<{ readOnly: boolean }>
showValidity={showValidity}
storeKeys={storeKeys.push('favorites') as StoreKeys}
schema={objectSchema.getIn(['properties', 'favorites']) as unknown as StoreSchemaType}
parentSchema={objectSchema}
level={1}
readOnly={false}
// noGrid={false} (as grid-item is included in `PluginStack`)
/>
<CountrySelect
level={1}
storeKeys={storeKeys.push('country') as StoreKeys}
schema={objectSchema.getIn(['properties', 'country']) as unknown as StoreSchemaType}
parentSchema={objectSchema}
/>
</Grid>
</ObjectGroup>
}
const DemoComponent = () => {
const showValidity = true
const [store, setStore] = React.useState(() => createStore(OrderedMap({})))
const onChange: onChangeHandler = React.useCallback(
(actions) => setStore(storeUpdater(actions)),
[setStore],
)
return <React.Fragment>
<UIStoreProvider
store={store}
onChange={onChange}
showValidity={showValidity}
>
<CustomFormContent
schema={schema}
showValidity={showValidity}
/>
<DataDebug/>
</UIStoreProvider>
</React.Fragment>
}
export const PageCustomForm: React.ComponentType = () => {
return <>
<Container maxWidth={'md'} fixed style={{display: 'flex'}}>
<Nav/>
<Box mx={2} py={1} style={{flexGrow: 1}}>
<Box mb={2}>
<Typography variant={'h1'} gutterBottom>UI-Schema Custom Form</Typography>
<Typography variant={'body2'} gutterBottom>The form on this page is the same as `SimpleForm`, but rendered at custom positions per-code.</Typography>
</Box>
<DemoComponent/>
</Box>
</Container>
</>
}