Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Checkbox.js to function component #20193

Merged
merged 14 commits into from
Jun 15, 2023
104 changes: 47 additions & 57 deletions src/components/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,77 +47,67 @@ const defaultProps = {
onMouseDown: undefined,
};

class Checkbox extends React.Component {
constructor(props) {
super(props);

this.handleSpaceKey = this.handleSpaceKey.bind(this);
this.firePressHandlerOnClick = this.firePressHandlerOnClick.bind(this);
}

handleSpaceKey(event) {
function Checkbox(props) {
const handleSpaceKey = (event) => {
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
if (event.code !== 'Space') {
return;
}

this.props.onPress();
}
props.onPress();
};

firePressHandlerOnClick(event) {
const firePressHandlerOnClick = (event) => {
// Pressable can be triggered with Enter key and by a click. As this is a checkbox,
// We do not want to toggle it, when Enter key is pressed.
if (event.type && event.type !== 'click') {
return;
}

this.props.onPress();
}

render() {
return (
<Pressable
disabled={this.props.disabled}
onPress={this.firePressHandlerOnClick}
onMouseDown={this.props.onMouseDown}
ref={this.props.forwardedRef}
style={this.props.style}
onKeyDown={this.handleSpaceKey}
accessibilityRole="checkbox"
accessibilityState={{
checked: this.props.isChecked,
}}
>
{this.props.children ? (
this.props.children
) : (
<View
style={[
styles.checkboxContainer,
this.props.containerStyle,
this.props.isChecked && styles.checkedContainer,
this.props.hasError && styles.borderColorDanger,
this.props.disabled && styles.cursorDisabled,
this.props.isChecked && styles.borderColorFocus,
]}
// Used as CSS selector to customize focus-visible style
dataSet={{checkbox: true}}
>
{this.props.isChecked && (
<Icon
src={Expensicons.Checkmark}
fill={themeColors.textLight}
height={14}
width={14}
/>
)}
</View>
)}
</Pressable>
);
}
props.onPress();
};

return (
<Pressable
disabled={props.disabled}
onPress={firePressHandlerOnClick}
onMouseDown={props.onMouseDown}
ref={props.forwardedRef}
style={props.style}
onKeyDown={handleSpaceKey}
accessibilityRole="checkbox"
accessibilityState={{checked: props.isChecked}}
>
{props.children ? (
props.children
) : (
<View
style={[
styles.checkboxContainer,
props.containerStyle,
props.isChecked && styles.checkedContainer,
props.hasError && styles.borderColorDanger,
props.disabled && styles.cursorDisabled,
props.isChecked && styles.borderColorFocus,
]}
// Used as CSS selector to customize focus-visible style
dataSet={{checkbox: true}}
>
{props.isChecked && (
<Icon
src={Expensicons.Checkmark}
fill={themeColors.textLight}
height={14}
width={14}
/>
)}
</View>
)}
</Pressable>
);
}

Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;
Checkbox.displayName = 'Checkbox';
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved

export default Checkbox;