Skip to content

Commit

Permalink
refactor(MenuToggleCheckbox): use Checkbox component internally
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Nov 20, 2023
1 parent 4b30981 commit 75b1cc7
Showing 1 changed file with 11 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/Check/check';
import { css } from '@patternfly/react-styles';
import { PickOptional } from '../../helpers/typeUtils';
import { getOUIAProps, OUIAProps, getDefaultOUIAId } from '../../helpers';
import { OUIAProps } from '../../helpers';
import { Checkbox } from '../Checkbox';

export interface MenuToggleCheckboxProps
extends Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'onChange' | 'disabled' | 'checked'>,
Expand Down Expand Up @@ -33,7 +32,7 @@ export interface MenuToggleCheckboxProps
ouiaSafe?: boolean;
}

class MenuToggleCheckbox extends React.Component<MenuToggleCheckboxProps, { ouiaStateId: string }> {
class MenuToggleCheckbox extends React.Component<MenuToggleCheckboxProps> {
static displayName = 'MenuToggleCheckbox';
static defaultProps: PickOptional<MenuToggleCheckboxProps> = {
isValid: true,
Expand All @@ -43,66 +42,24 @@ class MenuToggleCheckbox extends React.Component<MenuToggleCheckboxProps, { ouia

constructor(props: MenuToggleCheckboxProps) {
super(props);
this.state = {
ouiaStateId: getDefaultOUIAId(MenuToggleCheckbox.displayName)
};
}

handleChange = (event: React.FormEvent<HTMLInputElement>) => {
this.props.onChange((event.target as HTMLInputElement).checked, event);
};

calculateChecked = () => {
const { isChecked, defaultChecked } = this.props;
if (isChecked === null) {
// return false here and the indeterminate state will be set to true through the ref
return false;
} else if (isChecked !== undefined) {
return isChecked;
}
return defaultChecked;
};

render() {
const {
className,
isValid,
isDisabled,
isChecked,
children,
ouiaId,
ouiaSafe,
/* eslint-disable @typescript-eslint/no-unused-vars */
onChange,
defaultChecked,
id,
...props
} = this.props;
const text = children && (
<span className={css(styles.checkLabel, className)} aria-hidden="true" id={id}>
{children}
</span>
);

return (
<label className={css(styles.check, !children && styles.modifiers.standalone, className)} htmlFor={id}>
<input
className={css(styles.checkInput)}
{...(this.calculateChecked() !== undefined && { onChange: this.handleChange })}
name={id}
type="checkbox"
ref={(elem) => elem && (elem.indeterminate = isChecked === null)}
aria-invalid={!isValid}
disabled={isDisabled}
{...(defaultChecked !== undefined ? { defaultChecked } : { checked: isChecked })}
{...getOUIAProps(
MenuToggleCheckbox.displayName,
ouiaId !== undefined ? ouiaId : this.state.ouiaStateId,
ouiaSafe
)}
{...props}
/>
{text}
</label>
// @ts-ignore

Check failure on line 56 in packages/react-core/src/components/MenuToggle/MenuToggleCheckbox.tsx

View workflow job for this annotation

GitHub Actions / lint

Do not use "@ts-ignore" because it alters compilation errors
<Checkbox
onChange={(event: React.FormEvent<HTMLInputElement>, checked: boolean) => onChange(checked, event)}
isLabelWrapped
label={children}
{...props}
></Checkbox>
);
}
}
Expand Down

0 comments on commit 75b1cc7

Please sign in to comment.