Skip to content

Commit

Permalink
Merge pull request #69 from mcabs3/picker-ref
Browse files Browse the repository at this point in the history
added ref support for picker/datepicker components
  • Loading branch information
thodubois committed Oct 25, 2021
2 parents 5e9e26d + c35fe55 commit 6e2bb09
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 351 deletions.
14 changes: 13 additions & 1 deletion example/ExampleApp/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type {PickerItem} from 'react-native-woodpicker';
import React, {useState} from 'react';
import {Platform, StyleSheet, Text, View, Button} from 'react-native';

import {DatePicker, Picker} from 'react-native-woodpicker';
import {
DatePicker,
Picker,
PickerInstance,
DatePickerInstance,
} from 'react-native-woodpicker';

const data: Array<PickerItem> = [
{label: 'DataCat', value: 1},
Expand All @@ -17,6 +22,8 @@ const App = (): JSX.Element => {
const [pickedDate, setPickedDate] = useState<Date | null>(null);
const [pickedData, setPickedData] = useState<PickerItem>();

const pickerRef = React.useRef<PickerInstance | null>(null);
const datePickerRef = React.useRef<DatePickerInstance | null>(null);
const handleDateChange = (date: Date | null) => setPickedDate(date);

const resetDate = () => setPickedDate(new Date());
Expand All @@ -29,13 +36,16 @@ const App = (): JSX.Element => {
});

const handleText = () => pickedDate?.toDateString?.() ?? 'No value Selected';
const openPickerWidthRef = () => pickerRef.current?.open();
const openDatePickerWithRef = () => datePickerRef.current?.open();

return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
<Picker
ref={pickerRef}
item={pickedData}
items={data}
onItemChange={setPickedData}
Expand All @@ -46,6 +56,7 @@ const App = (): JSX.Element => {
//androidPickerMode="dropdown"
/>
<Button title="Set Value" onPress={resetPicker} />
<Button title="Open Picker (Ref)" onPress={openPickerWidthRef} />
<DatePicker
value={pickedDate}
onDateChange={handleDateChange}
Expand All @@ -62,6 +73,7 @@ const App = (): JSX.Element => {
//locale="fr"
/>
<Button title="Set Today" onPress={resetDate} />
<Button title="Open DatePicker (Ref)" onPress={openDatePickerWithRef} />
</View>
);
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ./src"
"lint": "eslint ./src",
"postinstall": "npm run build"
},
"devDependencies": {
"@react-native-community/datetimepicker": "3.5.0",
Expand Down
67 changes: 37 additions & 30 deletions src/components/AndroidPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react";
import React, { forwardRef, Ref } from "react";
import { View, ViewStyle } from "react-native";
import { Picker as RNPicker } from "@react-native-picker/picker";
import { styles } from "../helpers/stylesHelper";
import { PickerItem } from "../types";
import { AndroidPickerInstance } from "../types/picker";

export type Props = {
selectedItem: any;
Expand All @@ -18,34 +19,40 @@ export type Props = {
customProps: { [key: string]: any };
};

const AndroidPicker = ({
selectedItem,
disabled,
title,
mode,
renderInput,
renderPickerItems,
onItemChange,
containerStyle,
customProps,
}: Props): JSX.Element => {
return (
<View style={containerStyle}>
{renderInput()}
<RNPicker
style={styles.androidPickerContainer}
prompt={title}
// @ts-ignore
onValueChange={onItemChange}
selectedValue={selectedItem.value}
mode={mode || "dialog"}
enabled={!disabled}
{...customProps}
>
{renderPickerItems()}
</RNPicker>
</View>
);
};
const AndroidPicker = forwardRef(
(
{
selectedItem,
disabled,
title,
mode,
renderInput,
renderPickerItems,
onItemChange,
containerStyle,
customProps,
}: Props,
ref: Ref<AndroidPickerInstance>
): JSX.Element => {
return (
<View style={containerStyle}>
{renderInput()}
<RNPicker
ref={ref}
style={styles.androidPickerContainer}
prompt={title}
// @ts-ignore
onValueChange={onItemChange}
selectedValue={selectedItem.value}
mode={mode || "dialog"}
enabled={!disabled}
{...customProps}
>
{renderPickerItems()}
</RNPicker>
</View>
);
}
);

export default AndroidPicker;

0 comments on commit 6e2bb09

Please sign in to comment.