Skip to content

Feat/accessibility phase 1 #531

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

Merged
merged 22 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion demo/src/screens/componentScreens/CheckboxScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CheckboxScreen extends Component {
value2: false,
value3: true,
value4: true,
value5: false,
value5: false
};

render() {
Expand Down
2 changes: 2 additions & 0 deletions src/components/avatar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ export default class Avatar extends PureBaseComponent {
style={[this.getContainerStyle(), containerStyle]}
testID={testID}
onPress={onPress}
accessible
accessibilityLabel={'Avatar'}
accessibilityRole={onPress ? 'button' : 'image'}
{...this.extractAccessibilityProps()}
>
<View
Expand Down
24 changes: 18 additions & 6 deletions src/components/checkbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,24 @@ class Checkbox extends BaseComponent {
}

componentDidUpdate(prevProps) {
if (prevProps.value !== this.props.value) {
this.animateCheckbox(this.props.value);
const {value} = this.getThemeProps();
if (prevProps.value !== value) {
this.animateCheckbox(value);
}
}

getAccessibilityProps() {
const {accessibilityLabel, disabled, value} = this.getThemeProps();
const checkedState = value ? 'checked' : 'unchecked';

return {
accessible: true,
accessibilityLabel: accessibilityLabel ? `${accessibilityLabel} ${checkedState}` : `checkbox ${checkedState}`, //TODO: RN60 fix - label and role and convert to accessibilityActions
accessibilityRole: 'button',
accessibilityStates: disabled ? ['disabled'] : undefined
};
}

generateStyles() {
this.styles = createStyles(this.getThemeProps());
}
Expand All @@ -95,6 +108,7 @@ class Checkbox extends BaseComponent {

onPress = () => {
const {disabled} = this.getThemeProps();

if (!disabled) {
_.invoke(this.props, 'onValueChange', !this.props.value);
}
Expand All @@ -114,12 +128,10 @@ class Checkbox extends BaseComponent {
}

render() {
const {value, selectedIcon, color, iconColor, disabled, testID, ...others} = this.getThemeProps();
const accessibilityLabel = value ? 'checked' : 'unchecked';

const {selectedIcon, color, iconColor, disabled, testID, ...others} = this.getThemeProps();
return (
<TouchableOpacity
accessibilityLabel={accessibilityLabel}
{...this.getAccessibilityProps()}
activeOpacity={1}
testID={testID}
{...others}
Expand Down
53 changes: 47 additions & 6 deletions src/components/stepper/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import _ from 'lodash';
import React from 'react';
import {Text, View} from 'react-native';
import {AccessibilityInfo, Text, View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'lodash';
import createStyles from './style';
import {BaseComponent} from '../../commons';
import StepperButton from './StepperButton';
import createStyles from './style';
import {PureBaseComponent} from '../../commons';

/**
* @description: Stepper component with increase and decrease buttons
* @gif: https://media.giphy.com/media/3oFzm47bk0v4WV15O8/giphy.gif
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/FormScreen.js
*/
export default class Stepper extends BaseComponent {
export default class Stepper extends PureBaseComponent {
static displayName = 'Stepper';
static propTypes = {
/**
Expand Down Expand Up @@ -48,6 +48,47 @@ export default class Stepper extends BaseComponent {
};
}

getAccessibilityProps() {
const {value} = this.state;
const {accessibilityLabel} = this.props;
const labelSuffix = `value = ${value}`;
return {
accessibilityLabel: accessibilityLabel ? `${accessibilityLabel}, ${labelSuffix}` : `Stepper, ${labelSuffix}`,
accessible: true,
accessibilityRole: 'adjustable',
accessibilityActions: ['decrement', 'increment'],
onAccessibilityAction: this.onAccessibilityAction
};
}

onAccessibilityAction = event => {
const {value} = this.state;
const {min, max} = this.props;
const eventMsgContext = event.nativeEvent.action === 'decrement' ? 'Minimum' : 'Maximum';
const stepperLimitMsg = `${eventMsgContext} stepper value, ${value}, reached`;

switch (event.nativeEvent.action) {
case 'decrement':
if (value <= min) {
AccessibilityInfo.announceForAccessibility(`${stepperLimitMsg}`);
} else {
this.updateValue(value - 1);
AccessibilityInfo.announceForAccessibility(value - 1);
}
break;
case 'increment':
if (value >= max) {
AccessibilityInfo.announceForAccessibility(`${stepperLimitMsg}`);
} else {
this.updateValue(value + 1);
AccessibilityInfo.announceForAccessibility(value + 1);
}
break;
default:
break;
}
};

generateStyles() {
this.styles = createStyles(this.props.size);
}
Expand Down Expand Up @@ -80,7 +121,7 @@ export default class Stepper extends BaseComponent {
render() {
const {minusDisabled, plusDisabled, testID} = this.getDisabledState();
return (
<View style={[this.styles.container, this.props.containerStyle]}>
<View {...this.getAccessibilityProps()} style={[this.styles.container, this.props.containerStyle]}>
<View style={this.styles.title}>
<Text testID={`${testID}.label`} style={this.styles.titleText}>
{this.getLabel()}
Expand Down
24 changes: 18 additions & 6 deletions src/components/switch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,25 @@ class Switch extends BaseComponent {
this.styles = createStyles(this.getThemeProps());
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.toggle(nextProps.value);
componentDidUpdate(prevProps) {
const {value} = this.getThemeProps();
if (prevProps.value !== value) {
this.toggle(value);
}
}

getAccessibilityProps() {
const {accessibilityLabel, disabled, value} = this.getThemeProps();
const switchState = value ? 'on' : 'off';

return {
accessible: true,
accessibilityLabel: accessibilityLabel ? `${accessibilityLabel} ${switchState}` : `switch ${switchState}`, //TODO: RN60 fix label and role and convert to accessibilityActions
accessibilityRole: 'button',
accessibilityStates: disabled ? ['disabled'] : undefined
};
}

toggle(value) {
const {thumbPosition} = this.state;

Expand Down Expand Up @@ -140,12 +153,11 @@ class Switch extends BaseComponent {
}

render() {
const {value, ...others} = this.getThemeProps();
const accessibilityLabel = value ? 'switchOn' : 'switchOff';
const {...others} = this.getThemeProps();

return (
<TouchableOpacity
accessibilityLabel={accessibilityLabel}
{...this.getAccessibilityProps()}
activeOpacity={1}
{...others}
style={this.getSwitchStyle()}
Expand Down