-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
FormCheck.tsx
206 lines (182 loc) · 5.19 KB
/
FormCheck.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { useContext, useMemo } from 'react';
import Feedback from './Feedback';
import FormCheckInput from './FormCheckInput';
import FormCheckLabel from './FormCheckLabel';
import FormContext from './FormContext';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
export type FormCheckType = 'checkbox' | 'radio' | 'switch';
export interface FormCheckProps
extends BsPrefixProps,
React.InputHTMLAttributes<HTMLInputElement> {
inline?: boolean;
disabled?: boolean;
label?: React.ReactNode;
type?: FormCheckType;
isValid?: boolean;
isInvalid?: boolean;
feedbackTooltip?: boolean;
feedback?: React.ReactNode;
bsSwitchPrefix?: string;
}
const propTypes = {
/**
* @default 'form-check'
*/
bsPrefix: PropTypes.string,
/**
* bsPrefix override for the base switch class.
*
* @default 'form-switch'
*/
bsSwitchPrefix: PropTypes.string,
/**
* The FormCheck `ref` will be forwarded to the underlying input element,
* which means it will be a DOM node, when resolved.
*
* @type {ReactRef}
* @alias ref
*/
_ref: PropTypes.any,
/**
* The underlying HTML element to use when rendering the FormCheck.
*
* @type {('input'|elementType)}
*/
as: PropTypes.elementType,
/**
* A HTML id attribute, necessary for proper form accessibility.
* An id is recommended for allowing label clicks to toggle the check control.
*
* This is **required** when `type="switch"` due to how they are rendered.
*/
id: PropTypes.string,
/**
* Provide a function child to manually handle the layout of the FormCheck's inner components.
*
* ```jsx
* <FormCheck>
* <FormCheck.Input isInvalid type={radio} />
* <FormCheck.Label>Allow us to contact you?</FormCheck.Label>
* <Feedback type="invalid">Yo this is required</Feedback>
* </FormCheck>
* ```
*/
children: PropTypes.node,
/**
* Groups controls horizontally with other `FormCheck`s.
*/
inline: PropTypes.bool,
/**
* Disables the control.
*/
disabled: PropTypes.bool,
/**
* `title` attribute for the underlying `FormCheckLabel`.
*/
title: PropTypes.string,
/**
* Label for the control.
*/
label: PropTypes.node,
/**
* The type of checkable.
* @type {('radio' | 'checkbox' | 'switch')}
*/
type: PropTypes.oneOf(['radio', 'checkbox', 'switch']),
/** Manually style the input as valid */
isValid: PropTypes.bool,
/** Manually style the input as invalid */
isInvalid: PropTypes.bool,
/** Display feedback as a tooltip. */
feedbackTooltip: PropTypes.bool,
/** A message to display when the input is in a validation state */
feedback: PropTypes.node,
};
const FormCheck: BsPrefixRefForwardingComponent<'input', FormCheckProps> =
React.forwardRef<HTMLInputElement, FormCheckProps>(
(
{
id,
bsPrefix,
bsSwitchPrefix,
inline = false,
disabled = false,
isValid = false,
isInvalid = false,
feedbackTooltip = false,
feedback,
className,
style,
title = '',
type = 'checkbox',
label,
children,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as = 'input',
...props
},
ref,
) => {
bsPrefix = useBootstrapPrefix(bsPrefix, 'form-check');
bsSwitchPrefix = useBootstrapPrefix(bsSwitchPrefix, 'form-switch');
const { controlId } = useContext(FormContext);
const innerFormContext = useMemo(
() => ({
controlId: id || controlId,
}),
[controlId, id],
);
const hasLabel = label != null && label !== false && !children;
const input = (
<FormCheckInput
{...props}
type={type === 'switch' ? 'checkbox' : type}
ref={ref}
isValid={isValid}
isInvalid={isInvalid}
disabled={disabled}
as={as}
/>
);
return (
<FormContext.Provider value={innerFormContext}>
<div
style={style}
className={classNames(
className,
label && bsPrefix,
inline && `${bsPrefix}-inline`,
type === 'switch' && bsSwitchPrefix,
)}
>
{children || (
<>
{input}
{hasLabel && (
<FormCheckLabel title={title}>{label}</FormCheckLabel>
)}
{(isValid || isInvalid) && (
<Feedback
type={isValid ? 'valid' : 'invalid'}
tooltip={feedbackTooltip}
>
{feedback}
</Feedback>
)}
</>
)}
</div>
</FormContext.Provider>
);
},
);
FormCheck.displayName = 'FormCheck';
FormCheck.propTypes = propTypes;
export default Object.assign(FormCheck, {
Input: FormCheckInput,
Label: FormCheckLabel,
});