Debian Codegraph @

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

raw
1 import { Component, Fragment } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Switch/switch';
3 import { css } from '@patternfly/react-styles';
4 import CheckIcon from '@patternfly/react-icons/dist/esm/icons/check-icon';
5 import { getOUIAProps, OUIAProps } from '../../helpers';
6 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
7
8 export interface SwitchProps
9 extends Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled' | 'label'>, OUIAProps {
10 /** id for the label. */
11 id?: string;
12 /** Additional classes added to the switch */
13 className?: string;
14 /** Text value for the visible label */
15 label?: React.ReactNode;
16 /** Adds an accessible name to the switch when the label prop is not passed, and must describe the isChecked="true" state. */
17 'aria-label'?: string;
18 /** Adds an accessible name to the switch via one or more referenced id(s). The computed accessible name must describe the isChecked="true" state. */
19 'aria-labelledby'?: string;
20 /** Flag to show if the switch is checked when it is controlled by React state.
21 * To make the switch uncontrolled instead use the defaultChecked prop, but do not use both.
22 */
23 isChecked?: boolean;
24 /** Flag to set the default checked value of the switch when it is uncontrolled by React state.
25 * To make the switch controlled instead use the isChecked prop, but do not use both.
26 */
27 defaultChecked?: boolean;
28 /** Flag to show if the switch has a check icon. */
29 hasCheckIcon?: boolean;
30 /** Flag to show if the switch is disabled. */
31 isDisabled?: boolean;
32 /** A callback for when the switch selection changes. (event, isChecked) => {} */
33 onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void;
34 /** Flag to reverse the layout of toggle and label (label at start, toggle at end). */
35 isReversed?: boolean;
36 /** Value to overwrite the randomly generated data-ouia-component-id.*/
37 ouiaId?: number | string;
38 /** 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. */
39 ouiaSafe?: boolean;
40 }
41
42 class Switch extends Component<SwitchProps & OUIAProps> {
43 static displayName = 'Switch';
44
45 static defaultProps: SwitchProps = {
46 isChecked: true,
47 isDisabled: false,
48 isReversed: false,
49 'aria-label': undefined,
50 'aria-labelledby': undefined,
51 onChange: () => undefined as any
52 };
53
54 constructor(props: SwitchProps & OUIAProps) {
55 super(props);
56 if (!props.label && !props['aria-label'] && !props['aria-labelledby']) {
57 // eslint-disable-next-line no-console
58 console.error(
59 'Switch: Switch requires at least one of label, aria-labelledby, or aria-label props to be specified'
60 );
61 }
62 }
63
64 render() {
65 const {
66 id: idProp,
67 className,
68 label,
69 isChecked,
70 defaultChecked,
71 hasCheckIcon,
72 isDisabled,
73 onChange,
74 isReversed,
75 ouiaId,
76 ouiaSafe,
77 'aria-label': ariaLabel,
78 'aria-labelledby': ariaLabelledBy,
79 ...props
80 } = this.props;
81
82 return (
83 <SSRSafeIds prefix="pf-switch-" ouiaComponentType="Switch">
84 {(generatedId, generatedOuiaId) => {
85 const id = idProp || generatedId;
86 const hasAccessibleName = label || ariaLabel || ariaLabelledBy;
87 const isAriaLabelledBy = hasAccessibleName && (!ariaLabel || ariaLabelledBy);
88 const useDefaultAriaLabelledBy = !ariaLabelledBy && !ariaLabel;
89 const ariaLabelledByIds = ariaLabelledBy ?? `${id}-label`;
90
91 return (
92 <label
93 className={css(styles.switch, isReversed && styles.modifiers.reverse, className)}
94 htmlFor={id}
95 {...getOUIAProps(Switch.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)}
96 >
97 <input
98 id={id}
99 className={css(styles.switchInput)}
100 type="checkbox"
101 role="switch"
102 onChange={(event) => onChange(event, event.target.checked)}
103 {...(defaultChecked !== undefined ? { defaultChecked } : { checked: isChecked })}
104 disabled={isDisabled}
105 aria-labelledby={isAriaLabelledBy ? ariaLabelledByIds : null}
106 aria-label={ariaLabel}
107 {...props}
108 />
109 {label !== undefined ? (
110 <Fragment>
111 <span className={css(styles.switchToggle)}>
112 {hasCheckIcon && (
113 <span className={css(styles.switchToggleIcon)}>
114 <CheckIcon />
115 </span>
116 )}
117 </span>
118 <span
119 className={css(styles.switchLabel)}
120 id={isAriaLabelledBy && useDefaultAriaLabelledBy ? `${id}-label` : null}
121 aria-hidden="true"
122 >
123 {label}
124 </span>
125 </Fragment>
126 ) : (
127 <span className={css(styles.switchToggle)}>
128 <div className={css(styles.switchToggleIcon)}>
129 <CheckIcon />
130 </div>
131 </span>
132 )}
133 </label>
134 );
135 }}
136 </SSRSafeIds>
137 );
138 }
139 }
140
141 export { Switch };