Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ref support for picker/datepicker components #69

Merged
merged 8 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;