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

Inner state for picker modal #1095

Merged
merged 7 commits into from
Jun 9, 2019
Merged

Conversation

dmtrKovalenko
Copy link
Member

This PR closes #1049

Description

This PR make pickers to use the inner state for displaying inside modal. To reduce side-effects for other API. It will still dispatch onChange for keyboard mode but will dispatch onChange for standard pickers only when the date is accepted.

@dmtrKovalenko dmtrKovalenko requested a review from TrySound June 8, 2019 07:47
@dmtrKovalenko dmtrKovalenko added the enhancement New feature or request label Jun 8, 2019
@dmtrKovalenko
Copy link
Member Author

Also closes #1088

@codecov
Copy link

codecov bot commented Jun 8, 2019

Codecov Report

Merging #1095 into next will decrease coverage by 0.13%.
The diff coverage is 94.59%.

Impacted file tree graph

@@            Coverage Diff             @@
##             next    #1095      +/-   ##
==========================================
- Coverage   95.14%   95.01%   -0.14%     
==========================================
  Files          52       53       +1     
  Lines        1340     1344       +4     
  Branches      188      178      -10     
==========================================
+ Hits         1275     1277       +2     
- Misses         47       49       +2     
  Partials       18       18
Impacted Files Coverage Δ
lib/src/Picker/WrappedKeyboardPicker.tsx 100% <ø> (ø) ⬆️
lib/src/_shared/hooks/useKeyDown.ts 72.22% <ø> (ø) ⬆️
lib/src/_shared/hooks/useKeyboardPickerState.ts 76.92% <ø> (ø) ⬆️
lib/src/Picker/WrappedPurePicker.tsx 100% <ø> (ø) ⬆️
lib/src/_shared/hooks/useOpenState.ts 100% <100%> (ø)
lib/src/_shared/WithUtils.tsx 100% <100%> (ø) ⬆️
lib/src/_shared/hooks/usePickerState.ts 92.15% <91.3%> (-1.18%) ⬇️
lib/src/TimePicker/components/Clock.tsx 96% <0%> (-4%) ⬇️
lib/src/TimePicker/TimePickerToolbar.tsx 95% <0%> (-2.5%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 81c813e...5ebb355. Read the comment docs.

* Update packages 08.06.2019

* Fix prettier
@TrySound
Copy link
Contributor

TrySound commented Jun 9, 2019

Here ok button is missing but month click does not close popup. Is it intentional?
image

@dmtrKovalenko
Copy link
Member Author

I guess it is unrelated

@dmtrKovalenko dmtrKovalenko merged commit 48cd17a into next Jun 9, 2019
@dmtrKovalenko dmtrKovalenko deleted the feature/inner-state-for-picker branch June 9, 2019 08:03
props.onAccept(acceptedDate);
}
},
[setIsOpen, props]
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIU, the props object always changes. It's possible, in theory, that the component that passes the props has them memoized, but in all normal circumstances (using JSX, or call sites like this), the props object will be a new object. And useCallback only memoizes when the deps are the same according to Object.is(), so the useCallback() here is ineffective. It can either be removed (if we don't care about memoization) or be fixed:

const {onAccept, onChange} = props;
const acceptDate = useCallback(
  (acceptedDate: MaterialUiPickersDate) => {
    setIsOpen(false);
    onChange(acceptedDate);
     if (onAccept) {
      onAccept(acceptedDate);
    }
  },
  [setIsOpen, onAccept, onChange]
);

If you agree (I might be wrong, I'm very new to hooks), I can send a PR for this and similar occurrences.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that will be more efficient

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 not be changed if only internal state changed. It anyway it will be cool to have more clear deps for top level hook

@Philipp91
Copy link
Contributor

Off-topic question: Is there a way I can test changes like this one in my own app before they have been released to NPM? I tried cloning the repo, running the build and then pointing the NPM dependency from my app to the lib/build directory. Everything has the right structure, but at runtime it complains that React was loaded twice (which breaks hooks, "Invariant Violation: Invalid hook call.").

@dmtrKovalenko
Copy link
Member Author

You can make a build and link your app manually for the folder lib/build.
Go to lib/build
Run npm link
Go to your app
Run npm link @material-ui/pickers

@Philipp91
Copy link
Contributor

Philipp91 commented Jun 9, 2019

Thanks! I didn't know that npm link exists. The commands you gave lead to the same symlink I had before (just via the global node_modules directory), so it didn't resolve the duplicate-React issue. But knowing that npm link is the canonical way to do this allowed me to research this issue. I tried many things that try to fix the symlink situation, but none of them worked. Instead, I did this:
https://medium.com/@vcarl/problems-with-npm-link-and-an-alternative-4dbdd3e66811

material-ui-pickers/lib/build/ $ npm pack
my-app/ $ npm install ../material-ui-pickers/lib/build/material-ui-pickers-3.1.0.tgz

Obviously that's more annoying for iterating quickly, but it worked for testing this PR. I can confirm that the fix in this PR works for me and can replace the workaround classes discussed in #1049. Thanks a lot!

@pankaz
Copy link

pankaz commented Jun 13, 2019

Kindly merge #1036 into the "next" branch. The "clear" functionality is breaking in the current "next" branch.

dmtrKovalenko added a commit that referenced this pull request Jul 31, 2019
* Fix not working logo

* Mention required material-ui v4 in README.md

* 📝 improve grammar in docs (#1073)

* 📝 improve grammar in docs

First of all, amazing project! 🎉 I took the liberty, and read through the docs, and on my way, I corrected some of the English grammar, I hope you don't mind! Keep up the great work! 👨‍🏭

* 📝 more grammar improvements

* Fix time order in RTL DateTimePicker (mui-org#1071) (#1072)

* Consume fade correctly (#1077)

* Remove react-event-listener (#1075)

* Remove react-event-listener

* Fix lint

* Make reusable useKeyDown

* Update packages | 02 06 2018 (#1079)

* Update packages

* Shut up now bot

* Fix now.json silent location

* Fix ts error for navigator.permissions

* Disable ToolbarButtonOverride, closes #1076 (#1080)

* Fix inputRef collision, closes #1069 (#1081)

* [DatePicker] add strictCompareDates prop (#1067)

* DatePicker: add validateStrict props

Add strict camparison for minDate, maxDate, disablePast, disableFuture

* Min/Max date validation

rename props validateStrict to strictCompareDates

* DateValidation: strictCompareDates reduce conditions

* Async month changed callbacks (#1052)

* Added async handling of onMonthChange with loading indicator

* Only show loading indicator if onMonthChange returns a Promise

* Added test cases

* Added some more test cases. Testing push and pop functionality of loading queue

* Removed unused import

* Updated docs and changed callback type in MonthSelection

* Replaced 'async/await' with 'then' to reduce bundle size

* Importing CircularProgress by path instead

* Added support for optional loading indicator as a property

* Moved tests to e2e

* Removed unused import

* Fixed Prettier violation

* [typescript] Fix incorrectly inferred type for keyboard onChange (#1083)

* Support keyboardIcon for v3, closes #1084 (#1085)

* Support keyboardIcon for v3. closes #1084

* Add new prop to the docs

* Support TextFieldComponent prop (#1087)

* v3.1.0

* Change release script

* Change bundlephobia badge link

* Compare date strict tests (#1089)

* WrappedKeyboardPicker: send strictCompareDates prop

* compareDateStrict: add tests

* Fix "todayLabel" prop type description. (#1091)

* Fix "todayLabel" prop type description.

* Update lib/src/wrappers/ModalWrapper.tsx

Co-Authored-By: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com>

* Fix typescript material-ui overrides example (#1092)

* Spread emptyLabel from passing to textField, closes #1093

* Fix incorrect DateTimePicker year button alignment on wide devices

* Inner state for picker modal (#1095)

* Use inner state for displayed date in calendar

* Make inline pickers controlled by default

* Fix tests

* Fix deadlock on rendering with usePickerState

* Fix crashing on utils change

* Fix not applying keydown listener, closes #1090

* Update packages 08.06.2019 (#1096)

* Update packages 08.06.2019

* Fix prettier

* [docs] Add fetching data example (#1097)

* Use inner state for displayed date in calendar

* Make inline pickers controlled by default

* Fix tests

* Fix deadlock on rendering with usePickerState

* Fix crashing on utils change

* [docs] Add fetching data example

* Change text and remove unnecessary details from example

* Adjust some props description

* [Refactoring] Reorganize folder structure (#1098)

* Regorganize folder structure

* Create new views folder with all components

* Remove unnecessary theme type annotations

* Restore lost in merge changes

* Fix imports in tests

* [Refactoring] Refactor Calendar.tsx

* [Refactoring] Better hook memoization (#1101)

* [Refactoring] Refactor useOpenState implementation

Eliminate code duplication between the controlled and the uncontrolled implementation.
Albeit short today, the code that calls the onOpen/onClose callbacks was duplicated.
The controlled variant did not memoize with useCallbacks().

Fix memoization by not depending on `props` as a whole.

* [Refactoring] Better hook memoization in usePickerState/useKeyboardPickerState

Avoid passing the entire `props` object into the hooks' `deps', as it will usually be a fresh object, which prevents memoization.

* Make "Today" button to set inner state, instead of onChange

* Fix prettier in-merge error

* [#1111] Properly rely on views order when switching views (#1116)

* Polyfill array includes manually for IE11 (#1117)

* Polyfill array includes manually for IE11

* Make proper implementation of arrayIncludes 🙆‍♂️

* Update release script

* v3.1.1

* Add visual regression tests (#1121)

* Add visual regression tests

* Make docs run in backgorund on ci for tests

* Fix percy command not found error

* Add cypress verify on install

* Restory cypress binary cache before running

* Add additional yarn install

* Change order of builds

* Fix tsconfig.json error

* Add more visual regression scenarios

* Update config.yml

* Disable random icons in percy

* Make year to be 2019 in tests

* Add more scenarios

* Completely hide Ads in percy

* [docs] Fix crashing on changing utils (#1123)

* [docs] Fix crashing on changing utils

* Rewrite MuiPickersUtilsProvider on hooks

* Fix moment localization example

* Update SecondsTimePicker.example.jsx (#1125)

* Add Percy badge (#1129)

* [docs] Fix views type labels for DatePicker API (#1138)

* [#1126] Fix rerendering loop

* Fix invalidLabel prop-type warning

* [#1126] Fix rerendering loop (#1139)

* Custom spacing support (#1141)

* Add custom theme spacing example

* Fix issues with theme spacing

* Allow to pass custom rifm formatter (#1142)

* v3.1.2

* 3.1.2

* [Calendar] Remove arrows white background on sliding from disabled state (#1152)

* Add Hijri calendar example (#1153)

* Add Hijri calendar example

* Combine jalaali and hijri in single page

* Address PR feedback

* Fix headers size

* Do not throw error if input value was cleared symbol by symbol (#1160)

* [TimePicker] Properly hide separator if views not included (#1161)

* [InlineVariant] Do not update value on backdrop click (#1162)

* [docs] Fix variant prop type displaying, highlight code in 'type' column (#1163)

* Fix variant prop displaying, highlight type code

* Remove additional space after each prop

* Add variant="static" (#1164)

* Add variant="static"

* More variant="static" examples

* Landscape orientation support  (#1177)

* Fix persian examples

* Remove injected css after front-end mount

* Determine and apply styles for current orientation

* Add tests

* Support landscape for TimePicker

* Use isomorphic effect for event handler

* Fix lint errors

* Add new props to the documentation

* Fix displaying short weekdays in datepicker toolbar

* Custom toolbar component (#1182)

* Add new ToolbarComponent prop for custom toolbars

* Update props docs

* Run docgen on precommit hook

* Run docgen on precommit hook

* Run git add after precommit command

* Automatically format json before prettier

* Do not accept a and p sympbols in not am/pm mode (#1183)

* Fix keyboard popover position (#1184)

* Correct position of inline wrapper popover

* Adjust variant prop description

* Adjust props description

* Uncomment example

* Add prop-types.json to prettier-ignore

* Properly freeze clock for percy

* Use yarn to publish

* v3.2.0

* Update publish command

* Use KeyboardDatePicker for formik integration example

* [docs] Add redux form integration example (#1189)

* install redux and redux-form

* configure redux-form

* wrap App with redux provider

* create redux form example

* add react-redux types to dev dependencies

* Update docs/pages/guides/form-integration.mdx

Co-Authored-By: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com>

* move redux to example folder

* Wrap example with redux

* remove redux-thunk

* modify onChange to allow empty field

in the other way, the field always gotten the last valid value, even when explicit deleted.
This way, the user can clear the field!

* move store to inside Form example

* Update useIsLandscape.tsx (#1199)

Fix crashing on changing orientation for some devices

* Update CSS overrides example (#1201)

isSelected is no longer a valid theme entry for MUI Pickers, so update
the docs to reflect this change. Also extends the example for
dayDisabled override.

* Support auto detection of orientation change for safari (#1207)

* Add inputProps to the outputted typescript definitions (#1208)

* Fix dispatching 2 onChange events with variant="inline" (#1209)

* Fix dispatching 2 onChange events with variant="inline"

* Update lib/src/_shared/hooks/usePickerState.ts

* Make autoOk property to have bigger prioritiy

* Add releases page (#1210)

* Add releases page

It lists available documentation websites for v3+ versions

* Use constant for the latest url link

* Make releases table horisontally scrollable on mobile

* Fix grammar

* Add shorthand for static state managment (#1212)

* [KeyboardDateInput] Fix not updating formatter when new mask passed (#1213)

* fix when both clear button & showToday button (#1211)

* Add babel transpilation to esm (#1206)

* Add babel transpilation to esm

* Use babel for rollup as well

* Fix missing * in jsdoc comment for ToolbarComponent

* Update packages (#1217)

* Update packages

* Fix overrides typing

* Add { force: true } for readonly inputs

* Add one more { force: true }

* v3.2.1

* Remove old versions from readme

* More clearly highlight requiring version of date-fns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Picking a date from calendar should not modify the TextInput until the user clicks OK
4 participants