Debian Codegraph @

node/@patternfly/react-core/src/components/Checkbox/Checkbox.tsx

raw
1 import { Component } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Check/check';
3 import { css } from '@patternfly/react-styles';
4 import { PickOptional } from '../../helpers/typeUtils';
5 import { getOUIAProps, OUIAProps } from '../../helpers';
6 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
7 import { ASTERISK } from '../../helpers/htmlConstants';
8
9 export interface CheckboxProps
10 extends Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled' | 'label'>, OUIAProps {
11 /** Additional classes added to the checkbox wrapper. This wrapper will be div element by default. It will be a label element if
12 * isLabelWrapped is true, or it can be overridden by any element specified in the component prop.
13 */
14 className?: string;
15 /** Additional classes added to the checkbox input. */
16 inputClassName?: string;
17 /** Flag to indicate whether the checkbox wrapper element is a <label> element for the checkbox input. Will not apply if a component prop (with a value other than a "label") is specified. */
18 isLabelWrapped?: boolean;
19 /** Flag to show if the checkbox selection is valid or invalid. */
20 isValid?: boolean;
21 /** Flag to show if the checkbox is disabled. */
22 isDisabled?: boolean;
23 /** Flag to show if the checkbox is required. */
24 isRequired?: boolean;
25 /** Flag to show if the checkbox is checked. If null, the checkbox will be indeterminate (partially checked). */
26 isChecked?: boolean | null;
27 checked?: boolean;
28 /** A callback for when the checkbox selection changes. */
29 onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void;
30 /** Label text of the checkbox. */
31 label?: React.ReactNode;
32 /** Sets the position of the label. Defaults to 'end' (after the checkbox input). */
33 labelPosition?: 'start' | 'end';
34 /** Id of the checkbox. */
35 id: string;
36 /** Aria-label of the checkbox. */
37 'aria-label'?: string;
38 /** Description text of the checkbox. */
39 description?: React.ReactNode;
40 /** Body text of the checkbox */
41 body?: React.ReactNode;
42 /** Custom aria-describedby value for the checkbox input. If not provided and description is set, a unique ID will be generated automatically. */
43 'aria-describedby'?: string;
44 /** Sets the checkbox wrapper component to render. Defaults to "div". If set to "label", behaves the same as if isLabelWrapped prop was specified. */
45 component?: React.ElementType;
46 /** Value to overwrite the randomly generated data-ouia-component-id.*/
47 ouiaId?: number | string;
48 /** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
49 ouiaSafe?: boolean;
50 }
51
52 // tslint:disable-next-line:no-empty
53 const defaultOnChange = () => {};
54
55 class Checkbox extends Component<CheckboxProps> {
56 static displayName = 'Checkbox';
57 static defaultProps: PickOptional<CheckboxProps> = {
58 className: '',
59 isLabelWrapped: false,
60 isValid: true,
61 isDisabled: false,
62 isRequired: false,
63 isChecked: false,
64 onChange: defaultOnChange,
65 ouiaSafe: true
66 };
67
68 private handleChange = (event: React.FormEvent<HTMLInputElement>): void => {
69 this.props.onChange(event, event.currentTarget.checked);
70 };
71
72 render() {
73 const {
74 'aria-label': ariaLabel,
75 'aria-describedby': ariaDescribedBy,
76 className,
77 inputClassName,
78 onChange,
79 isLabelWrapped,
80 labelPosition = 'end',
81 isValid,
82 isDisabled,
83 isRequired,
84 isChecked,
85 label,
86 checked,
87 defaultChecked,
88 description,
89 body,
90 ouiaId,
91 ouiaSafe,
92 component,
93 ...props
94 } = this.props;
95 if (!props.id) {
96 // eslint-disable-next-line no-console
97 console.error('Checkbox:', 'id is required to make input accessible');
98 }
99 const checkedProps: { checked?: boolean; defaultChecked?: boolean } = {};
100 if ([true, false].includes(checked) || isChecked === true) {
101 checkedProps.checked = checked || isChecked;
102 }
103 if (onChange !== defaultOnChange) {
104 checkedProps.checked = isChecked;
105 }
106 if ([false, true].includes(defaultChecked)) {
107 checkedProps.defaultChecked = defaultChecked;
108 }
109
110 return (
111 <SSRSafeIds prefix="pf-checkbox-description-" ouiaComponentType="Checkbox">
112 {(descriptionId, generatedOuiaId) => {
113 let ariaDescribedByValue: string | undefined;
114 if (ariaDescribedBy !== undefined) {
115 ariaDescribedByValue = ariaDescribedBy === '' ? undefined : ariaDescribedBy;
116 } else if (description) {
117 ariaDescribedByValue = descriptionId;
118 }
119
120 const inputRendered = (
121 <input
122 {...props}
123 className={css(styles.checkInput, inputClassName)}
124 type="checkbox"
125 onChange={this.handleChange}
126 aria-invalid={!isValid}
127 aria-label={ariaLabel}
128 aria-describedby={ariaDescribedByValue}
129 disabled={isDisabled}
130 required={isRequired}
131 ref={(elem) => {
132 elem && (elem.indeterminate = isChecked === null);
133 }}
134 {...checkedProps}
135 {...getOUIAProps(Checkbox.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)}
136 />
137 );
138
139 const wrapWithLabel = (isLabelWrapped && !component) || component === 'label';
140
141 const Label = wrapWithLabel ? 'span' : 'label';
142 const labelRendered = label ? (
143 <Label
144 className={css(styles.checkLabel, isDisabled && styles.modifiers.disabled)}
145 htmlFor={!wrapWithLabel ? props.id : undefined}
146 >
147 {label}
148 {isRequired && (
149 <span className={css(styles.checkLabelRequired)} aria-hidden="true">
150 {ASTERISK}
151 </span>
152 )}
153 </Label>
154 ) : null;
155
156 const WrapperComponent = component ?? (wrapWithLabel ? 'label' : 'div');
157
158 checkedProps.checked = checkedProps.checked === null ? false : checkedProps.checked;
159 return (
160 <WrapperComponent
161 className={css(styles.check, !label && styles.modifiers.standalone, className)}
162 htmlFor={wrapWithLabel ? props.id : undefined}
163 >
164 {labelPosition === 'start' ? (
165 <>
166 {labelRendered}
167 {inputRendered}
168 </>
169 ) : (
170 <>
171 {inputRendered}
172 {labelRendered}
173 </>
174 )}
175 {description && (
176 <span id={descriptionId} className={css(styles.checkDescription)}>
177 {description}
178 </span>
179 )}
180 {body && <span className={css(styles.checkBody)}>{body}</span>}
181 </WrapperComponent>
182 );
183 }}
184 </SSRSafeIds>
185 );
186 }
187 }
188
189 export { Checkbox };