Debian Codegraph @

node/@patternfly/react-core/src/components/MenuToggle/MenuToggleCheckbox.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
8 export interface MenuToggleCheckboxProps
9 extends Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled' | 'checked'>, OUIAProps {
10 /** Additional classes added to the MenuToggleCheckbox */
11 className?: string;
12 /** Flag to show if the checkbox selection is valid or invalid */
13 isValid?: boolean;
14 /** Flag to show if the checkbox is disabled */
15 isDisabled?: boolean;
16 /** Flag to show if the checkbox is checked when it is controlled by React state.
17 * To make the checkbox uncontrolled instead use the defaultChecked prop, but do not use both.
18 */
19 isChecked?: boolean | null;
20 /** Flag to set the default checked value of the checkbox when it is uncontrolled by React state.
21 * To make the checkbox controlled instead use the isChecked prop, but do not use both.
22 */
23 defaultChecked?: boolean;
24 /** A callback for when the checkbox selection changes */
25 onChange?: (checked: boolean, event: React.FormEvent<HTMLInputElement>) => void;
26 /** Element to be rendered inside the <span> */
27 children?: React.ReactNode;
28 /** Id of the checkbox */
29 id: string;
30 /** Value to overwrite the randomly generated data-ouia-component-id.*/
31 ouiaId?: number | string;
32 /** 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. */
33 ouiaSafe?: boolean;
34 }
35
36 class MenuToggleCheckbox extends Component<MenuToggleCheckboxProps> {
37 static displayName = 'MenuToggleCheckbox';
38 static defaultProps: PickOptional<MenuToggleCheckboxProps> = {
39 isValid: true,
40 isDisabled: false,
41 onChange: () => undefined as any
42 };
43
44 handleChange = (event: React.FormEvent<HTMLInputElement>) => {
45 this.props.onChange((event.target as HTMLInputElement).checked, event);
46 };
47
48 calculateChecked = () => {
49 const { isChecked, defaultChecked } = this.props;
50 if (isChecked === null) {
51 // return false here and the indeterminate state will be set to true through the ref
52 return false;
53 } else if (isChecked !== undefined) {
54 return isChecked;
55 }
56 return defaultChecked;
57 };
58
59 render() {
60 const {
61 className,
62 isValid,
63 isDisabled,
64 isChecked,
65 children,
66 ouiaId,
67 ouiaSafe,
68 /* eslint-disable @typescript-eslint/no-unused-vars */
69 onChange,
70 defaultChecked,
71 id,
72 ...props
73 } = this.props;
74 const text = children && (
75 <span className={css(styles.checkLabel, className)} aria-hidden="true" id={id}>
76 {children}
77 </span>
78 );
79 return (
80 <SSRSafeIds prefix="pf-" ouiaComponentType={MenuToggleCheckbox.displayName}>
81 {(_, generatedOuiaId) => (
82 <label className={css(styles.check, !children && styles.modifiers.standalone, className)}>
83 <input
84 className={css(styles.checkInput)}
85 {...(this.calculateChecked() !== undefined && { onChange: this.handleChange })}
86 name={id}
87 type="checkbox"
88 ref={(elem) => {
89 elem && (elem.indeterminate = isChecked === null);
90 }}
91 aria-invalid={!isValid}
92 disabled={isDisabled}
93 {...(defaultChecked !== undefined ? { defaultChecked } : { checked: this.calculateChecked() })}
94 {...getOUIAProps(
95 MenuToggleCheckbox.displayName,
96 ouiaId !== undefined ? ouiaId : generatedOuiaId,
97 ouiaSafe
98 )}
99 {...props}
100 />
101 {text}
102 </label>
103 )}
104 </SSRSafeIds>
105 );
106 }
107 }
108
109 export { MenuToggleCheckbox };