Skip to content

Commit

Permalink
feat: Add placeholder support for Windows (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
christophpurrer committed Mar 15, 2022
1 parent e996f62 commit 16cdaee
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 1 deletion.
11 changes: 11 additions & 0 deletions example/src/App.tsx
Expand Up @@ -12,6 +12,7 @@ import {

import * as PickerExamples from './PickerExample';
import * as PickerIOSExamples from './PickerIOSExample';
import * as PickerWindowsExamples from './PickerWindowsExamples';

export default function App() {
const [isRTL, setIsRTL] = React.useState(I18nManager.isRTL);
Expand Down Expand Up @@ -49,6 +50,16 @@ export default function App() {
{element.render()}
</View>
))}
{Platform.OS === 'windows' && (
<Text style={styles.heading}>PickerWindows Examples</Text>
)}
{Platform.OS === 'windows' &&
PickerWindowsExamples.examples.map((element) => (
<View style={styles.elementContainer} key={element.title}>
<Text style={styles.title}> {element.title} </Text>
{element.render()}
</View>
))}
</View>
</ScrollView>
</SafeAreaView>
Expand Down
23 changes: 23 additions & 0 deletions example/src/PickerWindowsExamples.js
@@ -0,0 +1,23 @@
import * as React from 'react';
import {Text, View, StyleSheet, Button} from 'react-native';
import {Picker} from '../../js';

const Item: any = Picker.Item;

function PlaceholderPickerExample() {
return (
<View>
<Picker placeholder="Select a value">
<Item label="hello" value="key0" />
<Item label="world" value="key1" />
</Picker>
</View>
);
}

export const examples = [
{
title: 'Picker with placeholder',
render: PlaceholderPickerExample,
},
];
6 changes: 6 additions & 0 deletions js/Picker.js
Expand Up @@ -103,6 +103,12 @@ type PickerProps = $ReadOnly<{|
* @platform android
*/
prompt?: ?string,

/**
* Placeholder string for this picker, used on Windows if no item has been selected.
* @platform windows
*/
placeholder?: ?string,

/**
* Used to locate this view in end-to-end tests.
Expand Down
2 changes: 2 additions & 0 deletions js/PickerWindows.windows.js
Expand Up @@ -31,6 +31,7 @@ type PickerWindowsProps = $ReadOnly<{|
selectedValue?: any,
enabled?: boolean,
prompt?: string,
placeholder?: ?string,
testID?: string,
onChange?: (event: IPickerChangeEvent) => void,
onValueChange?: (value: any, itemIndex: number, text: string) => void,
Expand Down Expand Up @@ -87,6 +88,7 @@ class PickerWindows extends React.Component<
enabled: this.props.enabled,
items: this.state.items,
onChange: this._onChange,
placeholder: this.props.placeholder,
selectedIndex: this.state.selectedIndex,
testID: this.props.testID,
style: [styles.pickerWindows, this.props.style, this.props.itemStyle],
Expand Down
5 changes: 5 additions & 0 deletions typings/Picker.d.ts
Expand Up @@ -98,6 +98,11 @@ export interface PickerProps<T = ItemValue> extends ViewProps {
* The string used for the accessibility label. Will be read once focused on the picker but not on change.
*/
accessibilityLabel?: string;
/**
* Placeholder string for this picker, used on Windows if no item has been selected.
* @platform windows
*/
placeholder?: string;
/**
* Called when picker is focused
* @platform android
Expand Down
8 changes: 8 additions & 0 deletions windows/ReactNativePicker/ReactPickerView.cpp
Expand Up @@ -100,6 +100,14 @@ namespace winrt::ReactNativePicker::implementation {
else if (propertyName == "items") {
RepopulateItems(propertyValue.AsArray());
}
else if (propertyName == "placeholder") {
if (propertyValue.IsNull()) {
this->ClearValue(winrt::ComboBox::PlaceholderTextProperty());
}
else {
this->PlaceholderText(to_hstring(propertyValue.AsString()));
}
}
else if (propertyName == "backgroundColor") {
auto const color = propertyValue.To<winrt::Brush>();
auto res = this->Resources();
Expand Down
2 changes: 1 addition & 1 deletion windows/ReactNativePicker/ReactPickerView.h
Expand Up @@ -20,7 +20,7 @@ namespace winrt::ReactNativePicker::implementation {
Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
bool m_updating{ false };
// FUTURE: remove when we can require RS5+
int32_t m_selectedIndex{ 0 };
int32_t m_selectedIndex{ -1 };
std::vector<winrt::hstring> m_itemValues;
xaml::Media::Brush m_comboBoxColor{ nullptr };
xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};
Expand Down
1 change: 1 addition & 0 deletions windows/ReactNativePicker/ReactPickerViewManager.cpp
Expand Up @@ -40,6 +40,7 @@ namespace winrt::ReactNativePicker::implementation {
nativeProps.Insert(L"editable", ViewManagerPropertyType::Boolean);
nativeProps.Insert(L"enabled", ViewManagerPropertyType::Boolean);
nativeProps.Insert(L"items", ViewManagerPropertyType::Array);
nativeProps.Insert(L"placeholder", ViewManagerPropertyType::String);
nativeProps.Insert(L"selectedIndex", ViewManagerPropertyType::Number);
nativeProps.Insert(L"text", ViewManagerPropertyType::String);
return nativeProps.GetView();
Expand Down

0 comments on commit 16cdaee

Please sign in to comment.