-
Notifications
You must be signed in to change notification settings - Fork 3
/
SwitchField.jsx
121 lines (117 loc) · 2.86 KB
/
SwitchField.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
FormControl,
FormGroup,
FormHelperText,
FormLabel,
Stack,
Switch,
Typography
} from "@mui/material";
import PropTypes from "prop-types";
import React from "react";
/**
* Switch form component with layout to match IPG style. Must include two text options which are displayed either side of the switch.
*/
export default function FormSwitch({
checked,
defaultChecked,
disabled = false,
helperText,
label,
name,
onChange,
options,
size = "medium"
}) {
// return components
return (
<FormControl disabled={disabled}>
{label && <FormLabel style={{ fontSize: "0.75rem" }}>{label}</FormLabel>}
<FormGroup>
<Typography
component="div"
color={disabled ? "textSecondary" : "textPrimary"}
>
<Stack alignItems="center" component="label" direction="row">
<SwitchOptionLabel disabled={disabled} label={options[0]} />
<Switch
checked={checked}
defaultChecked={defaultChecked}
name={name}
onChange={onChange}
size={size}
/>
<SwitchOptionLabel disabled={disabled} label={options[1]} />
</Stack>
</Typography>
{helperText && <FormHelperText>{helperText}</FormHelperText>}
</FormGroup>
</FormControl>
);
}
/**
* Returns a label for displaying on either side of the switch
*/
function SwitchOptionLabel({ disabled, label }) {
return (
<Typography color={disabled ? "textSecondary" : "textPrimary"}>
{label}
</Typography>
);
}
// prop types
FormSwitch.propTypes = {
/**
* If true, the component is checked.
*/
checked: PropTypes.bool,
/**
* The default value of the input.
*/
defaultChecked: PropTypes.bool,
/**
* If true, the switch will be disabled.
*/
disabled: PropTypes.bool,
/**
* The helper text content.
*/
helperText: PropTypes.string,
/**
* The label content.
*/
label: PropTypes.string,
/**
* The name of the input.
*/
name: PropTypes.string,
/**
* Callback fired when the state is changed.
*
* **Signature**
* ```
* function(event) => void
* ```
*
* _event_: The event source of the callback. You can pull out the new checked state by accessing event.target.checked (boolean).
*/
onChange: PropTypes.func,
/**
* Options to display either side of the switch. Must be an array of length 2, with type string.
*/
options: function (props, propName, componentName) {
if (
!Array.isArray(props[propName]) ||
props[propName].length !== 2 ||
!props[propName].every(s => typeof s === "string")
) {
return new Error(
`Invalid prop ${propName} supplied to ${componentName}. Expected a string array of length 2.`
);
}
},
/**
* The size of switch.
*/
size: PropTypes.oneOf(["small", "medium"])
};