-
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
[WIP] Replace moment #218
[WIP] Replace moment #218
Conversation
Awesome! 👍 |
@dmtrKovalenko Please don't replace the formatting functions. Jalaali needs other formatters to work. I mean: for Gregorian its |
@alitaheri Maybe we can match format strings in persian utils from 'jYYYY' -> 'YYYY', because it would be helpful to have common formatting method for both date-fns and moment |
@cherniavskii @alitaheri |
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.
Sorry for taking so long to review this PR.
This PR is really huge (good work by the way), so I'm submitting first batch of review comments, I'll try to review more later ;)
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const withUtils = () => (Component) => { |
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.
Looks like first wrapper function is unused, so we can remove it (unless it is there for a purpose).
- const withUtils = () => (Component) => {
+ const withUtils = (Component) => {
Then usage of withUtils
will look like:
- withUtils()(Component)
+ withUtils(Component)
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.
I leave it for have an ability customize the decorator in the future.
lib/src/index.js
Outdated
@@ -3,3 +3,9 @@ export { default as DatePicker } from './DatePicker/DatePickerWrapper'; | |||
export { default as TimePicker } from './TimePicker/TimePickerWrapper'; | |||
|
|||
export { default as DateTimePicker } from './DateTimePicker/DateTimePickerWrapper'; | |||
|
|||
export { default as MuiPickerUtilsProvider } from './utils/MuiPickersUtilsProvider'; |
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.
typo in export name
- MuiPickerUtilsProvider
+ MuiPickersUtilsProvider
@@ -2,6 +2,8 @@ import * as React from 'react' | |||
import { Fragment, Component } from 'react'; | |||
import TimePickerWrapper from '../../src/TimePicker/TimePickerWrapper'; | |||
import { Moment } from 'moment' | |||
import { utilsToUse } from '../test-utils'; | |||
import MuiUtilsProvider from '../../src/utils/MuiPickersUtilsProvider' |
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.
Let's unify provider naming by using MuiPickersUtilsProvider
name everywhere
? momentUtils | ||
: dateFnsUtils; | ||
|
||
const getComponentWithUtils = Component => React.cloneElement(Component, { utils: utilsToUse }); |
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.
why not using withUtils
here
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.
Because then shallow rendering would render just a HOC, instead of testing component
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.
You can use enzyme dive
method or use createShallow({ dive: true })
from` MUI test utils
lib/package.json
Outdated
@@ -40,11 +40,15 @@ | |||
"react-dom": "^16.2.0" | |||
}, | |||
"dependencies": { | |||
"date-fns": "^1.29.0", |
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 about making material-ui-pickers
to not rely on date libraries at all?
We can add moment-utils
and datefns-utils
as peerDependencies (each with moment
or dateutils
dependencies accordingly)
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.
Of course I will remove date-fns dependency and will let user to choose which utils to use moment or date-fns. And there would not peerDependecies at all
lib/src/utils/date-fns-utils.js
Outdated
import getYear from 'date-fns/get_year'; | ||
import isEqual from 'date-fns/is_equal'; | ||
|
||
export default class DateFnsUtils { |
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.
why using class
here? Isn't good ol' object better for this purpose?
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.
- Have an ability to extend this class and override some props (usefull for persian picker)
- Easy to determine which utils are using via
typeof
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.
That makes more sense, but both may be done with object too
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.
Great job @dmtrKovalenko Thanks 👍 👍
lib/typings/utils.ts
Outdated
startOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate; | ||
endOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate; | ||
format(value: MaterialUiPickersDate): string; | ||
formatNumber(number: number): MaterialUiPickersDate |
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.
shouldn't this return string?
lib/typings/utils.ts
Outdated
isAfterYear(value: MaterialUiPickersDate, comparing: MaterialUiPickersDate): boolean; | ||
startOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate; | ||
endOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate; | ||
format(value: MaterialUiPickersDate): string; |
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.
can you document what strings this function gets for formats (e.g. 'YYYY'
, 'MMM D'
, ...). helps others implement their own utils.
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.
@alitaheri I think we should leave pure functions for rendering dates. Like in persian-utils now. It would be more convenient to users, who would be override existsing utils :)
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.
format. Takes a second argument. The format string. I'm only saying it would be helpful to document the expected input fot format strings. How can that make it impure?
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.
Oh, I didnt understand you, sorry)
lib/typings/utils.ts
Outdated
|
||
export interface Utils { | ||
date(value: any): MaterialUiPickersDate; | ||
addDays(value: MaterialUiPickersDate, count: Number): MaterialUiPickersDate; |
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.
count: number
lib/typings/utils.ts
Outdated
@@ -0,0 +1,34 @@ | |||
import { MaterialUiPickersDate } from './date' | |||
import { isEqual, isAfter, endOfDay, setHours } from 'date-fns'; |
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.
change the file name to .d.ts
and remove this import. I think this is redundant.
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.
@alitaheri I have removed date-fns imports, have no idea how they appeared there :)
this file just a common interface for moment-utils and date-fns-utils
@@ -10,7 +10,7 @@ describe('ModalWrapper', () => { | |||
}); | |||
|
|||
it('Should renders', () => { | |||
console.log(component.debug()); // TODO REMOVE ME | |||
// console.log(component.debug()); |
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.
why comment and not just remove the line? it's not pretty 😅
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.
Oh, thats just really helpfull to just uncomment line and have an ability to review component)
@@ -123,7 +123,7 @@ class Demo extends Component { | |||
sourceFile="PersianPickers.jsx" | |||
> | |||
<PersianPickers /> | |||
</SourcablePanel> | |||
</SourcablePanel> */} |
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.
Good idea 👍 I'll update my utils library and fix these demos 👍 👍
New build system
@alitaheri I have returned back all text formatting methods. And as far I understand have fixed all issues and finish with the feature at all. If you have any concerns let me a shout :) |
README.md
Outdated
|
||
```jsx | ||
import { MuiPickersUtilsProvider } from 'material-ui-pickers'; | ||
import MomentUtils from 'material-ui-picker/utils/moment-utils'; |
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.
typo, should be material-ui-pickers
, not material-ui-picker
README.md
Outdated
@@ -97,7 +110,7 @@ openToYearSelection | boolean | false | Open datepicker from year selection | |||
minDate | date | '1900-01-01' | Minimum selectable date | |||
maxDate | date | '2100-01-01' | Maximum selectable date | |||
onChange | func | required | Callback firing when date accepted | |||
returnMoment | boolean | true | Will return moment object in onChange | |||
returnMoment | boolean | true | Will return moment object in onChange (if moment utils using) |
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.
let's remove this prop (as proposed in #172) in order to improve API consistence
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.
@cherniavskii I dont think its a good proposal, because a lot of project is using moment + pickers now and that will cause a lot of breaking changes because it will require all users to process native date.
Or you sugges always return moment for moment pickers and date for datefns?
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.
Or you sugges always return moment for moment pickers and date for datefns
Yes, in case of date-fns returnMoment
prop doesn't make any sense.
because a lot of project is using moment + pickers now and that will cause a lot of breaking changes
Well, there will be breaking changes anyway
because it will require all users to process native date
Yes, but they can use utils
to do it.
This way we'll have consistent and predictable API. If we'll add utils for one more date processing library in the future - API will not 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.
You maybe not fully understand the purpose of utils architecture? Its a way to users that work with moment pass moment objects and get moment objects from the pickers, and also not reduce bundle size by another date library.
So it just the best of utils - return moment if moment utils and date if date utils
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.
Does returnMoment
still make sense?
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.
I think I will remove it and force passing moment everywhere if moment-utils provided
@@ -165,7 +178,7 @@ rightArrowIcon | react node | `<Icon>keyboard_arrow_right</Icon>`| Right arrow i | |||
dateRangeIcon | react node | `<Icon>date_range</Icon>`| Date tab icon | |||
timeIcon | react node | `<Icon>access_time</Icon>`| Time tab icon | |||
ampm | boolean | true | 12h/24h view for hour selection clock | |||
shouldDisableDate | (date: Moment) => boolean | () => false | Allow to disable custom date in calendar | |||
shouldDisableDate | (date: Moment | Date) => boolean | () => false | Allow to disable custom date in calendar |
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.
can we avoid passing moment object to this callback?
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.
No. We are poviding moment for api consistent. Moment utils is a way to use moment in this callback.
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 if we pass utils
as second argument? This will allow data processing
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.
@cherniavskii read my answer to your previous coment :)
@@ -175,15 +188,6 @@ mask | text mask (read more [here](https://github.com/text-mask/text-mask/blob/m | |||
clearable | boolean | false | If `true`, clear button will be displayed | |||
TextFieldComponent | func, string | undefined | Component that should replace the default Material-UI TextField | |||
|
|||
### l10n | |||
For l10n texts we're currently relying on moment which is stateful. To change the locale you have to import your langauge specific files an change the locale manually via `moment.locale(language)`. |
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.
Would be great to provide at least links to moment/date-fns l10n docs
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 will be a part of next release
No updates, support of persian pickers would be a part of next release.
@cherniavskii |
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.
Great, go ahead ;)
🎉
This PR will replace moment from peer dependency of the project #61
I have moved all moment specific usages to the utils, used for persian pickers.
But now persian pickers are broken, @alitaheri I need your input on that
Also we need to update typings and move utils to the separate npm package.
Feel free to discuss all things you have found here.