-
Notifications
You must be signed in to change notification settings - Fork 832
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
LocalizationProvider #1537
Merged
Merged
LocalizationProvider #1537
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2f22376
Rename MuiPickersUtilsPorvider => LocalizationProvider
dmtrKovalenko 3267dda
Improve utils typings
dmtrKovalenko 7ff010d
Implement global format override
dmtrKovalenko ce25632
[docs] Change name of date-io customization page
dmtrKovalenko 98dd267
Update examples to include new wording
dmtrKovalenko 5bd4aca
Fix prop-types typescript error
dmtrKovalenko 02572d8
Add daetAdapter prop for passing date-io utils directly to component
dmtrKovalenko 879bac6
Update percy and cypress
dmtrKovalenko 03e3e94
Unskip flaky test
dmtrKovalenko d00436a
Fix typo in error message
dmtrKovalenko f5275e8
Fix cypress test
dmtrKovalenko 5a12834
One more try to fix flaky test
dmtrKovalenko d228f4f
Remove flaky test
dmtrKovalenko 3b1e4d2
Fix visual regression scenarios tests with new version of cypress
dmtrKovalenko ffe5df0
Run cypress tests in chrome
dmtrKovalenko 36719aa
Ignore dark theme change in snapshots
dmtrKovalenko 71544e1
Try to fix example styles one more time
dmtrKovalenko 1f6c278
Fix inccorect name of cypress executor
dmtrKovalenko 4c9eb7a
Try weird hack to reinject styles
dmtrKovalenko 693bd49
Optimize theme toggling for visual regression
dmtrKovalenko e49eace
Update scenario names to get rid of duplications
dmtrKovalenko 54f6fdd
Rename `adapter` => `dateAdapter`
dmtrKovalenko e2d4625
Rename libFormats => dateFormats, libInstance => dateLibInstance
dmtrKovalenko b2c19b0
Remove version-specific code from README.md
dmtrKovalenko 47c9b19
Merge branch 'next' into feature/LocalizationProvider
dmtrKovalenko 51e75b0
Fix documentation erros in installation guide
dmtrKovalenko d3825a3
Run prettier on README.md
dmtrKovalenko e0a3f71
Use edge="end" for keyboard adornment icons, closes #1545
dmtrKovalenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ snapshot: | |
#codefund { | ||
display: none; | ||
} | ||
| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useState } from 'react'; | ||
import ruLocale from 'date-fns/locale/ru'; | ||
import deLocale from 'date-fns/locale/de'; | ||
import DateFnsAdapter from '@material-ui/pickers/adapter/date-fns'; | ||
import { useMemo } from 'react'; | ||
import { DatePicker } from '@material-ui/pickers'; | ||
|
||
const staticDateAdapter = new DateFnsAdapter({ locale: ruLocale }); | ||
|
||
function UsingDateAdapterProp() { | ||
const [locale] = useState(deLocale); | ||
const [selectedDate, handleDateChange] = useState(new Date()); | ||
const memoizedDateAdapter = useMemo(() => { | ||
return new DateFnsAdapter({ locale }); | ||
}, [locale]); | ||
|
||
return ( | ||
<> | ||
<DatePicker | ||
value={selectedDate} | ||
onChange={date => handleDateChange(date)} | ||
dateAdapter={staticDateAdapter} | ||
/> | ||
|
||
<DatePicker | ||
helperText="In case you need to generate adapter from state" | ||
value={selectedDate} | ||
onChange={date => handleDateChange(date)} | ||
dateAdapter={memoizedDateAdapter} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export default UsingDateAdapterProp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { useState } from 'react'; | ||
import DateFnsAdapter from '@material-ui/pickers/adapter/date-fns'; | ||
import { DatePicker } from '@material-ui/pickers'; | ||
import { LocalizationProvider } from '@material-ui/pickers'; | ||
|
||
// Simple example, in some cases it can be helpful to reverse year order (from future to past). | ||
// Here we are simply override called by pickers function and reversing the result | ||
class OverriddenAdapter extends DateFnsAdapter { | ||
getYearRange(start, end) { | ||
return super.getYearRange(start, end).reverse(); | ||
} | ||
} | ||
|
||
function DateFnsLocalizationExample() { | ||
const [selectedDate, handleDateChange] = useState(new Date()); | ||
|
||
return ( | ||
<LocalizationProvider dateAdapter={OverriddenAdapter}> | ||
<DatePicker openTo="year" value={selectedDate} onChange={handleDateChange} /> | ||
</LocalizationProvider> | ||
); | ||
} | ||
|
||
export default DateFnsLocalizationExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Ad from '_shared/Ad'; | ||
import Code from '_shared/Code.tsx'; | ||
import Example from '_shared/Example'; | ||
import PageMeta from '_shared/PageMeta'; | ||
import * as DateAdapterProp from './DateAdapterProp.example'; | ||
|
||
<PageMeta title="Customize date management logic" /> | ||
|
||
## Passing date adapter down to pickers | ||
|
||
There are a couple of ways to pass date-io adapter to pickers components. | ||
|
||
<Ad /> | ||
|
||
#### Context vs dateAdapter prop | ||
|
||
Recomended way to pass date adapter is using our `LocalizationProvider` and pass it through the context. | ||
Also there is a `dateAdapter` prop available, it allows to get rid of additional context. | ||
|
||
But you need to understand a few things: | ||
|
||
- `dateAdapter` will create a new context instance inside the date-picker, it may impact rendering performance if you have a lot of pickers on the screen | ||
- You must make sure your adapter is properly memoized before passing it down | ||
(if not – all components tree inside any picker will always rerender on your component change) | ||
|
||
<Example source={DateAdapterProp} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Ad from '_shared/Ad'; | ||
import Code from '_shared/Code.tsx'; | ||
import Example from '_shared/Example'; | ||
import PageMeta from '_shared/PageMeta'; | ||
import utilsInterfaceCode from '!raw-loader!@date-io/core/IUtils.d.ts'; | ||
import * as FormatsExample from './Formats.example'; | ||
import * as OverrideLogicExample from './OverrideLogic.example'; | ||
|
||
<PageMeta title="Customize date management logic" /> | ||
|
||
## Customization date management logic | ||
|
||
For some reason, like timezone management and localization you may need to control how datepickers are working | ||
with your date management library. | ||
|
||
<Ad /> | ||
|
||
#### Global formats customization | ||
|
||
All the formats used by the datepicker can be changed by `libFormats` prop in `LocalizationProvider`. | ||
Find all availble formats in [Adapter interface](#utils-interface) | ||
|
||
<Example source={FormatsExample} /> | ||
|
||
#### Override date logic | ||
|
||
It is also possible to extend any adapter we providing and change the logic of date manipulations. | ||
Simply extend exported version of `date-io` adapter, and make it work as you need accordingly to [used interface](#utils-interface) | ||
|
||
> You can use ES6 class syntax or override values with the help of `.prototype` Object property. | ||
|
||
<Example source={OverrideLogicExample} /> | ||
|
||
#### Utils interface | ||
|
||
_Note_ TDate - date object passed from the state (moment, native Date or Luxon`s DateTime) | ||
|
||
<Code language="ts" children={utilsInterfaceCode} /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of using
React.useState
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is even more invalid, somewhere I thought that this kind of import will be deprecated. We are primarily using
import * as React
, but in examples using this notation.I have no preference and can change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a related thread on this topic at mui/material-ui#19802.
What approach have you seen the more frequently used by TypeScript users on one side? And by JavaScript users on the other?
There are no clear guidelines by React on the matter, but I could find these two examples:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For typescript codebase, it is much easier to use
import * as React
because a lot of internal types are also exported, likeReact.ReactNode
,React.HtmlProps
and so on.