-
Notifications
You must be signed in to change notification settings - Fork 72
[LG-5538] feat(time-input): Parse time #3378
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
Changes from all commits
bf86e07
612b474
a578ecb
804cd88
206e145
9715407
dad7b62
f559593
3e6f6be
1afe57e
cb84996
2547fdc
94ed98f
2d2fbd5
aa4e7a2
d38ee94
d57c109
aff3376
9e343ff
346cb74
31dba0b
388339e
5455489
237c4ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export const VERSION = '5.1.0'; | ||
| export const VERSION = '5.2.0'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,52 @@ | ||
| import React from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import { type StoryMetaType } from '@lg-tools/storybook-utils'; | ||
| import { StoryFn } from '@storybook/react'; | ||
|
|
||
| import { DateType, SupportedLocales } from '@leafygreen-ui/date-utils'; | ||
|
|
||
| import { TimeInput } from '.'; | ||
|
|
||
| const meta: StoryMetaType<typeof TimeInput> = { | ||
| title: 'Components/Inputs/TimeInput', | ||
| component: TimeInput, | ||
| parameters: { | ||
| default: 'LiveExample', | ||
| controls: { | ||
| exclude: [ | ||
| 'handleValidation', | ||
| 'initialValue', | ||
| 'onChange', | ||
| 'onDateChange', | ||
| 'onSegmentChange', | ||
| 'value', | ||
| 'onTimeChange', | ||
| ], | ||
| }, | ||
| }, | ||
| args: { | ||
| showSeconds: true, | ||
| locale: SupportedLocales.ISO_8601, | ||
| timeZone: 'UTC', | ||
| }, | ||
| argTypes: { | ||
| locale: { control: 'select', options: Object.values(SupportedLocales) }, | ||
| timeZone: { | ||
| control: 'select', | ||
| options: [undefined, 'UTC', 'America/New_York', 'Europe/London'], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| const Template: StoryFn<typeof TimeInput> = props => <TimeInput {...props} />; | ||
| const Template: StoryFn<typeof TimeInput> = props => { | ||
| const [value, setValue] = useState<DateType | undefined>( | ||
| new Date('1990-02-20T14:30:50Z'), | ||
| ); | ||
|
|
||
| return ( | ||
| <TimeInput {...props} value={value} onTimeChange={time => setValue(time)} /> | ||
| ); | ||
| }; | ||
|
|
||
| export const LiveExample = Template.bind({}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| import { DateTimeParts } from './shared.types'; | ||
|
|
||
| export const unitOptions = [ | ||
| { displayName: 'AM', value: 'AM' }, | ||
| { displayName: 'PM', value: 'PM' }, | ||
| ]; | ||
|
|
||
| export const defaultDateTimeParts: DateTimeParts = { | ||
| hour: '', | ||
| minute: '', | ||
| second: '', | ||
| month: '', | ||
| day: '', | ||
| year: '', | ||
| dayPeriod: 'AM', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export const DateTimePartKeys = { | ||
| hour: 'hour', | ||
| minute: 'minute', | ||
| second: 'second', | ||
| month: 'month', | ||
| day: 'day', | ||
| year: 'year', | ||
| dayPeriod: 'dayPeriod', | ||
| } as const; | ||
|
|
||
| export type DateTimePartKeys = | ||
| (typeof DateTimePartKeys)[keyof typeof DateTimePartKeys]; | ||
|
|
||
| export type DateTimeParts = Record<DateTimePartKeys, string>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { getFormatParts } from './getFormatParts'; | ||
|
|
||
| describe('packages/time-input/utils/getFormatParts', () => { | ||
| test('returns the correct format parts without seconds', () => { | ||
| const formatParts = getFormatParts({}); | ||
| expect(formatParts).toEqual([ | ||
| { type: 'hour', value: '' }, | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'minute', value: '' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('returns the correct format parts with seconds', () => { | ||
| const formatParts = getFormatParts({ showSeconds: true }); | ||
| expect(formatParts).toEqual([ | ||
| { type: 'hour', value: '' }, | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'minute', value: '' }, | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'second', value: '' }, | ||
| ]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * Returns an array of {@link Intl.DateTimeFormatPart} for the provided locale. | ||
| * | ||
| * Filters out the dayPeriod and the empty literal before it | ||
| * since they are not part of the time format parts. | ||
| * | ||
| * This will return `:` for every literal part regardless of the locale. | ||
| * | ||
| * @param showSeconds - Whether to show seconds | ||
| * @returns The format parts | ||
| * | ||
| * @example | ||
| * | ||
| * ```js | ||
| * getFormatParts({ showSeconds: true }); | ||
| * | ||
| * // [ | ||
| * // { type: 'hour', value: '' }, | ||
| * // { type: 'literal', value: ':' }, | ||
| * // { type: 'minute', value: '' }, | ||
| * // { type: 'literal', value: ':' }, | ||
| * // { type: 'second', value: '' }, | ||
| * // ] | ||
| */ | ||
| export const getFormatParts = ({ | ||
| showSeconds = false, | ||
| }: { | ||
| showSeconds?: boolean; | ||
| }): Array<Intl.DateTimeFormatPart> | undefined => { | ||
| const formatParts: Array<Intl.DateTimeFormatPart> = [ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any formats that we support that have a different order? Will we need to use the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my research, I don't think the order will change. The only thing that can change is that some locales don't use
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm seems okay since these are our Although it is a bit sus that our list is so short... I would expect us to have much more supported locales. Is this the latest list based off product requirements?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TheSonOfThomp do you remember why we only support these? My guess is that we would have to add tests for every locale, and there are hundreds of locales?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll merge this in and can address it in a new PR if we end up wanting to make changes. |
||
| { type: 'hour', value: '' }, | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'minute', value: '' }, | ||
| ...(showSeconds | ||
| ? ([ | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'second', value: '' }, | ||
| ] as Array<Intl.DateTimeFormatPart>) | ||
| : []), | ||
| ]; | ||
|
|
||
| return formatParts; | ||
| }; | ||
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.
nit: we could add barrel files to clean up this line and the imports in the following lines
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.
Ended up updating this in the last PR in this train