Skip to content

Commit fdbea99

Browse files
chriscohoatpeacechen
authored andcommitted
Added accessibility labels (d-a-n#49)
* Add accessibility props
1 parent f995108 commit fdbea99

File tree

4 files changed

+1298
-47
lines changed

4 files changed

+1298
-47
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SampleApp extends Component {
4040
{ key: index++, section: true, label: 'Fruits' },
4141
{ key: index++, label: 'Red Apples' },
4242
{ key: index++, label: 'Cherries' },
43-
{ key: index++, label: 'Cranberries' },
43+
{ key: index++, label: 'Cranberries', accessibilityLabel: 'Tap here for cranberries' },
4444
// etc...
4545
// Can also add additional custom keys which are passed to the onChange callback
4646
{ key: index++, label: 'Vegetable', customKey: 'Not a fruit' }
@@ -60,6 +60,9 @@ class SampleApp extends Component {
6060
data={data}
6161
initValue="Select something yummy!"
6262
supportedOrientations={['landscape']}
63+
accessible={true}
64+
scrollViewAccessibilityLabel={'Scrollable options'}
65+
cancelButtonAccessibilityLabel={'Cancel Button'}
6366
onChange={(option)=>{ this.setState({textInputValue:option.label})}}>
6467

6568
<TextInput
@@ -100,3 +103,7 @@ Prop | Type | Optional | Default | Description
100103
`cancelTextStyle` | object | Yes | {} | style definitions for the cancel text element
101104
`cancelContainerStyle`| object | Yes | {} | style definitions for the cancel container
102105
`backdropPressToClose`| bool | Yes | false | `true` makes the modal close when the overlay is pressed
106+
`accessible`| bool | Yes | false | `true` enables accessibility for modal and options. Note: data items should have an `accessibilityLabel` property if this is enabled
107+
`scrollViewAccessibilityLabel` | string | Yes | undefined | Accessibility label for the modal ScrollView
108+
`cancelButtonAccessibilityLabel` | string | Yes | undefined | Accessibility label for the cancel button
109+

index.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,57 @@ const ViewPropTypes = RNViewPropTypes || View.propTypes;
2121
let componentIndex = 0;
2222

2323
const propTypes = {
24-
data: PropTypes.array,
25-
onChange: PropTypes.func,
26-
initValue: PropTypes.string,
27-
animationType: Modal.propTypes.animationType,
28-
style: ViewPropTypes.style,
29-
selectStyle: ViewPropTypes.style,
30-
selectTextStyle: Text.propTypes.style,
31-
optionStyle: ViewPropTypes.style,
32-
optionTextStyle: Text.propTypes.style,
33-
optionContainerStyle: ViewPropTypes.style,
34-
sectionStyle: ViewPropTypes.style,
35-
sectionTextStyle: Text.propTypes.style,
36-
cancelContainerStyle: ViewPropTypes.style,
37-
cancelStyle: ViewPropTypes.style,
38-
cancelTextStyle: Text.propTypes.style,
39-
overlayStyle: ViewPropTypes.style,
40-
cancelText: PropTypes.string,
41-
disabled: PropTypes.bool,
42-
supportedOrientations: Modal.propTypes.supportedOrientations,
43-
keyboardShouldPersistTaps: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
44-
backdropPressToClose: PropTypes.bool,
24+
data: PropTypes.array,
25+
onChange: PropTypes.func,
26+
initValue: PropTypes.string,
27+
animationType: Modal.propTypes.animationType,
28+
style: ViewPropTypes.style,
29+
selectStyle: ViewPropTypes.style,
30+
selectTextStyle: Text.propTypes.style,
31+
optionStyle: ViewPropTypes.style,
32+
optionTextStyle: Text.propTypes.style,
33+
optionContainerStyle: ViewPropTypes.style,
34+
sectionStyle: ViewPropTypes.style,
35+
sectionTextStyle: Text.propTypes.style,
36+
cancelContainerStyle: ViewPropTypes.style,
37+
cancelStyle: ViewPropTypes.style,
38+
cancelTextStyle: Text.propTypes.style,
39+
overlayStyle: ViewPropTypes.style,
40+
cancelText: PropTypes.string,
41+
disabled: PropTypes.bool,
42+
supportedOrientations: Modal.propTypes.supportedOrientations,
43+
keyboardShouldPersistTaps: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
44+
backdropPressToClose: PropTypes.bool,
45+
accessible: PropTypes.bool,
46+
scrollViewAccessibilityLabel: PropTypes.string,
47+
cancelButtonAccessibilityLabel: PropTypes.string,
4548
};
4649

4750
const defaultProps = {
48-
data: [],
49-
onChange: () => {},
50-
initValue: 'Select me!',
51-
animationType: 'slide',
52-
style: {},
53-
selectStyle: {},
54-
selectTextStyle: {},
55-
optionStyle: {},
56-
optionTextStyle: {},
57-
optionContainerStyle: {},
58-
sectionStyle: {},
59-
sectionTextStyle: {},
60-
cancelContainerStyle: {},
61-
cancelStyle: {},
62-
cancelTextStyle: {},
63-
overlayStyle: {},
64-
cancelText: 'cancel',
65-
disabled: false,
66-
supportedOrientations: ['portrait', 'landscape'],
67-
keyboardShouldPersistTaps: 'always',
68-
backdropPressToClose: false,
51+
data: [],
52+
onChange: () => {},
53+
initValue: 'Select me!',
54+
animationType: 'slide',
55+
style: {},
56+
selectStyle: {},
57+
selectTextStyle: {},
58+
optionStyle: {},
59+
optionTextStyle: {},
60+
optionContainerStyle: {},
61+
sectionStyle: {},
62+
sectionTextStyle: {},
63+
cancelContainerStyle: {},
64+
cancelStyle: {},
65+
cancelTextStyle: {},
66+
overlayStyle: {},
67+
cancelText: 'cancel',
68+
disabled: false,
69+
supportedOrientations: ['portrait', 'landscape'],
70+
keyboardShouldPersistTaps: 'always',
71+
backdropPressToClose: false,
72+
accessible: false,
73+
scrollViewAccessibilityLabel: undefined,
74+
cancelButtonAccessibilityLabel: undefined,
6975
};
7076

7177
export default class ModalSelector extends React.Component {
@@ -119,7 +125,7 @@ export default class ModalSelector extends React.Component {
119125

120126
renderOption = (option, isLastItem) => {
121127
return (
122-
<TouchableOpacity key={option.key} onPress={() => this.onChange(option)}>
128+
<TouchableOpacity key={option.key} onPress={() => this.onChange(option)} accessible={this.props.accessible} accessibilityLabel={option.accessibilityLabel || undefined}>
123129
<View style={[styles.optionStyle, this.props.optionStyle, isLastItem &&
124130
{borderBottomWidth: 0}]}>
125131
<Text style={[styles.optionTextStyle,this.props.optionTextStyle]}>{option.label}</Text>
@@ -144,14 +150,14 @@ export default class ModalSelector extends React.Component {
144150
}}>
145151
<View style={[styles.overlayStyle, this.props.overlayStyle]}>
146152
<View style={[styles.optionContainer, this.props.optionContainerStyle]}>
147-
<ScrollView keyboardShouldPersistTaps={this.props.keyboardShouldPersistTaps}>
153+
<ScrollView keyboardShouldPersistTaps={this.props.keyboardShouldPersistTaps} accessible={this.props.accessible} accessibilityLabel={this.props.scrollViewAccessibilityLabel}>
148154
<View style={{paddingHorizontal: 10}}>
149155
{options}
150156
</View>
151157
</ScrollView>
152158
</View>
153159
<View style={[styles.cancelContainer, this.props.cancelContainerStyle]}>
154-
<TouchableOpacity onPress={this.close}>
160+
<TouchableOpacity onPress={this.close} accessible={this.props.accessible} accessibilityLabel={this.props.cancelButtonAccessibilityLabel}>
155161
<View style={[styles.cancelStyle, this.props.cancelStyle]}>
156162
<Text style={[styles.cancelTextStyle,this.props.cancelTextStyle]}>{this.props.cancelText}</Text>
157163
</View>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-modal-selector",
3-
"version": "0.0.24",
3+
"version": "0.0.25",
44
"description": "A cross-platform (iOS / Android), selector/picker component for React Native that is highly customizable and supports sections.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)