mui / material-ui-pickers

Date & Time pickers for Material UI (support from v1 to v4)
https://github.com/mui/material-ui-pickers/issues/2157
MIT License
2.32k stars 832 forks source link

DateRangePicker not accepting dateAdapter #1658

Closed Dashue closed 4 years ago

Dashue commented 4 years ago

Environment

Tech Version
@material-ui/pickers 4.0.0-alpha.5
material-ui 4.9.10
TypeScript 3.8.3
React 16.13.1
Browser
Peer library moment 2.24.0

Steps to reproduce

  1. Create a DateRangePicker
  2. Feed it a import dateAdapter: MomentAdapter from '@material-ui/pickers/adapter/moment'

Expected behavior

Expected to work just like existing Date*Pickers from the library

Actual behavior

Can not find utils in context. It looks like you forgot to wrap your component in LocalizationProvider, or pass dateAdapter prop directly.

Live example

https://codesandbox.io/s/daterangepicker-momentdateadapter-gh7yj?file=/src/index.tsx

For some reason giving different error regarding moment, but if you copy the code to a vscode it does give the reported error.

dmtrKovalenko commented 4 years ago

Thanks, yeah it is a problem. Fix is only add the same wrapping with withDateAdapterProp as for usual picker https://github.com/mui-org/material-ui-pickers/blob/7bed283699ef768936a3ec7c5dc89571492dddd4/lib/src/Picker/makePickerWithState.tsx#L140

Working reproduction: https://codesandbox.io/s/daterangepicker-momentdateadapter-yhe25?file=/src/index.tsx

oliviertassinari commented 4 years ago

@Dashue Side question, what's your motivation for using the dateAdapter prop instead of the LocalizationProvivder? Thanks!

Dashue commented 4 years ago

@dmtrKovalenko wow that was quick! 👍

@oliviertassinari purely personal, just needed to hard-code something and lack of exposure to providers made dateAdapter = new MomentAdapter({locale:'us-GB'}) feel less magical.

Got it working with a useMomentAdapter hoc, and will either switch everything to use dateAdapter if that gets supported or switch to using LocalizationProvider.

If I wasn't hard-coding things I wouldn't be too sure of how to update the LocalizationProvider value once set?

Thanks for your time guys!

oliviertassinari commented 4 years ago

@Dashue If dateAdapter wasn't available as a prop, would you miss it (forcing you to us LocalizationProvider)?

Dashue commented 4 years ago

@oliviertassinari probably not

dmtrKovalenko commented 4 years ago

Fixed as for alpha.9

oliviertassinari commented 4 years ago

probably not

@Dashue OK, interesting. I have open #1978 on this matter.