-
Notifications
You must be signed in to change notification settings - Fork 9
feat(RelativeRangeDatePicker): allow null values in relative range presets #188
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
ec14359
64ce7f9
48e74cc
502ee3a
0807e6a
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 |
|---|---|---|
|
|
@@ -17,11 +17,12 @@ const b = block('relative-range-date-picker-presets'); | |
|
|
||
| export interface PresetProps { | ||
| className?: string; | ||
| onChoosePreset: (start: string, end: string) => void; | ||
| onChoosePreset: (start: string | null, end: string | null) => void; | ||
| withTime?: boolean; | ||
| minValue?: DateTime; | ||
| size?: 's' | 'm' | 'l' | 'xl'; | ||
| presetTabs?: PresetTab[]; | ||
| allowNullableValues?: boolean; | ||
| } | ||
| export function Presets({ | ||
| className, | ||
|
|
@@ -30,9 +31,13 @@ export function Presets({ | |
| withTime, | ||
| onChoosePreset, | ||
| presetTabs, | ||
| allowNullableValues, | ||
| }: PresetProps) { | ||
| const tabs = React.useMemo(() => { | ||
| return filterPresetTabs(presetTabs ?? getDefaultPresetTabs({withTime}), {minValue}); | ||
| return filterPresetTabs( | ||
| presetTabs ?? getDefaultPresetTabs({withTime, allowNullableValues}), | ||
| {minValue, allowNullableValues}, | ||
| ); | ||
| }, [withTime, minValue, presetTabs]); | ||
|
|
||
| const [activeTabId, setActiveTab] = React.useState(tabs[0]?.id); | ||
|
|
@@ -83,7 +88,7 @@ export const SIZE_TO_ITEM_HEIGHT = { | |
| interface PresetsListProps { | ||
| size?: 's' | 'm' | 'l' | 'xl'; | ||
| presets: Preset[]; | ||
| onChoosePreset: (start: string, end: string) => void; | ||
| onChoosePreset: (start: string | null, end: string | null) => void; | ||
| } | ||
| function PresetsList({presets, onChoosePreset, size = 'm'}: PresetsListProps) { | ||
| const ref = React.useRef<List<Preset>>(null); | ||
|
|
@@ -122,7 +127,7 @@ function PresetsList({presets, onChoosePreset, size = 'm'}: PresetsListProps) { | |
| renderItem={(item) => item.title} | ||
| itemHeight={SIZE_TO_ITEM_HEIGHT[size]} | ||
| onItemClick={(item) => { | ||
| onChoosePreset(item.from, item.to); | ||
| onChoosePreset(item?.from, item?.to); | ||
|
||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,18 +32,28 @@ const countUnit = { | |||||||||||||||||||||||||
| y: 'Last {count} year', | ||||||||||||||||||||||||||
| } as const; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export function getPresetTitle(start: string, end: string, presets: Preset[] = allPresets) { | ||||||||||||||||||||||||||
| const startText = start.replace(/\s+/g, ''); | ||||||||||||||||||||||||||
| const endText = end.replace(/\s+/g, ''); | ||||||||||||||||||||||||||
| export function getPresetTitle( | ||||||||||||||||||||||||||
ValeraS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| start?: string | null, | ||||||||||||||||||||||||||
| end?: string | null, | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| start?: string | null, | |
| end?: string | null, | |
| start: string | null, | |
| end: string | null, |
Outdated
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.
| if (!start) { | |
| return `${i18n('To')}: ${endText}`; | |
| } | |
| if (!end) { | |
| return `${i18n('From')}: ${startText}`; | |
| } | |
| if (!startText) { | |
| return `${i18n('To')}: ${endText}`; | |
| } | |
| if (!endText) { | |
| return `${i18n('From')}: ${startText}`; | |
| } |
Outdated
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.
| if (end === 'now') { | |
| const match = lastRe.exec(startText); | |
| const match = lastRe.exec(startText || ''); | |
| if (endText === 'now') { | |
| const match = lastRe.exec(startText); |
ValeraS marked this conversation as resolved.
Show resolved
Hide resolved
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.
Do we need to filter DEFAULT_OTHERS_PRESETS with allowNullableValues?
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,13 +31,39 @@ interface GetDefaultTitleArgs { | |||||||||||||||||||||||||||||||||||||||||||
| value: RangeValue<Value | null> | null; | ||||||||||||||||||||||||||||||||||||||||||||
| timeZone: string; | ||||||||||||||||||||||||||||||||||||||||||||
| alwaysShowAsAbsolute?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||
| allowNullableValues?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||
| format?: string; | ||||||||||||||||||||||||||||||||||||||||||||
| presets?: Preset[]; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const isPresetValue = (value: RangeValue<Value | null> | null, allowNullableValues?: boolean) => { | ||||||||||||||||||||||||||||||||||||||||||||
| if (!value || value.start?.type === 'absolute' || value.end?.type === 'absolute') { | ||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (!allowNullableValues && (value.start === null || value.end === null)) { | ||||||||||||||||||||||||||||||||||||||||||||
| // we can't get here with no nullable values allowed but just in case... | ||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| let start, end; | ||||||||||||||||||||||||||||||||||||||||||||
| if (value.start === null) { | ||||||||||||||||||||||||||||||||||||||||||||
| start = null; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (value.start?.type === 'relative') { | ||||||||||||||||||||||||||||||||||||||||||||
| start = value.start?.value || ''; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (value.end === null) { | ||||||||||||||||||||||||||||||||||||||||||||
| end = null; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (value.end?.type === 'relative') { | ||||||||||||||||||||||||||||||||||||||||||||
| end = value.end?.value || ''; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| let start, end; | |
| if (value.start === null) { | |
| start = null; | |
| } | |
| if (value.start?.type === 'relative') { | |
| start = value.start?.value || ''; | |
| } | |
| if (value.end === null) { | |
| end = null; | |
| } | |
| if (value.end?.type === 'relative') { | |
| end = value.end?.value || ''; | |
| } | |
| let start = null; | |
| let end = null; | |
| if (value.start?.type === 'relative') { | |
| start = value.start.value; | |
| } | |
| if (value.end?.type === 'relative') { | |
| end = value.end.value; | |
| } |
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.
Why are you trying to mess with arguments of the default story? If you want to show example with custom presets, you can do it via
argTypesor another story.