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

Fix checkbox accessibility #8874

Merged
merged 30 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
dce0b2c
fix: checkbox accessibility (toggle with space)
mdneyazahmad May 4, 2022
13bf388
feat: add style prop to CheckboxButton
mdneyazahmad May 4, 2022
d589249
fix: secure textinput eye icon accesibility
mdneyazahmad May 4, 2022
acf7c34
fix: add missing propTypes and defaultProps
mdneyazahmad May 4, 2022
9e908cd
fix: linting errors
mdneyazahmad May 4, 2022
a02facd
fix: lint errors
mdneyazahmad May 4, 2022
e64cbc9
fix: error on native checkbox
mdneyazahmad May 6, 2022
4e668a6
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad May 6, 2022
4215fb8
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad May 9, 2022
763eed8
refactor: move checkboxes files
mdneyazahmad May 9, 2022
d708f43
refactor: add space keycode in CONST file
mdneyazahmad May 11, 2022
833cc6d
refactor: remove displayName from class component
mdneyazahmad May 11, 2022
33ab163
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad May 15, 2022
b382d2c
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad May 20, 2022
c041a03
Refactor CheckboxButton component
mdneyazahmad Jun 9, 2022
2afa23b
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad Jun 9, 2022
5c135a2
Delete CheckboxButton component
mdneyazahmad Jun 12, 2022
9aa62a3
Implement platform specific behavior for checkbox
mdneyazahmad Jun 13, 2022
fba92ce
Add checkbox accessibility role
mdneyazahmad Jun 16, 2022
091bb02
Refactor checkbox toggle function
mdneyazahmad Jun 16, 2022
af7dcee
Update comment for Checkbox component
mdneyazahmad Jun 16, 2022
17d1524
Merge branch 'main' of github.com:Expensify/App into fix/checkbox
mdneyazahmad Jun 16, 2022
6664d15
fix: merge conflict
mdneyazahmad Aug 20, 2022
7a6200c
Add focus ring to checkbox
mdneyazahmad Aug 20, 2022
4847fb3
Remove focus from checkbox label
mdneyazahmad Aug 20, 2022
0799705
Merge branch 'main' into fix/checkbox
mdneyazahmad Aug 28, 2022
0afa9b5
Refactor checkbox
mdneyazahmad Aug 28, 2022
80b5961
Fix prop to correct name
mdneyazahmad Aug 28, 2022
a0c17db
Refactor checkbox
mdneyazahmad Aug 29, 2022
03f1115
Remove focus ring from checkbox label
mdneyazahmad Aug 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 82 additions & 19 deletions src/components/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import {View, Pressable} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles/styles';
import stylePropTypes from '../styles/stylePropTypes';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';

Expand All @@ -18,6 +19,12 @@ const propTypes = {
/** Should the input be disabled */
disabled: PropTypes.bool,

/** Children (icon) for Checkbox */
children: PropTypes.node,

/** Additional styles to add to checkbox button */
style: stylePropTypes,

/** A ref to forward to the Pressable */
forwardedRef: PropTypes.oneOfType([
PropTypes.func,
Expand All @@ -28,30 +35,86 @@ const propTypes = {
const defaultProps = {
hasError: false,
disabled: false,
style: [],
forwardedRef: undefined,
children: null,
};

const Checkbox = props => (
<Pressable
disabled={props.disabled}
onPress={() => props.onPress(!props.isChecked)}
ref={props.forwardedRef}
>
<View
style={[
styles.checkboxContainer,
props.isChecked && styles.checkedContainer,
props.hasError && styles.borderColorDanger,
props.disabled && styles.cursorDisabled,
]}
>
<Icon src={Expensicons.Checkmark} fill="white" height={14} width={14} />
</View>
</Pressable>
);
class Checkbox extends React.Component {
constructor(props) {
super(props);
this.state = {
isFocused: false,
};

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

onFocus() {
this.setState({isFocused: true});
}

onBlur() {
this.setState({isFocused: false});
}

handleSpaceKey(event) {
if (event.code !== 'Space') {
return;
}

this.props.onPress();
}

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}
onFocus={this.onFocus}
onBlur={this.onBlur}
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.isChecked && styles.checkedContainer,
this.props.hasError && styles.borderColorDanger,
this.props.disabled && styles.cursorDisabled,
this.state.isFocused && styles.borderColorFocus,
]}
>
<Icon src={Expensicons.Checkmark} fill="white" height={14} width={14} />
</View>
)}
</Pressable>
);
}
}

Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;
Checkbox.displayName = 'Checkbox';

export default Checkbox;
2 changes: 2 additions & 0 deletions src/components/CheckboxWithLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class CheckboxWithLabel extends React.Component {
shouldSaveDraft={this.props.shouldSaveDraft}
/>
<TouchableOpacity
focusable={false}
onPress={this.toggleCheckbox}
style={[
styles.ml3,
Expand All @@ -105,6 +106,7 @@ class CheckboxWithLabel extends React.Component {
styles.flexWrap,
styles.flexShrink1,
styles.alignItemsCenter,
styles.noSelect,
]}
>
{this.props.label && (
Expand Down
8 changes: 4 additions & 4 deletions src/components/TextInput/BaseTextInput.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'underscore';
import React, {Component} from 'react';
import {
Animated, View, TouchableWithoutFeedback, Pressable, AppState, Keyboard,
Animated, View, TouchableWithoutFeedback, AppState, Keyboard,
} from 'react-native';
import Str from 'expensify-common/lib/str';
import RNTextInput from '../RNTextInput';
Expand All @@ -14,6 +14,7 @@ import * as Expensicons from '../Icon/Expensicons';
import Text from '../Text';
import * as styleConst from './styleConst';
import * as StyleUtils from '../../styles/StyleUtils';
import Checkbox from '../Checkbox';
import getSecureEntryKeyboardType from '../../libs/getSecureEntryKeyboardType';
import CONST from '../../CONST';

Expand Down Expand Up @@ -280,16 +281,15 @@ class BaseTextInput extends Component {
keyboardType={getSecureEntryKeyboardType(this.props.keyboardType, this.props.secureTextEntry, this.state.passwordHidden)}
/>
{this.props.secureTextEntry && (
<Pressable
accessibilityRole="button"
mdneyazahmad marked this conversation as resolved.
Show resolved Hide resolved
<Checkbox
style={styles.secureInputShowPasswordButton}
onPress={this.togglePasswordVisibility}
>
<Icon
src={this.state.passwordHidden ? Expensicons.Eye : Expensicons.EyeDisabled}
fill={themeColors.icon}
/>
</Pressable>
</Checkbox>
)}
</View>
</View>
Expand Down