forked from rjsf-team/react-jsonschema-form
/
CheckboxWidget.tsx
56 lines (51 loc) · 1.43 KB
/
CheckboxWidget.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
import React from "react";
import { WidgetProps, schemaRequiresTrueValue } from "@rjsf/utils";
import Form from "react-bootstrap/Form";
const CheckboxWidget = (props: WidgetProps) => {
const {
id,
value,
disabled,
readonly,
label,
schema,
autofocus,
onChange,
onBlur,
onFocus,
} = props;
// Because an unchecked checkbox will cause html5 validation to fail, only add
// the "required" attribute if the field value must be "true", due to the
// "const" or "enum" keywords
const required = schemaRequiresTrueValue(schema);
const _onChange = ({
target: { checked },
}: React.FocusEvent<HTMLInputElement>) => onChange(checked);
const _onBlur = ({
target: { checked },
}: React.FocusEvent<HTMLInputElement>) => onBlur(id, checked);
const _onFocus = ({
target: { checked },
}: React.FocusEvent<HTMLInputElement>) => onFocus(id, checked);
const desc = label || schema.description;
return (
<Form.Group
className={`checkbox ${disabled || readonly ? "disabled" : ""}`}
>
<Form.Check
id={id}
name={id}
label={desc}
checked={typeof value === "undefined" ? false : value}
required={required}
disabled={disabled || readonly}
autoFocus={autofocus}
onChange={_onChange}
type="checkbox"
onBlur={_onBlur}
onFocus={_onFocus}
/>
</Form.Group>
);
};
export default CheckboxWidget;