react-native-datepicker icon indicating copy to clipboard operation
react-native-datepicker copied to clipboard

How to open date picker manually

Open deepupathak opened this issue 7 years ago • 8 comments

Issue

Hi, I am unable to open date picker manually. Please provide solution for this problem. I am sharing code snippet below.

Code

openDatePicker(date, mode, format, index) {
   this.setState({ datePickerDate: date, datePickerMode: mode, datePickerFormat: format });
   DatePicker.onPressDate;
}

and I am calling this method (openDatePicker) on row click.

Environment

  1. react-native -v: 0.47.1
  2. node -v:6.2.1
  3. npm -v:4.2.0
  4. target platform: Android
  5. operating system: windows 10 64 bit

deepupathak avatar Dec 19 '17 16:12 deepupathak

the above solution doesn't work. I'm running into this problem too. I'd like to display the date picker from my custom button. How do you show/hide the date picker on a specific event?

bslayerw avatar Jan 29 '18 17:01 bslayerw

use: <DatePicker showIcon={false} hideText={true} ref={(ref)=>this.datePickerRef=ref} ... />

and from your element: onPress={() => this.datePickerRef.onPressDate()

EyalSi avatar Feb 04 '18 21:02 EyalSi

EyalSi's method worked for me.

JoelTrottier avatar Mar 06 '18 22:03 JoelTrottier

Here is how you can open it manually with hooks:

import { useRef } from 'react';

// ...

const datePickerRef = useRef(null);

// ...

<SomeComponent onPress={() => datePickerRef.current.onPressDate()} />

// ...

<DatePicker showIcon={false} hideText={true} ref={datePickerRef} />

evrimfeyyaz avatar Jul 24 '19 05:07 evrimfeyyaz

Here is how you can open it manually with hooks:

import { useRef } from 'react';

// ...

const datePickerRef = useRef(null);

// ...

<SomeComponent onPress={() => datePickerRef.current.onPressDate()} />

// ...

<DatePicker showIcon={false} hideText={true} ref={datePickerRef} />

Thanks a lot, clean solution.

kasim444 avatar Nov 19 '19 09:11 kasim444

@EyalSi I did exactly that and I get an error saying "this.ref.onPressDate is not a function...

jasuno avatar May 26 '20 21:05 jasuno

Here is how you can open it manually with hooks:

import { useRef } from 'react';

// ...

const datePickerRef = useRef(null);

// ...

<SomeComponent onPress={() => datePickerRef.current.onPressDate()} />

// ...

<DatePicker showIcon={false} hideText={true} ref={datePickerRef} />

@EyalSi I'm using typescript. what should be the typing of ref? Any suggestion?

aliwaqar981 avatar Mar 28 '21 06:03 aliwaqar981

@aliwaqar981, you can import DatePicker and use it as the type as const datePickerRe = useRef<DatePicker>(null)

CryceTruly avatar Aug 14 '21 10:08 CryceTruly