-
Notifications
You must be signed in to change notification settings - Fork 7
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); | ||
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. Why do you need optional chaining here? |
||
}} | ||
/> | ||
); | ||
|
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, | ||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||
presets: Preset[] = allPresets, | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
const startText = start?.replace(/\s+/g, '') ?? start; | ||||||||||||||||||||||||||
const endText = end?.replace(/\s+/g, '') ?? end; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (const preset of presets) { | ||||||||||||||||||||||||||
if (preset.from === startText && preset.to === endText) { | ||||||||||||||||||||||||||
return preset.title; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if (!start) { | ||||||||||||||||||||||||||
return `${i18n('To')}: ${endText}`; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if (!end) { | ||||||||||||||||||||||||||
return `${i18n('From')}: ${startText}`; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (end === 'now') { | ||||||||||||||||||||||||||
const match = lastRe.exec(startText); | ||||||||||||||||||||||||||
const match = lastRe.exec(startText || ''); | ||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||
if (match) { | ||||||||||||||||||||||||||
const [, count, unit] = match; | ||||||||||||||||||||||||||
if (isDateUnit(unit)) { | ||||||||||||||||||||||||||
|
@@ -60,20 +70,24 @@ function isDateUnit(value: string): value is 's' | 'm' | 'h' | 'd' | 'w' | 'M' | | |||||||||||||||||||||||||
return ['s', 'm', 'h', 'd', 'w', 'M', 'y'].includes(value); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export function filterPresets(presets: Preset[], minValue?: DateTime) { | ||||||||||||||||||||||||||
export function filterPresets( | ||||||||||||||||||||||||||
presets: Preset[], | ||||||||||||||||||||||||||
minValue?: DateTime, | ||||||||||||||||||||||||||
allowNullableValues?: boolean, | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
return presets.filter((preset) => { | ||||||||||||||||||||||||||
const from = dateTimeParse(preset.from); | ||||||||||||||||||||||||||
const to = dateTimeParse(preset.to, {roundUp: true}); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (!from || !to) { | ||||||||||||||||||||||||||
if (!allowNullableValues && (!from || !to)) { | ||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (to.isBefore(from)) { | ||||||||||||||||||||||||||
if (to?.isBefore(from)) { | ||||||||||||||||||||||||||
ValeraS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (minValue && from.isBefore(minValue)) { | ||||||||||||||||||||||||||
if (minValue && from?.isBefore(minValue)) { | ||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -90,9 +104,11 @@ export interface PresetTab { | |||||||||||||||||||||||||
export function getDefaultPresetTabs({ | ||||||||||||||||||||||||||
withTime, | ||||||||||||||||||||||||||
minValue, | ||||||||||||||||||||||||||
allowNullableValues, | ||||||||||||||||||||||||||
}: { | ||||||||||||||||||||||||||
minValue?: DateTime; | ||||||||||||||||||||||||||
withTime?: boolean; | ||||||||||||||||||||||||||
allowNullableValues?: boolean; | ||||||||||||||||||||||||||
}) { | ||||||||||||||||||||||||||
const tabs: PresetTab[] = []; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -105,7 +121,7 @@ export function getDefaultPresetTabs({ | |||||||||||||||||||||||||
if (withTime) { | ||||||||||||||||||||||||||
mainPresets.unshift(...DEFAULT_TIME_PRESETS); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
mainTab.presets = filterPresets(mainPresets, minValue); | ||||||||||||||||||||||||||
mainTab.presets = filterPresets(mainPresets, minValue, allowNullableValues); | ||||||||||||||||||||||||||
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. Do we need to filter |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (mainTab.presets.length > 0) { | ||||||||||||||||||||||||||
tabs.push(mainTab); | ||||||||||||||||||||||||||
|
@@ -124,9 +140,12 @@ export function getDefaultPresetTabs({ | |||||||||||||||||||||||||
return tabs; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export function filterPresetTabs(tabs: PresetTab[], {minValue}: {minValue?: DateTime} = {}) { | ||||||||||||||||||||||||||
export function filterPresetTabs( | ||||||||||||||||||||||||||
tabs: PresetTab[], | ||||||||||||||||||||||||||
{minValue, allowNullableValues}: {minValue?: DateTime; allowNullableValues?: boolean} = {}, | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
return tabs.reduce<PresetTab[]>((acc, tab) => { | ||||||||||||||||||||||||||
const presets = filterPresets(tab.presets, minValue); | ||||||||||||||||||||||||||
const presets = filterPresets(tab.presets, minValue, allowNullableValues); | ||||||||||||||||||||||||||
if (presets.length) { | ||||||||||||||||||||||||||
acc.push({ | ||||||||||||||||||||||||||
...tab, | ||||||||||||||||||||||||||
|
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 || ''; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
return {start, end}; | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
export function getDefaultTitle({ | ||||||||||||||||||||||||||||||||||||||||||||
value, | ||||||||||||||||||||||||||||||||||||||||||||
timeZone, | ||||||||||||||||||||||||||||||||||||||||||||
alwaysShowAsAbsolute, | ||||||||||||||||||||||||||||||||||||||||||||
allowNullableValues, | ||||||||||||||||||||||||||||||||||||||||||||
format = 'L', | ||||||||||||||||||||||||||||||||||||||||||||
presets, | ||||||||||||||||||||||||||||||||||||||||||||
}: GetDefaultTitleArgs) { | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -62,12 +88,9 @@ export function getDefaultTitle({ | |||||||||||||||||||||||||||||||||||||||||||
: dateTimeParse(value.end.value, {timeZone, roundUp: true})?.format(format) ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||
!alwaysShowAsAbsolute && | ||||||||||||||||||||||||||||||||||||||||||||
value.start?.type === 'relative' && | ||||||||||||||||||||||||||||||||||||||||||||
value.end?.type === 'relative' | ||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||
return `${getPresetTitle(value.start.value, value.end.value, presets)}`; | ||||||||||||||||||||||||||||||||||||||||||||
const presetSearch = isPresetValue(value, allowNullableValues); | ||||||||||||||||||||||||||||||||||||||||||||
if (!alwaysShowAsAbsolute && presetSearch) { | ||||||||||||||||||||||||||||||||||||||||||||
return `${getPresetTitle(presetSearch.start, presetSearch.end, presets)}`; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
const delimiter = ' — '; | ||||||||||||||||||||||||||||||||||||||||||||
|
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
argTypes
or another story.