Datetimepicker: on Cancel

Created on 8 Jan 2020  路  12Comments  路  Source: react-native-datetimepicker/datetimepicker

Bug

{show && mode={'date'}
minimumDate={new Date("2020-01-06")}
is24Hour={true}
display="default"
onChange={this.setDate}
/>
}

setDate = (event, date) => {
this.setState({
pickdate: moment.utc(date).format('MM/DD/YYYY'),
show: Platform.OS === 'ios' ? true : false,
})
}

library version :- "@react-native-community/datetimepicker": "^2.1.0",

on click cancel also this.setDate method is calling because of this on cancel click also date is selecting...
is there any different callbacks for ok and cancel ?

Most helpful comment

i had the same problem the props onCancel not found but i fix my problem by small trick check if date passed trough setDate if undefined so the cancel button was clicked

All 12 comments

i had the same problem the props onCancel not found but i fix my problem by small trick check if date passed trough setDate if undefined so the cancel button was clicked

@simofilahi thanks , it's work !

@simofilahi thanks a lot..

@simofilahi thanks

Anyone aware of how to run a function when clicking 'OK'?

Anyone aware of how to run a function when clicking 'OK'?

183/5000
Hi

the event for datetimepicker has two types of status when you click

dismissed = calcel
set = is modified

validate what type of event is activated and will work without problems

This pattern should be added to the documentation.

@simofilahi Can you please tell me how to do so.I am new on this

@AdarshJais setDate take an argument, all you is to check this argument is it undefined so you clicked cancel.
If you still don't understand how to do that, paste ur code here and will see where is the problem

You can use the event.

if(event.type == "set") {
    //ok button clicked
} else {
    //cancel button clicked
}

you can listen to event onChange

 const onChange = (event, selectedDate) => {

        const currentDate = selectedDate || date;
        // add selected date . 
    // set  date show to null = hide 
        set_date_state({ show: null })

        }
    const onChange = (event, selectedDate) => {         //add the if else function inside the onChange function

   const currentDate = selectedDate || date;

 setShow(Platform.OS=== 'ios');

          if(event.type == "set") {          //ok button

                  setDate(currentDate)
        } else {                                    //cancel Button
                 return null
      }
Was this page helpful?
0 / 5 - 0 ratings