Skip to content

Commit 16cdaee

Browse files
feat: Add placeholder support for Windows (#386)
1 parent e996f62 commit 16cdaee

File tree

8 files changed

+57
-1
lines changed

8 files changed

+57
-1
lines changed

example/src/App.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
import * as PickerExamples from './PickerExample';
1414
import * as PickerIOSExamples from './PickerIOSExample';
15+
import * as PickerWindowsExamples from './PickerWindowsExamples';
1516

1617
export default function App() {
1718
const [isRTL, setIsRTL] = React.useState(I18nManager.isRTL);
@@ -49,6 +50,16 @@ export default function App() {
4950
{element.render()}
5051
</View>
5152
))}
53+
{Platform.OS === 'windows' && (
54+
<Text style={styles.heading}>PickerWindows Examples</Text>
55+
)}
56+
{Platform.OS === 'windows' &&
57+
PickerWindowsExamples.examples.map((element) => (
58+
<View style={styles.elementContainer} key={element.title}>
59+
<Text style={styles.title}> {element.title} </Text>
60+
{element.render()}
61+
</View>
62+
))}
5263
</View>
5364
</ScrollView>
5465
</SafeAreaView>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
import {Text, View, StyleSheet, Button} from 'react-native';
3+
import {Picker} from '../../js';
4+
5+
const Item: any = Picker.Item;
6+
7+
function PlaceholderPickerExample() {
8+
return (
9+
<View>
10+
<Picker placeholder="Select a value">
11+
<Item label="hello" value="key0" />
12+
<Item label="world" value="key1" />
13+
</Picker>
14+
</View>
15+
);
16+
}
17+
18+
export const examples = [
19+
{
20+
title: 'Picker with placeholder',
21+
render: PlaceholderPickerExample,
22+
},
23+
];

js/Picker.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ type PickerProps = $ReadOnly<{|
103103
* @platform android
104104
*/
105105
prompt?: ?string,
106+
107+
/**
108+
* Placeholder string for this picker, used on Windows if no item has been selected.
109+
* @platform windows
110+
*/
111+
placeholder?: ?string,
106112

107113
/**
108114
* Used to locate this view in end-to-end tests.

js/PickerWindows.windows.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type PickerWindowsProps = $ReadOnly<{|
3131
selectedValue?: any,
3232
enabled?: boolean,
3333
prompt?: string,
34+
placeholder?: ?string,
3435
testID?: string,
3536
onChange?: (event: IPickerChangeEvent) => void,
3637
onValueChange?: (value: any, itemIndex: number, text: string) => void,
@@ -87,6 +88,7 @@ class PickerWindows extends React.Component<
8788
enabled: this.props.enabled,
8889
items: this.state.items,
8990
onChange: this._onChange,
91+
placeholder: this.props.placeholder,
9092
selectedIndex: this.state.selectedIndex,
9193
testID: this.props.testID,
9294
style: [styles.pickerWindows, this.props.style, this.props.itemStyle],

typings/Picker.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
9898
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
9999
*/
100100
accessibilityLabel?: string;
101+
/**
102+
* Placeholder string for this picker, used on Windows if no item has been selected.
103+
* @platform windows
104+
*/
105+
placeholder?: string;
101106
/**
102107
* Called when picker is focused
103108
* @platform android

windows/ReactNativePicker/ReactPickerView.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ namespace winrt::ReactNativePicker::implementation {
100100
else if (propertyName == "items") {
101101
RepopulateItems(propertyValue.AsArray());
102102
}
103+
else if (propertyName == "placeholder") {
104+
if (propertyValue.IsNull()) {
105+
this->ClearValue(winrt::ComboBox::PlaceholderTextProperty());
106+
}
107+
else {
108+
this->PlaceholderText(to_hstring(propertyValue.AsString()));
109+
}
110+
}
103111
else if (propertyName == "backgroundColor") {
104112
auto const color = propertyValue.To<winrt::Brush>();
105113
auto res = this->Resources();

windows/ReactNativePicker/ReactPickerView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace winrt::ReactNativePicker::implementation {
2020
Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
2121
bool m_updating{ false };
2222
// FUTURE: remove when we can require RS5+
23-
int32_t m_selectedIndex{ 0 };
23+
int32_t m_selectedIndex{ -1 };
2424
std::vector<winrt::hstring> m_itemValues;
2525
xaml::Media::Brush m_comboBoxColor{ nullptr };
2626
xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};

windows/ReactNativePicker/ReactPickerViewManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace winrt::ReactNativePicker::implementation {
4040
nativeProps.Insert(L"editable", ViewManagerPropertyType::Boolean);
4141
nativeProps.Insert(L"enabled", ViewManagerPropertyType::Boolean);
4242
nativeProps.Insert(L"items", ViewManagerPropertyType::Array);
43+
nativeProps.Insert(L"placeholder", ViewManagerPropertyType::String);
4344
nativeProps.Insert(L"selectedIndex", ViewManagerPropertyType::Number);
4445
nativeProps.Insert(L"text", ViewManagerPropertyType::String);
4546
return nativeProps.GetView();

0 commit comments

Comments
 (0)