-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberInput.jsx
97 lines (84 loc) · 2.5 KB
/
NumberInput.jsx
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
import { Form } from 'react-bootstrap';
import PropTypes from 'prop-types';
import { isEmpty, isNil } from 'lodash-es';
import { FormStates as FORM_STATES } from '../../lib';
const FIELD_PROPERTIES = {
LABEL: 'label',
IS_MANDATORY: 'isMandatory',
};
const NumberInputControl = ({ fieldProperties, fieldValue, setFieldValue, formState }) => {
const { label, isMandatory } = fieldProperties;
const handleValueChange = (e) => {
setFieldValue(e.target.value);
};
const labelDisplayValue = isEmpty(label) ? '' : label;
return (
<>
<Form.Label>{`${labelDisplayValue} ${isMandatory ? '*' : ''}`}</Form.Label>
<Form.Control
disabled={formState === FORM_STATES.FORM_VIEW}
value={isNil(fieldValue) ? '' : fieldValue}
type="number"
onChange={handleValueChange}
/>
</>
);
};
export const NumberInputFieldPropertiesControl = ({ setProperties, fieldProperties }) => {
const handleOptionChange = (type) => (e) => {
const updatedProps = { ...fieldProperties };
switch (type) {
case FIELD_PROPERTIES.LABEL:
updatedProps[type] = e.target.value;
break;
case FIELD_PROPERTIES.IS_MANDATORY:
updatedProps[type] = !updatedProps[type];
break;
default:
break;
}
setProperties(updatedProps);
};
return (
<>
<Form.Group>
<Form.Label>Field Name</Form.Label>
<Form.Control
type="text"
placeholder="Field Name"
value={fieldProperties[FIELD_PROPERTIES.LABEL] || ''}
onChange={handleOptionChange(FIELD_PROPERTIES.LABEL)}
/>
</Form.Group>
<br />
<Form.Check
type="checkbox"
id="mandatory-checkbox"
label="Mandatory"
checked={fieldProperties[FIELD_PROPERTIES.IS_MANDATORY] || false}
onChange={handleOptionChange(FIELD_PROPERTIES.IS_MANDATORY)}
/>
</>
);
};
const NumberInput = {
type: 'NUMBER_INPUT',
displayName: 'Number Input',
Control: NumberInputControl,
FieldPropertiesControl: NumberInputFieldPropertiesControl,
onChange: () => {},
};
NumberInputControl.propTypes = {
fieldValue: PropTypes.string,
setFieldValue: PropTypes.func.isRequired,
fieldProperties: PropTypes.object.isRequired,
formState: PropTypes.string.isRequired,
};
NumberInputControl.defaultProps = {
fieldValue: '',
};
NumberInputFieldPropertiesControl.propTypes = {
setProperties: PropTypes.func.isRequired,
fieldProperties: PropTypes.object.isRequired,
};
export default NumberInput;