forked from rjsf-team/react-jsonschema-form
/
WrapIfAdditionalTemplate.tsx
70 lines (63 loc) · 1.71 KB
/
WrapIfAdditionalTemplate.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
import React from "react";
import {
ADDITIONAL_PROPERTY_FLAG,
WrapIfAdditionalTemplateProps,
} from "@rjsf/utils";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import Form from "react-bootstrap/Form";
const WrapIfAdditionalTemplate = ({
classNames,
children,
disabled,
id,
label,
onDropPropertyClick,
onKeyChange,
readonly,
required,
schema,
uiSchema,
registry,
}: WrapIfAdditionalTemplateProps) => {
// Button templates are not overridden in the uiSchema
const { RemoveButton } = registry.templates.ButtonTemplates;
const keyLabel = `${label} Key`; // i18n ?
const additional = ADDITIONAL_PROPERTY_FLAG in schema;
if (!additional) {
return <div className={classNames}>{children}</div>;
}
const handleBlur = ({ target }: React.FocusEvent<HTMLInputElement>) =>
onKeyChange(target.value);
const keyId = `${id}-key`;
return (
<Row className={classNames} key={keyId}>
<Col xs={5}>
<Form.Group>
<Form.Label htmlFor={keyId}>{keyLabel}</Form.Label>
<Form.Control
required={required}
defaultValue={label}
disabled={disabled || readonly}
id={keyId}
name={keyId}
onBlur={!readonly ? handleBlur : undefined}
type="text"
/>
</Form.Group>
</Col>
<Col xs={5}>{children}</Col>
<Col xs={2} className="py-4">
<RemoveButton
iconType="block"
className="w-100"
disabled={disabled || readonly}
onClick={onDropPropertyClick(label)}
uiSchema={uiSchema}
registry={registry}
/>
</Col>
</Row>
);
};
export default WrapIfAdditionalTemplate;