-
Notifications
You must be signed in to change notification settings - Fork 12
WB Date Picker: initial migration #2876
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
Open
marcysutton
wants to merge
29
commits into
feature/datepicker
Choose a base branch
from
WB-1112
base: feature/datepicker
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c1d6554
[WB-1112] Initial commit
marcysutton 561f52f
[WB-1112] Update react-day-picker
marcysutton 2acce1e
[WB-1112] Initial port from frontend
marcysutton 2cfc85e
[WB-1112] WIP: dependency migration
marcysutton 50cfde8
[WB-1112] Continued Date Picker migration
marcysutton 0cf4e5e
[WB-1112] Improve rootWithEsc props
marcysutton 451b6b7
[WB-1112] Fix date-picker tests
marcysutton 05a6836
[WB-1112] Fix remaining tests, add stories
marcysutton 2c8250a
[WB-1112] docs(changeset): Add new Date Picker package and related de…
marcysutton b00e5ee
[WB-1112] Clean up moment references
marcysutton beb90f2
[WB-1112] Add modal dependency
marcysutton fbdf13b
[WB-1112] Add missing deps
marcysutton 2cc7e5f
[WB-1112] Update tsconfig
marcysutton c05384d
[WB-1112] Fine tune snapshots
marcysutton 72a41f2
[WB-1112] Remove unused theme files
marcysutton 6146853
[WB-1112] Improve styling
marcysutton 6357c82
[WB-1112] Update dependencies
marcysutton a648bc9
[WB-1112] Restore deps for Storybook
marcysutton 39dd0b9
[WB-1112] Remove empty files
marcysutton 4ce062e
[WB-1112] Remove skipped tests
marcysutton f773387
[WB-1112] Rework CSS bundling
marcysutton a44753f
[WB-1112] Update exports, remove stories
marcysutton b92e51a
[WB-1112] Remove unused file
marcysutton 2dd7f84
[WB-1112] Update utils and tests
marcysutton 7b77ef2
[WB-1112] Simplify PR
marcysutton ccc5db3
[WB-1112] Update docs
marcysutton d77cc9b
[WB-1112] Restore CSS handling approach
marcysutton 6ed6765
[WB-1112] Add more dates to overlay story
marcysutton 4dbb09e
[WB-1112] Add theme accent color
marcysutton 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 hidden or 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,6 @@ | ||
| --- | ||
| "@khanacademy/wonder-blocks-date-picker": minor | ||
| "@khanacademy/wb-dev-build-settings": minor | ||
| --- | ||
|
|
||
| Add new Date Picker package and related dev settings |
183 changes: 183 additions & 0 deletions
183
__docs__/wonder-blocks-date-picker/date-picker-input.stories.tsx
This file contains hidden or 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,183 @@ | ||
| import {Temporal} from "temporal-polyfill"; | ||
| import * as React from "react"; | ||
| import {enUS} from "react-day-picker/locale"; | ||
| import type {CustomModifiers} from "@khanacademy/wonder-blocks-date-picker"; | ||
|
|
||
| import {View} from "@khanacademy/wonder-blocks-core"; | ||
| import {BodyText} from "@khanacademy/wonder-blocks-typography"; | ||
|
|
||
| import { | ||
| DatePickerInput, | ||
| TemporalLocaleUtils, | ||
| } from "@khanacademy/wonder-blocks-date-picker"; | ||
| import ComponentInfo from "../components/component-info"; | ||
| import packageConfig from "../../packages/wonder-blocks-date-picker/package.json"; | ||
|
|
||
| type StoryArgs = React.ComponentProps<typeof DatePickerInput>; | ||
|
|
||
| // A wrapper component that handles state management for the stories | ||
| const DateInputWrapper = (props: StoryArgs) => { | ||
marcysutton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const [isInvalid, setIsInvalid] = React.useState(false); | ||
| const [selectedDate, setSelectedDate] = React.useState< | ||
| Date | null | undefined | ||
| >(() => { | ||
| if (props.value && typeof props.value === "string") { | ||
| try { | ||
| const locale = enUS.code; | ||
| const parsed = TemporalLocaleUtils.parseDate( | ||
| props.value, | ||
| props.dateFormat, | ||
| locale, | ||
| ); | ||
| return parsed | ||
| ? TemporalLocaleUtils.temporalDateToJsDate(parsed) | ||
| : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| const handleChange = ( | ||
| date: Date | null | undefined, | ||
| modifiers: Partial<CustomModifiers>, | ||
| ) => { | ||
| // We don't want to send back invalid dates. | ||
| if (!date || modifiers.disabled) { | ||
| setIsInvalid(true); | ||
| return; | ||
| } | ||
|
|
||
| setIsInvalid(false); | ||
| setSelectedDate(date); | ||
| }; | ||
| const locale = navigator.language || "en"; | ||
|
|
||
| const selectedDateAsValue = selectedDate | ||
| ? TemporalLocaleUtils.formatDate( | ||
| TemporalLocaleUtils.jsDateToTemporalDate(selectedDate), | ||
| props.dateFormat, | ||
| locale, | ||
| ) | ||
| : props.value; | ||
|
|
||
| return ( | ||
| <View> | ||
| <DatePickerInput | ||
| {...props} | ||
| onBlur={() => { | ||
| setSelectedDate(selectedDate); | ||
| }} | ||
| onChange={handleChange} | ||
| value={selectedDateAsValue} | ||
| /> | ||
| {isInvalid && <BodyText>Invalid date</BodyText>} | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const minDate = Temporal.Now.plainDateISO(); | ||
| const maxDate = minDate.add({days: 10}); | ||
|
|
||
| export default { | ||
| title: "Packages / Date Picker / DatePickerInput", | ||
| component: DatePickerInput, | ||
| parameters: { | ||
| componentSubtitle: ( | ||
| <ComponentInfo | ||
| name={packageConfig.name} | ||
| version={packageConfig.version} | ||
| /> | ||
| ), | ||
| }, | ||
| }; | ||
|
|
||
| export const SelectedDateIsNow = { | ||
| args: { | ||
| disabled: false, | ||
| value: TemporalLocaleUtils.formatDate( | ||
| Temporal.Now.plainDateISO(), | ||
| "MMMM D, YYYY", | ||
| "en-US", | ||
| ), | ||
| dateFormat: "MMMM D, YYYY", | ||
| parseDate: TemporalLocaleUtils.parseDateToJsDate, | ||
| getModifiersForDay: TemporalLocaleUtils.getModifiersForDay, | ||
| modifiers: { | ||
| selected: TemporalLocaleUtils.temporalDateToJsDate( | ||
| Temporal.Now.plainDateISO(), | ||
| ), | ||
| // We want to disable past dates and dates after 10 days from now | ||
| disabled: (date: Date) => { | ||
| const temporalDate = | ||
| TemporalLocaleUtils.jsDateToTemporalDate(date); | ||
|
|
||
| return ( | ||
| (minDate && | ||
| Temporal.PlainDate.compare(temporalDate, minDate) < | ||
| 0) || | ||
| (maxDate && | ||
| Temporal.PlainDate.compare(temporalDate, maxDate) > 0) | ||
| ); | ||
| }, | ||
| }, | ||
| }, | ||
| render: (args: StoryArgs) => <DateInputWrapper {...args} />, | ||
| }; | ||
|
|
||
| export const DisabledState = { | ||
| args: { | ||
| disabled: true, | ||
| value: "May 7, 2021", | ||
| parseDate: TemporalLocaleUtils.parseDateToJsDate, | ||
| dateFormat: "MMMM D, YYYY", | ||
| }, | ||
| chromatic: { | ||
| // Disabling because this is behavior is tested in TextField stories | ||
| disableSnapshot: true, | ||
| }, | ||
| render: (args: StoryArgs) => <DateInputWrapper {...args} />, | ||
| }; | ||
|
|
||
| export const InvalidDate = { | ||
| args: { | ||
| value: "May 7, 2024", | ||
| dateFormat: "MMMM D, YYYY", | ||
| parseDate: TemporalLocaleUtils.parseDateToJsDate, | ||
| getModifiersForDay: TemporalLocaleUtils.getModifiersForDay, | ||
| modifiers: { | ||
| selected: TemporalLocaleUtils.temporalDateToJsDate( | ||
| Temporal.Now.plainDateISO(), | ||
| ), | ||
| // We want to disable past dates and dates after 10 days from now | ||
| disabled: (date: Date) => { | ||
| const temporalDate = | ||
| TemporalLocaleUtils.jsDateToTemporalDate(date); | ||
| return ( | ||
| (minDate && | ||
| Temporal.PlainDate.compare(temporalDate, minDate) < | ||
| 0) || | ||
| (maxDate && | ||
| Temporal.PlainDate.compare(temporalDate, maxDate) > 0) | ||
| ); | ||
| }, | ||
| }, | ||
| }, | ||
| render: (args: StoryArgs) => <DateInputWrapper {...args} />, | ||
| }; | ||
|
|
||
| export const WithAriaLabel = { | ||
| args: { | ||
| "aria-label": "Choose a date", | ||
| value: "May 7, 2021", | ||
| dateFormat: "MMMM D, YYYY", | ||
| }, | ||
| parameters: { | ||
| chromatic: { | ||
| // Disabling because this doesn't test anything visual. | ||
| disableSnapshot: true, | ||
| }, | ||
| }, | ||
| render: (args: StoryArgs) => <DatePickerInput {...args} />, | ||
| }; | ||
This file contains hidden or 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,3 @@ | ||
| import type {ArgTypes} from "@storybook/react-vite"; | ||
|
|
||
| export default {} satisfies ArgTypes; |
Oops, something went wrong.
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.
suggestion: I don't think we need to expose the package internals as we only expect devs to use
DatePickerdirectly. These stories can be removed. Same withDatePickerOverlayand probably withMaybeNativeDatePicker(still TBD).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 call. I removed them!