Skip to content

Commit 363baa8

Browse files
authored
fix: Focus and ref fixes (#284)
1 parent 2e84701 commit 363baa8

File tree

3 files changed

+66
-52
lines changed

3 files changed

+66
-52
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
| @react-native-picker/picker | react-native |
1616
| --- | --- |
17+
| >= 1.16.0 | 0.61+ |
1718
| >= 1.2.0 | 0.60+ or 0.59+ with [Jetifier](https://www.npmjs.com/package/jetifier) |
1819
| >= 1.0.0 | 0.57 |
1920

@@ -146,7 +147,7 @@ Add `Picker` like this:
146147
</Picker>
147148
```
148149

149-
If you want to open/close picker programmatically on android, pass ref to `Picker`:
150+
If you want to open/close picker programmatically on android (available from version 1.16.0+), pass ref to `Picker`:
150151

151152
```javascript
152153
const pickerRef = useRef();
@@ -304,11 +305,11 @@ such that the total number of lines does not exceed this number. Default is '1'
304305

305306
## Methods
306307

307-
### `blur` (Android only)
308+
### `blur` (Android only, lib version 1.16.0+)
308309

309310
Programmatically closes picker
310311

311-
### `focus` (Android only)
312+
### `focus` (Android only, lib version 1.16.0+)
312313

313314
Programmatically opens picker
314315

js/PickerAndroid.android.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
import AndroidDialogPickerNativeComponent from './AndroidDialogPickerNativeComponent';
2121
import AndroidDropdownPickerNativeComponent from './AndroidDropdownPickerNativeComponent';
2222

23-
const REF_PICKER = 'picker';
2423
const MODE_DROPDOWN = 'dropdown';
2524

2625
import type {TextStyleProp} from 'StyleSheet';
@@ -133,17 +132,16 @@ function PickerAndroid(props: PickerAndroidProps, ref: PickerRef): React.Node {
133132
onValueChange(null, position);
134133
}
135134
}
136-
const {current} = pickerRef;
137135

138136
// The picker is a controlled component. This means we expect the
139137
// on*Change handlers to be in charge of updating our
140138
// `selectedValue` prop. That way they can also
141139
// disallow/undo/mutate the selection of certain values. In other
142140
// words, the embedder of this component should be the source of
143141
// truth, not the native component.
144-
if (current[REF_PICKER] && selected !== position) {
142+
if (pickerRef.current && selected !== position) {
145143
// TODO: using setNativeProps is deprecated and will be unsupported once Fabric lands. Use codegen to generate native commands
146-
current[REF_PICKER].setNativeProps({
144+
pickerRef.current.setNativeProps({
147145
selected,
148146
});
149147
}
@@ -164,7 +162,6 @@ function PickerAndroid(props: PickerAndroidProps, ref: PickerRef): React.Node {
164162
onFocus: props.onFocus,
165163
onSelect,
166164
prompt: props.prompt,
167-
ref: pickerRef,
168165
selected,
169166
style: props.style,
170167
backgroundColor: props.backgroundColor,
@@ -173,7 +170,7 @@ function PickerAndroid(props: PickerAndroidProps, ref: PickerRef): React.Node {
173170
numberOfLines: props.numberOfLines,
174171
};
175172

176-
return <Picker ref={REF_PICKER} {...rootProps} />;
173+
return <Picker ref={pickerRef} {...rootProps} />;
177174
}
178175

179176
export default React.forwardRef<PickerAndroidProps>(PickerAndroid);

typings/Picker.d.ts

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ import { TextStyle, StyleProp, ViewProps } from 'react-native'
44
export type ItemValue = number | string
55

66
export interface PickerItemProps<T = ItemValue> {
7-
label?: string;
7+
label?: string;
88
value?: T;
99
color?: string;
1010
fontFamily?: string,
11-
testID?: string;
12-
/**
13-
* Style to apply to individual item labels.
14-
* Only following values take effect:
15-
* - 'color'
16-
* - 'backgroundColor'
17-
* - 'fontSize'
18-
* - 'fontFamily'
19-
*
20-
* @platform android
21-
*/
22-
style?: StyleProp<TextStyle>
23-
/**
24-
* If set to false, the specific item will be disabled, i.e. the user will not be able to make a
25-
* selection.
26-
* @default true
27-
* @platform android
28-
*/
29-
enabled?:boolean
11+
testID?: string;
12+
/**
13+
* Style to apply to individual item labels.
14+
* Only following values take effect:
15+
* - 'color'
16+
* - 'backgroundColor'
17+
* - 'fontSize'
18+
* - 'fontFamily'
19+
*
20+
* @platform android
21+
*/
22+
style?: StyleProp<TextStyle>
23+
/**
24+
* If set to false, the specific item will be disabled, i.e. the user will not be able to make a
25+
* selection.
26+
* @default true
27+
* @platform android
28+
*/
29+
enabled?:boolean
3030
}
3131

3232
export interface PickerProps<T = ItemValue> extends ViewProps {
@@ -69,38 +69,54 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
6969
/**
7070
* Used to locate this view in end-to-end tests.
7171
*/
72-
testID?: string;
72+
testID?: string;
73+
/**
74+
* Color of arrow for spinner dropdown in hexadecimal format
75+
* @platform android
76+
*/
77+
dropdownIconColor?: string;
78+
/**
79+
* On Android, used to truncate the text with an ellipsis after computing the text layout, including line wrapping,
80+
* such that the total number of lines does not exceed this number. Default is '1'
81+
* @platform android
82+
*/
83+
numberOfLines?: number;
84+
/**
85+
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
86+
*/
87+
accessibilityLabel?: string;
7388
/**
74-
* Color of arrow for spinner dropdown in hexadecimal format
89+
* Called when picker is focused
7590
* @platform android
7691
*/
77-
dropdownIconColor?: string;
78-
79-
/**
80-
* On Android, used to truncate the text with an ellipsis after computing the text layout, including line wrapping,
81-
* such that the total number of lines does not exceed this number. Default is '1'
82-
* @platform android
83-
*/
84-
numberOfLines?: number;
85-
86-
/**
87-
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
88-
*/
89-
accessibilityLabel?: string;
90-
92+
onFocus?: (e: NativeSyntheticEvent<TargetedEvent>) => void
93+
/**
94+
* Called when picker is blurred
95+
* @platform android
96+
*/
97+
onBlur?: (e: NativeSyntheticEvent<TargetedEvent>) => void
9198
}
9299

93100
declare class Picker<T> extends React.Component<PickerProps<T>, {}> {
94101
/**
95102
* On Android, display the options in a dialog (this is the default).
96103
*/
97-
static readonly MODE_DIALOG: 'dialog';
98-
/**
99-
* On Android, display the options in a dropdown.
100-
*/
101-
static readonly MODE_DROPDOWN: 'dropdown';
104+
static readonly MODE_DIALOG: 'dialog';
105+
/**
106+
* On Android, display the options in a dropdown.
107+
*/
108+
static readonly MODE_DROPDOWN: 'dropdown';
109+
110+
static Item: React.ComponentType<PickerItemProps<ItemValue>>;
102111

103-
static Item: React.ComponentType<PickerItemProps<ItemValue>>;
112+
/**
113+
* @platform android
114+
*/
115+
focus: () => void
116+
/**
117+
* @platform android
118+
*/
119+
blur: () => void
104120
}
105121

106-
export {Picker};
122+
export { Picker };

0 commit comments

Comments
 (0)