-
Notifications
You must be signed in to change notification settings - Fork 29
/
MuiMainDummy.tsx
78 lines (69 loc) · 2.81 KB
/
MuiMainDummy.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
import React from 'react'
import Paper from '@mui/material/Paper'
import Button from '@mui/material/Button'
import { MuiSchemaDebug } from './MuiSchemaDebug'
import { UISchemaMap } from '@ui-schema/json-schema/Definitions'
import { createEmptyStore, UIStoreProvider } from '@ui-schema/react/UIStore'
import Grid from '@mui/material/Grid'
import { List } from 'immutable'
import { StoreKeys } from '@ui-schema/system/ValueStore'
import { browserT } from '../../t'
import { isInvalid } from '@ui-schema/react/ValidityReporter'
import { storeUpdater } from '@ui-schema/react/storeUpdater'
import { memo } from '@ui-schema/react/Utils/memo'
import { WidgetEngine } from '@ui-schema/react/WidgetEngine'
interface MainDummyProps {
schema: UISchemaMap
}
const WidgetEngineMemo = memo(WidgetEngine)
const MainDummy: React.FC<MainDummyProps> = ({schema}) => {
const [showValidity, setShowValidity] = React.useState(false)
const [store, setStore] = React.useState(() => createEmptyStore(schema.get('type')))
const onChange = React.useCallback((actions) => setStore(storeUpdater(actions)), [setStore])
return <UIStoreProvider
store={store}
onChange={onChange}
showValidity={showValidity}
//doNotDefault
>
<Paper
sx={{
p: 2,
display: 'flex',
overflow: 'auto',
flexDirection: 'column',
}}
>
{/*<GridStack isRoot schema={schema}/>*/}
<Grid container spacing={2}>
<WidgetEngineMemo
storeKeys={List([]) as StoreKeys}
schemaKeys={List([]) as StoreKeys}
schema={schema}
parentSchema={undefined}
required={false}
t={browserT}
isVirtual={false}
noGrid={false}
/>
</Grid>
<Button onClick={() => setShowValidity(!showValidity)}>{showValidity ? 'hide ' : ''}validity</Button>
{isInvalid(store.getValidity()) ? 'invalid' : 'valid'}
</Paper>
<MuiSchemaDebug setSchema={undefined} schema={schema}/>
</UIStoreProvider>
}
export const DummyRenderer: React.FC<{
id: string
schema: any
toggleDummy?: (id: string) => void
getDummy?: (id: string) => boolean
open?: boolean
}> = ({id, schema, toggleDummy, getDummy, open = false}) => <React.Fragment>
{open || !toggleDummy ? null :
<Button style={{marginBottom: 12}} onClick={() => toggleDummy(id)} variant={(getDummy && getDummy(id) ? 'contained' : 'outlined')}>
{id.replace(/([A-Z0-9])/g, ' $1').replace(/^./, str => str.toUpperCase())}
</Button>}
{(getDummy && getDummy(id)) || open || !toggleDummy ?
<MainDummy schema={schema}/> : null}
</React.Fragment>