-
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
Open
shaneeza
wants to merge
19
commits into
shaneeza/segment-logic-integration
Choose a base branch
from
LG-5538/segments-parse-time
base: shaneeza/segment-logic-integration
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bf86e07
feat(time-input): add utils for toggling select and formatparts
shaneeza 612b474
wip
shaneeza a578ecb
refactor(time-input): enhance time formatting utilities and improve d…
shaneeza 804cd88
refactor(time-input): improve time input handling and formatting logic
shaneeza 206e145
temp remove ts check
shaneeza 9715407
refactor(time-input): simplify getFormatParts function by removing lo…
shaneeza dad7b62
test(time-input): add unit tests for getFormatParts utility to verify…
shaneeza f559593
refactor(time-input): remove locale dependency from formatParts in Ti…
shaneeza 3e6f6be
feat(time-input): add default time parts and enhance time input conte…
shaneeza 1afe57e
refactor(time-input): rename shouldShowSelect to is12hFormat for clar…
shaneeza cb84996
refactor(time-input): clean up unused state and comments in TimeInput…
shaneeza 2547fdc
refactor(time-input): remove unused console logs and simplify time fo…
shaneeza 94ed98f
refactor(time-input): remove outdated TODO comment in TimeInputContex…
shaneeza 2d2fbd5
Merge branch 'shaneeza/segment-logic-integration' of github.com:mongo…
shaneeza aa4e7a2
feat(time-input): implement getNonLiteralTimeParts utility function w…
shaneeza d38ee94
refactor(time-input): rename defaultTimeParts to defaultDateTimeParts…
shaneeza d57c109
refactor(time-input): simplify import structure for getFormatPartsVal…
shaneeza aff3376
refactor(time-input): enhance getFormatter utility to conditionally i…
shaneeza 9e343ff
refactor(time-input): consolidate imports for utility functions in Ti…
shaneeza 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
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
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
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 |
|---|---|---|
| @@ -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({}); |
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
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
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 |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| import { TimeParts } from './shared.types'; | ||
|
|
||
| export const unitOptions = [ | ||
| { displayName: 'AM', value: 'AM' }, | ||
| { displayName: 'PM', value: 'PM' }, | ||
| ]; | ||
|
|
||
| export const defaultDateTimeParts: TimeParts = { | ||
| hour: '', | ||
| minute: '', | ||
| second: '', | ||
| month: '', | ||
| day: '', | ||
| year: '', | ||
| dayPeriod: 'AM', | ||
| }; |
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,13 @@ | ||
| export const TimePartKeys = { | ||
| hour: 'hour', | ||
| minute: 'minute', | ||
| second: 'second', | ||
| month: 'month', | ||
| day: 'day', | ||
| year: 'year', | ||
| dayPeriod: 'dayPeriod', | ||
| } as const; | ||
|
|
||
| export type TimePartKeys = (typeof TimePartKeys)[keyof typeof TimePartKeys]; | ||
|
|
||
| export type TimeParts = Record<TimePartKeys, string>; | ||
23 changes: 23 additions & 0 deletions
23
packages/time-input/src/utils/getFormatParts/getFormatParts.spec.ts
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,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: '' }, | ||
| ]); | ||
| }); | ||
| }); |
43 changes: 43 additions & 0 deletions
43
packages/time-input/src/utils/getFormatParts/getFormatParts.ts
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,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> = [ | ||
| { type: 'hour', value: '' }, | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'minute', value: '' }, | ||
| ...(showSeconds | ||
| ? ([ | ||
| { type: 'literal', value: ':' }, | ||
| { type: 'second', value: '' }, | ||
| ] as Array<Intl.DateTimeFormatPart>) | ||
| : []), | ||
| ]; | ||
|
|
||
| return formatParts; | ||
| }; |
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.