Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import type {Value} from '../../RelativeDatePicker';
import {RelativeRangeDatePicker} from '../RelativeRangeDatePicker';
import type {RelativeRangeDatePickerProps} from '../types';

const DEFAULT_RANGE_DATE_PICKER_PRESET: RelativeRangeDatePickerProps['presetTabs'] = [
{
id: 'main',
title: 'Presets',
presets: [
{
from: 'now',
to: 'now+30d',
title: '30 days',
},
{
from: 'now',
to: null,
title: 'Unlimited',
},
],
},
];

const meta: Meta<typeof RelativeRangeDatePicker> = {
title: 'Components/RelativeRangeDatePicker',
component: RelativeRangeDatePicker,
Expand Down Expand Up @@ -43,8 +62,19 @@ export const Default = {
const timeZone = props.timeZone;
const minValue = props.minValue ? dateTimeParse(props.minValue, {timeZone}) : undefined;
const maxValue = props.maxValue ? dateTimeParse(props.maxValue, {timeZone}) : undefined;
let presetTabs;
if (props.withPresets) {
presetTabs = DEFAULT_RANGE_DATE_PICKER_PRESET;
}

return <RelativeRangeDatePicker {...props} minValue={minValue} maxValue={maxValue} />;
return (
<RelativeRangeDatePicker
{...props}
minValue={minValue}
maxValue={maxValue}
presetTabs={presetTabs}
/>
);
},
args: {
onUpdate: (res, timeZone) => {
Expand Down Expand Up @@ -88,6 +118,16 @@ export const Default = {
},
},
timeZone: timeZoneControl,
allowNullableValues: {
control: {
type: 'boolean',
},
},
withPresets: {
control: {
type: 'boolean',
},
},
},
} satisfies Story;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const Control = React.forwardRef<HTMLInputElement, ControlProps>(
},
ref,
) => {
const {alwaysShowAsAbsolute, presetTabs, getRangeTitle} = props;
const {alwaysShowAsAbsolute, presetTabs, getRangeTitle, allowNullableValues} = props;
const format = props.format || 'L';

const text = React.useMemo(
Expand All @@ -55,6 +55,7 @@ export const Control = React.forwardRef<HTMLInputElement, ControlProps>(
value: state.value,
timeZone: state.timeZone,
alwaysShowAsAbsolute,
allowNullableValues,
format,
presets: presetTabs?.flatMap(({presets}) => presets),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ interface PresetsDocProps {
className?: string;
size?: 's' | 'm' | 'l' | 'xl';
docs?: Preset[];
onStartUpdate: (start: string) => void;
onEndUpdate: (end: string) => void;
onStartUpdate: (start: string | null) => void;
onEndUpdate: (end: string | null) => void;
}

export function PickerDoc({docs = data, ...props}: PresetsDocProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ export function PickerForm(
size={props.size}
docs={props.docs}
onStartUpdate={(start) => {
state.setStart({type: 'relative', value: start});
state.setStart(
start === null ? null : {type: 'relative', value: start},
);
}}
onEndUpdate={(end) => {
state.setEnd({type: 'relative', value: end});
state.setEnd(end === null ? null : {type: 'relative', value: end});
}}
/>
</div>
Expand Down Expand Up @@ -137,14 +139,15 @@ export function PickerForm(
presetTabs={props.presetTabs}
onChoosePreset={(start, end) => {
state.setRange(
{type: 'relative', value: start},
{type: 'relative', value: end},
start === null ? null : {type: 'relative', value: start},
end === null ? null : {type: 'relative', value: end},
);
if (!props.withApplyButton) {
props.onApply();
}
}}
minValue={props.minValue}
allowNullableValues={props.allowNullableValues}
className={b('presets')}
/>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function useRelativeRangeDatePickerDialogState(props: PickerFormProps) {
}
}

function setRange(newStart: Value, newEnd: Value) {
function setRange(newStart: Value | null, newEnd: Value | null) {
if (props.readOnly) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {i18n} from './i18n';

export interface Preset {
from: string;
to: string;
from: string | null;
to: string | null;
title: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ 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(
start: string | null,
end: string | null,
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 (!startText) {
return `${i18n('To')}: ${endText}`;
}
if (!endText) {
return `${i18n('From')}: ${startText}`;
}

if (end === 'now') {
if (endText === 'now') {
const match = lastRe.exec(startText);
if (match) {
const [, count, unit] = match;
Expand All @@ -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)) {
return false;
}

if (minValue && from.isBefore(minValue)) {
if (minValue && from?.isBefore(minValue)) {
return false;
}

Expand All @@ -90,9 +104,11 @@ export interface PresetTab {
export function getDefaultPresetTabs({
withTime,
minValue,
allowNullableValues,
}: {
minValue?: DateTime;
withTime?: boolean;
allowNullableValues?: boolean;
}) {
const tabs: PresetTab[] = [];

Expand All @@ -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);
Copy link
Collaborator

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?


if (mainTab.presets.length > 0) {
tabs.push(mainTab);
Expand All @@ -114,7 +130,7 @@ export function getDefaultPresetTabs({
const otherTab: PresetTab = {
id: 'other',
title: i18n('Other'),
presets: filterPresets(DEFAULT_OTHERS_PRESETS, minValue),
presets: filterPresets(DEFAULT_OTHERS_PRESETS, minValue, allowNullableValues),
};

if (otherTab.presets.length > 0) {
Expand All @@ -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,
Expand Down
30 changes: 24 additions & 6 deletions src/components/RelativeRangeDatePicker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,34 @@ 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 = null;
let end = null;
if (value.start?.type === 'relative') {
start = value.start.value;
}
if (value.end?.type === 'relative') {
end = value.end.value;
}
return {start, end};
};
export function getDefaultTitle({
value,
timeZone,
alwaysShowAsAbsolute,
allowNullableValues,
format = 'L',
presets,
}: GetDefaultTitleArgs) {
Expand All @@ -62,12 +83,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 = ' — ';
Expand Down