Skip to content

Commit 0a525b6

Browse files
KevinGVargasfacebook-github-bot
authored andcommitted
Support for accessibility Label prop to the Picker component (#27342)
Summary: With a Picker we would like to allow accessibility labels to be passed as a prop for situations where we want go give more detail. For example if we have a number picker that will be used for a timer instead of just saying 3, we might want to say 3 hours. ## Changelog [General] [Added] - Picker test with an accessibility label prop [General] [Added] - Support for accessibility Label prop to the Picker component Pull Request resolved: #27342 Test Plan: Test plan is testing in RNTester making sure the examples work Differential Revision: D18770184 Pulled By: hramos fbshipit-source-id: e6f8ab4a9c50f3fb46342198441ecc71394913d3
1 parent 973253a commit 0a525b6

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

Libraries/Components/Picker/Picker.js

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ type PickerProps = $ReadOnly<{|
106106
* Used to locate this view in end-to-end tests.
107107
*/
108108
testID?: ?string,
109+
/**
110+
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
111+
*/
112+
accessibilityLabel?: ?string,
109113
|}>;
110114

111115
/**

Libraries/Components/Picker/PickerIOS.ios.js

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Props = $ReadOnly<{|
4848
onChange?: ?(event: PickerIOSChangeEvent) => mixed,
4949
onValueChange?: ?(itemValue: string | number, itemIndex: number) => mixed,
5050
selectedValue: ?(number | string),
51+
accessibilityLabel?: ?string,
5152
|}>;
5253

5354
type State = {|
@@ -106,6 +107,7 @@ class PickerIOS extends React.Component<Props, State> {
106107
items={this.state.items}
107108
selectedIndex={this.state.selectedIndex}
108109
onChange={this._onChange}
110+
accessibilityLabel={this.props.accessibilityLabel}
109111
/>
110112
</View>
111113
);

Libraries/Components/Picker/RCTPickerNativeComponent.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type NativeProps = $ReadOnly<{|
3939
selectedIndex: number,
4040
style?: ?TextStyleProp,
4141
testID?: ?string,
42+
accessibilityLabel?: ?string,
4243
|}>;
4344

4445
type ComponentType = HostComponent<NativeProps>;

RNTester/js/examples/Picker/PickerExample.js

+31
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ class ColorPickerExample extends React.Component<{...}, ColorState> {
125125
);
126126
}
127127
}
128+
class AccessibilityLabelPickerExample extends React.Component<{}, State> {
129+
state: State = {
130+
value: '3',
131+
};
132+
133+
render(): React.Node {
134+
return (
135+
<Picker
136+
accessibilityLabel={this.state.value + 'Hours'}
137+
style={styles.picker}
138+
selectedValue={this.state.value}
139+
onValueChange={v => this.setState({value: v})}>
140+
<Item label="1" value="1" />
141+
<Item label="2" value="2" />
142+
<Item label="3" value="3" />
143+
</Picker>
144+
);
145+
}
146+
}
128147

129148
const styles = StyleSheet.create({
130149
picker: {
@@ -160,6 +179,12 @@ exports.examples = [
160179
return <PromptPickerExample />;
161180
},
162181
},
182+
{
183+
title: 'Accessibility Label pickers',
184+
render: function(): React.Element<typeof AccessibilityLabelPickerExample> {
185+
return <AccessibilityLabelPickerExample />;
186+
},
187+
},
163188
{
164189
title: 'Picker with no listener',
165190
render: function(): React.Element<typeof PromptPickerExample> {
@@ -186,4 +211,10 @@ exports.examples = [
186211
return <ColorPickerExample />;
187212
},
188213
},
214+
{
215+
title: 'AccessibilityLabel pickers',
216+
render: function(): React.Element<typeof AccessibilityLabelPickerExample> {
217+
return <AccessibilityLabelPickerExample />;
218+
},
219+
},
189220
];

React/Views/RCTPicker.m

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "RCTConvert.h"
1111
#import "RCTUtils.h"
1212

13-
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate>
13+
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate, UIPickerViewAccessibilityDelegate>
1414
@end
1515

1616
@implementation RCTPicker
@@ -109,4 +109,10 @@ - (void)pickerView:(__unused UIPickerView *)pickerView
109109
}
110110
}
111111

112+
#pragma mark - UIPickerViewAccessibilityDelegate protocol
113+
114+
- (NSString *)pickerView:(UIPickerView *)pickerView accessibilityLabelForComponent:(NSInteger)component{
115+
return super.accessibilityLabel;
116+
}
117+
112118
@end

React/Views/RCTPickerManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ - (UIView *)view
2323

2424
RCT_EXPORT_VIEW_PROPERTY(items, NSArray<NSDictionary *>)
2525
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
26+
RCT_REMAP_VIEW_PROPERTY(accessibilityLabel, reactAccessibilityElement.accessibilityLabel, NSString)
2627
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
2728
RCT_EXPORT_VIEW_PROPERTY(color, UIColor)
2829
RCT_EXPORT_VIEW_PROPERTY(textAlign, NSTextAlignment)

0 commit comments

Comments
 (0)