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

[WIP] Replace moment #218

Merged
merged 24 commits into from
Feb 28, 2018
Merged

[WIP] Replace moment #218

merged 24 commits into from
Feb 28, 2018

Conversation

dmtrKovalenko
Copy link
Member

@dmtrKovalenko dmtrKovalenko commented Feb 13, 2018

🎉
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.

@dmtrKovalenko dmtrKovalenko changed the title 🎉 Replace moment [WIP] Replace moment Feb 13, 2018
@dmtrKovalenko dmtrKovalenko self-assigned this Feb 13, 2018
@remcohaszing remcohaszing mentioned this pull request Feb 13, 2018
1 task
@cherniavskii
Copy link
Member

Awesome! 👍
I'll take a look at it later :)

@alitaheri
Copy link
Member

@dmtrKovalenko Please don't replace the formatting functions. Jalaali needs other formatters to work. I mean: for Gregorian its YYYY but for Jalali its jYYYY. please revert back to using explicit functions like getYearText

@dmtrKovalenko
Copy link
Member Author

@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

@dmtrKovalenko
Copy link
Member Author

@cherniavskii @alitaheri
I have finished probably all logic for date-fns injection. I will return back not generic formatting (via functions) for persian pickers tommorrow, so can you please review this PR so we will release it this week.
Thank you 🎉

@dmtrKovalenko dmtrKovalenko changed the base branch from master to develop February 21, 2018 07:41
Copy link
Member

@cherniavskii cherniavskii left a 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) => {
Copy link
Member

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)

Copy link
Member Author

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';
Copy link
Member

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'
Copy link
Member

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 });
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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",
Copy link
Member

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)

Copy link
Member Author

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

import getYear from 'date-fns/get_year';
import isEqual from 'date-fns/is_equal';

export default class DateFnsUtils {
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Have an ability to extend this class and override some props (usefull for persian picker)
  2. Easy to determine which utils are using via typeof

Copy link
Member

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

Copy link
Member

@alitaheri alitaheri left a 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 👍 👍

startOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate;
endOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate;
format(value: MaterialUiPickersDate): string;
formatNumber(number: number): MaterialUiPickersDate
Copy link
Member

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?

isAfterYear(value: MaterialUiPickersDate, comparing: MaterialUiPickersDate): boolean;
startOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate;
endOfDay(value: MaterialUiPickersDate): MaterialUiPickersDate;
format(value: MaterialUiPickersDate): string;
Copy link
Member

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.

Copy link
Member Author

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 :)

Copy link
Member

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?

Copy link
Member Author

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)


export interface Utils {
date(value: any): MaterialUiPickersDate;
addDays(value: MaterialUiPickersDate, count: Number): MaterialUiPickersDate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count: number

@@ -0,0 +1,34 @@
import { MaterialUiPickersDate } from './date'
import { isEqual, isAfter, endOfDay, setHours } from 'date-fns';
Copy link
Member

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.

Copy link
Member Author

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());
Copy link
Member

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 😅

Copy link
Member Author

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> */}
Copy link
Member

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 👍 👍

@dmtrKovalenko
Copy link
Member Author

@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 :)
So what we will do with persian pickers next?
All you need - its just import moment utils, extend them and override all displayed methods.

README.md Outdated

```jsx
import { MuiPickersUtilsProvider } from 'material-ui-pickers';
import MomentUtils from 'material-ui-picker/utils/moment-utils';
Copy link
Member

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)
Copy link
Member

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

Copy link
Member Author

@dmtrKovalenko dmtrKovalenko Feb 27, 2018

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?

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

@dmtrKovalenko dmtrKovalenko Feb 27, 2018

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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

@dmtrKovalenko dmtrKovalenko Feb 27, 2018

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)`.
Copy link
Member

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

Copy link
Member Author

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

@dmtrKovalenko dmtrKovalenko dismissed alitaheri’s stale review February 28, 2018 12:09

No updates, support of persian pickers would be a part of next release.

@dmtrKovalenko
Copy link
Member Author

@cherniavskii
I want to merge this PR, test a bit new functionality and create a new release. (rc-1)
We will add support of localization at all and integrate persian pickers in the next release, so do you have any additional questions?

Copy link
Member

@cherniavskii cherniavskii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, go ahead ;)

@dmtrKovalenko dmtrKovalenko merged commit 7457129 into develop Feb 28, 2018
@cherniavskii cherniavskii deleted the feature/replace-moment branch March 1, 2018 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants