This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,69 @@
/**
* 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 strict-local
*/
'use strict';
import * as React from 'react';
import codegenNativeCommands from '../../Utilities/codegenNativeCommands';
import requireNativeComponent from '../../ReactNative/requireNativeComponent';
import type {
DirectEventHandler,
Int32,
WithDefault,
} from '../../Types/CodegenTypes';
import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
import type {ProcessedColorValue} from '../../StyleSheet/processColor';
import type {ViewProps} from '../../Components/View/ViewPropTypes';
type PickerItem = $ReadOnly<{|
label: string,
color?: ?ProcessedColorValue,
|}>;
type PickerItemSelectEvent = $ReadOnly<{|
position: Int32,
|}>;
type NativeProps = $ReadOnly<{|
...ViewProps,
style?: ?TextStyleProp,
// Props
color?: ?ColorValue,
backgroundColor?: ?ColorValue,
enabled?: WithDefault<boolean, true>,
items: $ReadOnlyArray<PickerItem>,
prompt?: WithDefault<string, ''>,
selected: Int32,
// Events
onSelect?: DirectEventHandler<PickerItemSelectEvent>,
|}>;
type NativeType = HostComponent<NativeProps>;
interface NativeCommands {
+setNativeSelectedPosition: (
viewRef: React.ElementRef<NativeType>,
index: number,
) => void;
}
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['setNativeSelectedPosition'],
});
export default (requireNativeComponent<NativeProps>(
'AndroidDialogPicker',
): NativeType);

View File

@ -0,0 +1,69 @@
/**
* 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 strict-local
*/
'use strict';
import * as React from 'react';
import codegenNativeCommands from '../../Utilities/codegenNativeCommands';
import requireNativeComponent from '../../ReactNative/requireNativeComponent';
import type {
DirectEventHandler,
Int32,
WithDefault,
} from '../../Types/CodegenTypes';
import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
import type {ProcessedColorValue} from '../../StyleSheet/processColor';
import type {ViewProps} from '../../Components/View/ViewPropTypes';
type PickerItem = $ReadOnly<{|
label: string,
color?: ?ProcessedColorValue,
|}>;
type PickerItemSelectEvent = $ReadOnly<{|
position: Int32,
|}>;
type NativeProps = $ReadOnly<{|
...ViewProps,
style?: ?TextStyleProp,
// Props
color?: ?ColorValue,
backgroundColor?: ?ColorValue,
enabled?: WithDefault<boolean, true>,
items: $ReadOnlyArray<PickerItem>,
prompt?: WithDefault<string, ''>,
selected: Int32,
// Events
onSelect?: DirectEventHandler<PickerItemSelectEvent>,
|}>;
type NativeType = HostComponent<NativeProps>;
interface NativeCommands {
+setNativeSelectedPosition: (
viewRef: React.ElementRef<NativeType>,
index: number,
) => void;
}
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['setNativeSelectedPosition'],
});
export default (requireNativeComponent<NativeProps>(
'AndroidDropdownPicker',
): NativeType);

View File

@ -0,0 +1,165 @@
/**
* 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';
const PickerAndroid = require('./PickerAndroid');
const PickerIOS = require('./PickerIOS');
const Platform = require('../../Utilities/Platform');
const React = require('react');
const UnimplementedView = require('../UnimplementedViews/UnimplementedView');
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
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. Can be a string or an integer.
*/
value?: ?(number | 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. Can be a string or an integer.
*/
selectedValue?: ?(number | 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,
|}>;
/**
* 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(>=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(>=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;

View File

@ -0,0 +1,149 @@
/**
* 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 strict-local
*/
'use strict';
import AndroidDropdownPickerNativeComponent, {
Commands as AndroidDropdownPickerCommands,
} from './AndroidDropdownPickerNativeComponent';
import AndroidDialogPickerNativeComponent, {
Commands as AndroidDialogPickerCommands,
} from './AndroidDialogPickerNativeComponent';
import * as React from 'react';
import StyleSheet from '../../StyleSheet/StyleSheet';
import invariant from 'invariant';
import processColor from '../../StyleSheet/processColor';
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
import type {SyntheticEvent} from '../../Types/CoreEventTypes';
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
type PickerItemSelectSyntheticEvent = SyntheticEvent<
$ReadOnly<{|
position: number,
|}>,
>;
type PickerItemValue = number | string;
type Props = $ReadOnly<{|
accessibilityLabel?: ?Stringish,
children?: React.Node,
style?: ?TextStyleProp,
backgroundColor?: ?ColorValue,
selectedValue?: ?PickerItemValue,
enabled?: ?boolean,
mode?: ?('dialog' | 'dropdown'),
onValueChange?: ?(itemValue: ?PickerItemValue, itemIndex: number) => mixed,
prompt?: ?string,
testID?: string,
|}>;
/**
* Not exposed as a public API - use <Picker> instead.
*/
function PickerAndroid(props: Props): React.Node {
const pickerRef = React.useRef(null);
const [items, selected] = React.useMemo(() => {
// eslint-disable-next-line no-shadow
let selected = 0;
// eslint-disable-next-line no-shadow
const items = React.Children.map(props.children, (child, index) => {
if (child === null) {
return null;
}
if (child.props.value === props.selectedValue) {
selected = index;
}
const {color, label} = child.props;
const processedColor = processColor(color);
invariant(
processedColor == null || typeof processedColor === 'number',
'Unexpected color given for PickerAndroid color prop',
);
return {
color: color == null ? null : processedColor,
label,
};
});
return [items, selected];
}, [props.children, props.selectedValue]);
const onSelect = React.useCallback(
({nativeEvent}: PickerItemSelectSyntheticEvent) => {
const {position} = nativeEvent;
const onValueChange = props.onValueChange;
if (onValueChange != null) {
if (position >= 0) {
const children = React.Children.toArray(props.children).filter(
item => item != null,
);
const value = children[position].props.value;
if (props.selectedValue !== value) {
onValueChange(value, position);
}
} else {
onValueChange(null, position);
}
}
const {current} = pickerRef;
if (current != null && position !== selected) {
const Commands =
props.mode === 'dropdown'
? AndroidDropdownPickerCommands
: AndroidDialogPickerCommands;
Commands.setNativeSelectedPosition(current, selected);
}
},
[
props.children,
props.onValueChange,
props.selectedValue,
props.mode,
selected,
],
);
const rootProps = {
accessibilityLabel: props.accessibilityLabel,
enabled: props.enabled,
items,
onSelect,
prompt: props.prompt,
ref: pickerRef,
selected,
style: StyleSheet.compose(
styles.pickerAndroid,
props.style,
),
backgroundColor: props.backgroundColor,
testID: props.testID,
};
return props.mode === 'dropdown' ? (
<AndroidDropdownPickerNativeComponent {...rootProps} />
) : (
<AndroidDialogPickerNativeComponent {...rootProps} />
);
}
const styles = StyleSheet.create({
pickerAndroid: {
// The picker will conform to whatever width is given, but we do
// have to set the component's height explicitly on the
// surrounding view to ensure it gets rendered.
// TODO would be better to export a native constant for this,
// like in iOS the RCTDatePickerManager.m
height: 50,
},
});
module.exports = PickerAndroid;

View File

@ -0,0 +1,12 @@
/**
* 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
*/
'use strict';
module.exports = require('../UnimplementedViews/UnimplementedView');

View File

@ -0,0 +1,14 @@
/**
* 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
*/
// This is a controlled component version of RCTPickerIOS.
'use strict';
module.exports = require('../UnimplementedViews/UnimplementedView');

View File

@ -0,0 +1,164 @@
/**
* 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 strict-local
*/
// This is a controlled component version of RCTPickerIOS.
'use strict';
const React = require('react');
const StyleSheet = require('../../StyleSheet/StyleSheet');
const View = require('../View/View');
const invariant = require('invariant');
const processColor = require('../../StyleSheet/processColor');
import RCTPickerNativeComponent, {
Commands as PickerCommands,
} from './RCTPickerNativeComponent';
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
import type {ColorValue} from '../../StyleSheet/StyleSheetTypes';
import type {ProcessedColorValue} from '../../StyleSheet/processColor';
import type {SyntheticEvent} from '../../Types/CoreEventTypes';
import type {ViewProps} from '../View/ViewPropTypes';
type PickerIOSChangeEvent = SyntheticEvent<
$ReadOnly<{|
newValue: number | string,
newIndex: number,
|}>,
>;
type RCTPickerIOSItemType = $ReadOnly<{|
label: ?Label,
value: ?(number | string),
textColor: ?ProcessedColorValue,
|}>;
type Label = Stringish | number;
type Props = $ReadOnly<{|
...ViewProps,
children: React.ChildrenArray<React.Element<typeof PickerIOSItem>>,
itemStyle?: ?TextStyleProp,
onChange?: ?(event: PickerIOSChangeEvent) => mixed,
onValueChange?: ?(itemValue: string | number, itemIndex: number) => mixed,
selectedValue: ?(number | string),
accessibilityLabel?: ?string,
|}>;
type State = {|
selectedIndex: number,
items: $ReadOnlyArray<RCTPickerIOSItemType>,
|};
type ItemProps = $ReadOnly<{|
label: ?Label,
value?: ?(number | string),
color?: ?ColorValue,
|}>;
const PickerIOSItem = (props: ItemProps): null => {
return null;
};
class PickerIOS extends React.Component<Props, State> {
_picker: ?React.ElementRef<typeof RCTPickerNativeComponent> = null;
_lastNativeValue: ?number;
state: State = {
selectedIndex: 0,
items: [],
};
static Item: (props: ItemProps) => null = PickerIOSItem;
static getDerivedStateFromProps(props: Props): State {
let selectedIndex = 0;
const items = [];
React.Children.toArray(props.children)
.filter(child => child !== null)
.forEach(function(child, index) {
if (child.props.value === props.selectedValue) {
selectedIndex = index;
}
const processedTextColor = processColor(child.props.color);
invariant(
processedTextColor == null || typeof processedTextColor === 'number',
'Unexpected color given for PickerIOSItem color',
);
items.push({
value: child.props.value,
label: child.props.label,
textColor: processedTextColor,
});
});
return {selectedIndex, items};
}
render(): React.Node {
return (
<View style={this.props.style}>
<RCTPickerNativeComponent
ref={picker => {
this._picker = picker;
}}
testID={this.props.testID}
style={[styles.pickerIOS, this.props.itemStyle]}
items={this.state.items}
selectedIndex={this.state.selectedIndex}
onChange={this._onChange}
accessibilityLabel={this.props.accessibilityLabel}
/>
</View>
);
}
componentDidUpdate() {
// This is necessary in case native updates the picker and JS decides
// that the update should be ignored and we should stick with the value
// that we have in JS.
if (
this._picker &&
this._lastNativeValue !== undefined &&
this._lastNativeValue !== this.state.selectedIndex
) {
PickerCommands.setNativeSelectedIndex(
this._picker,
this.state.selectedIndex,
);
}
}
_onChange = event => {
if (this.props.onChange) {
this.props.onChange(event);
}
if (this.props.onValueChange) {
this.props.onValueChange(
event.nativeEvent.newValue,
event.nativeEvent.newIndex,
);
}
this._lastNativeValue = event.nativeEvent.newIndex;
this.forceUpdate();
};
}
const styles = StyleSheet.create({
pickerIOS: {
// The picker will conform to whatever width is given, but we do
// have to set the component's height explicitly on the
// surrounding view to ensure it gets rendered.
height: 216,
},
});
module.exports = PickerIOS;

View File

@ -0,0 +1,63 @@
/**
* 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.
*
* @flow strict-local
* @format
*/
'use strict';
const requireNativeComponent = require('../../ReactNative/requireNativeComponent');
import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
import type {SyntheticEvent} from '../../Types/CoreEventTypes';
import type {TextStyleProp} from '../../StyleSheet/StyleSheet';
import type {ProcessedColorValue} from '../../StyleSheet/processColor';
import codegenNativeCommands from '../../Utilities/codegenNativeCommands';
import * as React from 'react';
type PickerIOSChangeEvent = SyntheticEvent<
$ReadOnly<{|
newValue: number | string,
newIndex: number,
|}>,
>;
type RCTPickerIOSItemType = $ReadOnly<{|
label: ?Label,
value: ?(number | string),
textColor: ?ProcessedColorValue,
|}>;
type Label = Stringish | number;
type NativeProps = $ReadOnly<{|
items: $ReadOnlyArray<RCTPickerIOSItemType>,
onChange: (event: PickerIOSChangeEvent) => void,
selectedIndex: number,
style?: ?TextStyleProp,
testID?: ?string,
accessibilityLabel?: ?string,
|}>;
type ComponentType = HostComponent<NativeProps>;
interface NativeCommands {
+setNativeSelectedIndex: (
viewRef: React.ElementRef<ComponentType>,
index: number,
) => void;
}
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['setNativeSelectedIndex'],
});
const RCTPickerNativeComponent: ComponentType = requireNativeComponent<NativeProps>(
'RCTPicker',
);
export default RCTPickerNativeComponent;