-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 1.67 KB
/
index.js
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
import PropTypes from 'prop-types'
import React, { useRef } from 'react'
import { DynamicFormSize } from '../Schema'
import util from '../util'
import { NodeForm } from './NodeForm'
import { ROLE_INPUT_DYNAMIC_SUBMIT } from './roles'
import { buttonSizeFromSchemaSize } from './helper/styleHelper'
// TODO: Add overall end to end test for all types of form
// TODO: Add test for keys with large schema.
export const DynamicForm = ({
schema,
onChange = () => {},
onSubmit,
submitSize = DynamicFormSize.medium
}) => {
// Value container to store values.
const valueContainer = useRef({})
const changeValue = ({ newValue }) => {
valueContainer.current = newValue
// Update out function that value is changed.
if (util.isFunction(onChange)) {
util.dispatchAsync(() => {
onChange({ ...valueContainer.current })
})
}
}
const submitButtonSize = buttonSizeFromSchemaSize(submitSize)
const className = {
card: 'd-grid gap-1',
submitButton: `btn btn-success mx-1 w-auto ${submitButtonSize}`
}
let submitButton = null
if (util.isFunction(onSubmit)) {
submitButton = (
<button
className={className.submitButton}
role={ROLE_INPUT_DYNAMIC_SUBMIT}
onClick={() => {
onSubmit({ ...valueContainer.current })
}}
>
Submit
</button>
)
}
return (
<div className={className.card}>
<NodeForm schema={schema} onChange={changeValue} />
{submitButton}
</div>
)
}
DynamicForm.propTypes = {
schema: PropTypes.object.isRequired,
onChange: PropTypes.func,
onSubmit: PropTypes.func,
submitSize: PropTypes.string // TODO: Check only for allowed values.
}