Files
react-native/Libraries/Components/Picker/Picker.js
T
Dennis Urtubia 44717152ca Adds accessiblity actions on core components (#31532)
Summary:
Android: Adding custom actions (https://github.com/facebook/react-native/issues/30854).
Adds accessiblity actions on core components (Button, TextInput, Text, and Picker).

## Changelog
[General] [Added] - Adds accessiblity actions on core components

Pull Request resolved: https://github.com/facebook/react-native/pull/31532

Test Plan:
- `npm test`
- Rendering of components on `RNTesterApp` using talkback:
    - Check if accessibility actions were available;
    ![image](https://user-images.githubusercontent.com/33161939/118381843-a668c180-b5c5-11eb-9ce4-016a49157dc5.png)
    - Trigger `activate` action for all components;
    ![image](https://user-images.githubusercontent.com/33161939/118381736-7bca3900-b5c4-11eb-82fb-32e824e1b38c.png)

## Notes
- For `TextInput` an unexpected error is raised:
![image](https://user-images.githubusercontent.com/33161939/118381603-d1054b00-b5c2-11eb-93f2-1d5730ee2d24.png)

Reviewed By: kacieb

Differential Revision: D28654294

Pulled By: lunaleaps

fbshipit-source-id: 80dd3f3c7aa27bbaf16ef12997e8f55952a02eb2
2021-05-26 16:49:11 -07:00

195 lines
5.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import * as React from 'react';
import PickerAndroid from './PickerAndroid';
import PickerIOS from './PickerIOS';
import Platform from '../../Utilities/Platform';
import UnimplementedView from '../UnimplementedViews/UnimplementedView';
import type {TextStyleProp, ColorValue} from '../../StyleSheet/StyleSheet';
import type {
AccessibilityActionEvent,
AccessibilityActionInfo,
} from '../View/ViewAccessibility';
const MODE_DIALOG = 'dialog';
const MODE_DROPDOWN = 'dropdown';
type PickerItemProps = $ReadOnly<{|
/**
* Text to display for this item.
*/
label: string,
/**
* The value to be passed to picker's `onValueChange` callback when
* this item is selected.
*/
value?: ?string,
/**
* Color of this item's text.
* @platform android
*/
color?: ColorValue,
/**
* Used to locate the item in end-to-end tests.
*/
testID?: string,
|}>;
/**
* Individual selectable item in a Picker.
*/
export type {PickerItem};
class PickerItem extends React.Component<PickerItemProps> {
render() {
// The items are not rendered directly
throw null;
}
}
type PickerProps = $ReadOnly<{|
children?: React.Node,
style?: ?TextStyleProp,
/**
* Value matching value of one of the items.
*/
selectedValue?: ?string,
/**
* Callback for when an item is selected. This is called with the following parameters:
* - `itemValue`: the `value` prop of the item that was selected
* - `itemIndex`: the index of the selected item in this picker
*/
onValueChange?: ?(itemValue: string | number, itemIndex: number) => mixed,
/**
* If set to false, the picker will be disabled, i.e. the user will not be able to make a
* selection.
* @platform android
*/
enabled?: ?boolean,
/**
* On Android, specifies how to display the selection items when the user taps on the picker:
*
* - 'dialog': Show a modal dialog. This is the default.
* - 'dropdown': Shows a dropdown anchored to the picker view
*
* @platform android
*/
mode?: ?('dialog' | 'dropdown'),
/**
* Style to apply to each of the item labels.
* @platform ios
*/
itemStyle?: ?TextStyleProp,
/**
* Color of the item background.
* @platform android
*/
backgroundColor?: ColorValue,
/**
* Prompt string for this picker, used on Android in dialog mode as the title of the dialog.
* @platform android
*/
prompt?: ?string,
/**
* Used to locate this view in end-to-end tests.
*/
testID?: ?string,
/**
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
*/
accessibilityLabel?: ?string,
/**
* When `true`, indicates that the view is an accessibility element.
* By default, all the touchable elements are accessible.
*
* See https://reactnative.dev/docs/view.html#accessible
*/
accessible?: ?boolean,
/**
* Provides an array of custom actions available for accessibility.
*
*/
accessibilityActions?: ?$ReadOnlyArray<AccessibilityActionInfo>,
/**
* When `accessible` is true, the system will try to invoke this function
* when the user performs an accessibility custom action.
*
*/
onAccessibilityAction?: ?(event: AccessibilityActionEvent) => mixed,
|}>;
/**
* Renders the native picker component on iOS and Android. Example:
*
* <Picker
* selectedValue={this.state.language}
* onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
* <Picker.Item label="Java" value="java" />
* <Picker.Item label="JavaScript" value="js" />
* </Picker>
*/
class Picker extends React.Component<PickerProps> {
/**
* On Android, display the options in a dialog.
*/
static MODE_DIALOG: $TEMPORARY$string<'dialog'> = MODE_DIALOG;
/**
* On Android, display the options in a dropdown (this is the default).
*/
static MODE_DROPDOWN: $TEMPORARY$string<'dropdown'> = MODE_DROPDOWN;
static Item: typeof PickerItem = PickerItem;
static defaultProps: {|mode: $TEMPORARY$string<'dialog'>|} = {
mode: MODE_DIALOG,
};
render(): React.Node {
if (Platform.OS === 'ios') {
/* $FlowFixMe[prop-missing] (>=0.81.0 site=react_native_ios_fb) This
* suppression was added when renaming suppression sites. */
/* $FlowFixMe[incompatible-type] (>=0.81.0 site=react_native_ios_fb) This
* suppression was added when renaming suppression sites. */
return <PickerIOS {...this.props}>{this.props.children}</PickerIOS>;
} else if (Platform.OS === 'android') {
return (
/* $FlowFixMe[incompatible-type] (>=0.81.0 site=react_native_android_fb) This
* suppression was added when renaming suppression sites. */
/* $FlowFixMe[prop-missing] (>=0.81.0 site=react_native_android_fb) This
* suppression was added when renaming suppression sites. */
<PickerAndroid {...this.props}>{this.props.children}</PickerAndroid>
);
} else {
return <UnimplementedView />;
}
}
}
module.exports = Picker;