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

iOS DateTimePicker in countdown mode fails to call onChange on first update #30

Open
elsieclark opened this issue Sep 26, 2019 · 9 comments · May be fixed by #312
Open

iOS DateTimePicker in countdown mode fails to call onChange on first update #30

elsieclark opened this issue Sep 26, 2019 · 9 comments · May be fixed by #312
Labels
help wanted Extra attention is needed

Comments

@elsieclark
Copy link
Contributor

Bug

In countdown mode, the first time the value in the picker gets updated, the onChange event isn't triggered. Every subsequent time, it is. The date, time, and datetime modes work fine.

I found the following Stack Overflow thread which describes exactly the same problem, except just in Swift, no React Native. This leads me to think there's a problem with the underlying Swift API.

For anyone else who runs into this problem, I was able to get around it by updating the value prop at least once upon initialization, before the user has time to interact with it. Even changing the value date by a single second seems to do the trick; I recommend that to avoid playing an animation on the update.

Workaround:

  value={this.state.initialized ? new Date(0, 0, 0, hours, minutes, seconds) : new Date(0, 0, 0, hours, minutes, seconds+1)}

Environment info

System:
    OS: macOS 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
    Memory: 680.85 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.15.3 - /usr/local/opt/nvm/versions/node/v10.15.3/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.9.2 - /usr/local/opt/nvm/versions/node/v10.15.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.0, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
  IDEs:
    Xcode: 11.0/11A420a - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.3 => 0.60.3 
  npmGlobalPackages:
    react-native-cli: 2.0.1

Library version: 2.1.0

Steps To Reproduce

  1. Add a DateTimePicker to your app
  2. Set mode prop to countdown
  3. Set onChange prop to something that should produce visible output
  4. Load up the component in your App. Slide the picker to a new value (First change). The onChange handler will not trigger.
  5. Slide the picker to another new value (Second change). The onChange handler will trigger.
    ...

Describe what you expected to happen:

  1. The onChange handler should update both times.

Reproducible sample code

<DateTimePicker
    mode={'countdown' as any}
    onChange={(event, date) => console.log('Updated!')}
    value={new Date(0, 0, 0, hours, minutes, seconds)}
/>
chetstone added a commit to chetstone/react-native-modal-datetime-picker that referenced this issue Jan 18, 2020
@chetstone
Copy link

@willdavidc Thanks for this workaround. It helps a lot, but unfortunately doesn't fix the problem completely. If the user spins the picker to 0 hours 0 minutes, iOS automatically changes it to 0 hours 1 minute, and onChange again doesn't fire the next time the user moves the picker.

Example code here.

@chetstone
Copy link

@Swaagie Could someone take a look at this bug? The workaround suggested by @willdavidc helps, though it took me a lot of trial and error to figure out where to set and reset this.state.initialized. My result is shown in this PR for another component but the owner of that library quite reasonably does not want to merge the workaround. It needs to be fixed here, probably in native code, which I'm not particularly proficient at.

Thanks!

@chetstone
Copy link

I've switched to react-native-countdown-picker. It doesn't have this problem. Although the library hasn't been updated in a long time, it is a very simple wrapper around the ActionSheetPicker-3.0 pod, which is actively maintained.
If you install from my PR (not sure when or if it'll ever get merged), you'll get auto-linking in RN >= 0.60.0

yarn add 4Catalyzer/react-native-countdown-picker#pull/3/head

@vonovak vonovak added the help wanted Extra attention is needed label Feb 1, 2020
@dylancom
Copy link

dylancom commented Mar 4, 2020

The workaround doesn't work for me...
Any news on this?
For the time being I've switched to: https://github.com/uraway/react-native-simple-time-picker

@connorlindsey
Copy link

A bit frustrating, but I have the workaround working for me. I'm using hooks and this is how I've set it up.

Not amazing, but it's working well enough.

  // Variable to hold the time, set it to 1 minute
  const [duration, setDuration] = useState(new Date(0, 0, 0, 0, 1, 0, 0))
  const [hasLoaded, setHasLoaded] = useState(false)
  useEffect(() => {
    setTimeout(() => {
      setHasLoaded(true)
    }, 50) // I needed to include the timeout for this to work
  }, [])
  //...

  <DateTimePicker
  ...
  value={hasLoaded ? duration : new Date(0, 0, 0, 0, 1, 5)}
  /> 
  // Other time probably doesn't have to be 5 seconds, but that's what I did

@aceiii aceiii linked a pull request Oct 17, 2020 that will close this issue
4 tasks
@pvanny1124
Copy link

A bit frustrating, but I have the workaround working for me. I'm using hooks and this is how I've set it up.

Not amazing, but it's working well enough.

  // Variable to hold the time, set it to 1 minute
  const [duration, setDuration] = useState(new Date(0, 0, 0, 0, 1, 0, 0))
  const [hasLoaded, setHasLoaded] = useState(false)
  useEffect(() => {
    setTimeout(() => {
      setHasLoaded(true)
    }, 50) // I needed to include the timeout for this to work
  }, [])
  //...

  <DateTimePicker
  ...
  value={hasLoaded ? duration : new Date(0, 0, 0, 0, 1, 5)}
  /> 
  // Other time probably doesn't have to be 5 seconds, but that's what I did

this worked for me. Thanks a lot!

@ingerable
Copy link

A bit frustrating, but I have the workaround working for me. I'm using hooks and this is how I've set it up.

Not amazing, but it's working well enough.

  // Variable to hold the time, set it to 1 minute
  const [duration, setDuration] = useState(new Date(0, 0, 0, 0, 1, 0, 0))
  const [hasLoaded, setHasLoaded] = useState(false)
  useEffect(() => {
    setTimeout(() => {
      setHasLoaded(true)
    }, 50) // I needed to include the timeout for this to work
  }, [])
  //...

  <DateTimePicker
  ...
  value={hasLoaded ? duration : new Date(0, 0, 0, 0, 1, 5)}
  /> 
  // Other time probably doesn't have to be 5 seconds, but that's what I did

worked for me but not very convenient as said above

@hugows
Copy link

hugows commented Feb 28, 2023

Thanks for this - I had to use it only in iOS 15.

In my case, because my picker was in a modal, I have to do the "workaround" every time the modal was toggled.

@chetstone
Copy link

TLDR; This appears to have been fixed by Apple in iOS 16, so this can probably be closed.

I ran across this again when I updated my app to RN 0.72. I had totally forgotten about it. (It's been a long time). I'm trying to support older versions of iOS, so tested with various simulators. The workaround described above seems to work up till iOS 16, after which it is no longer needed. In addition, I found that the other problem I reported above goes away in iOS 15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants