Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -44,7 +63,16 @@ export const Default = {
const minValue = props.minValue ? dateTimeParse(props.minValue, {timeZone}) : undefined;
const maxValue = props.maxValue ? dateTimeParse(props.maxValue, {timeZone}) : undefined;

return <RelativeRangeDatePicker {...props} minValue={minValue} maxValue={maxValue} />;
return (
<RelativeRangeDatePicker
{...props}
minValue={minValue}
maxValue={maxValue}
allowNullableValues
withPresets
presetTabs={DEFAULT_RANGE_DATE_PICKER_PRESET}
/>
);
},
args: {
onUpdate: (res, timeZone) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function DocContent({size, docs, onStartUpdate, onEndUpdate}: DocContentProps) {
size={isMobile ? 'l' : getButtonSizeForInput(size)}
onClick={() => onStartUpdate(item.from)}
>
{item.from}
{item?.from}
</Button>
),
},
Expand All @@ -90,7 +90,7 @@ function DocContent({size, docs, onStartUpdate, onEndUpdate}: DocContentProps) {
template: (item) => (
<Button
size={isMobile ? 'l' : getButtonSizeForInput(size)}
onClick={() => onEndUpdate(item.to)}
onClick={() => onEndUpdate(item?.to)}
>
{item.to}
</Button>
Expand Down 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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 Expand Up @@ -122,7 +122,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need optional chaining here?

}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ 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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
start?: string | null,
end?: string | null,
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) {
Expand All @@ -43,7 +47,7 @@ export function getPresetTitle(start: string, end: string, presets: Preset[] = a
}

if (end === 'now') {
const match = lastRe.exec(startText);
const match = lastRe.exec(startText || '');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 || '');
if (endText === 'now') {
const match = lastRe.exec(startText);

if (match) {
const [, count, unit] = match;
if (isDateUnit(unit)) {
Expand All @@ -65,15 +69,11 @@ export function filterPresets(presets: Preset[], minValue?: DateTime) {
const from = dateTimeParse(preset.from);
const to = dateTimeParse(preset.to, {roundUp: true});

if (!from || !to) {
if (to?.isBefore(from)) {
return false;
}

if (to.isBefore(from)) {
return false;
}

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

Expand Down
29 changes: 23 additions & 6 deletions src/components/RelativeRangeDatePicker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ interface GetDefaultTitleArgs {
format?: string;
presets?: Preset[];
}

const isPresetValue = (value: RangeValue<Value | null> | null) => {
if (!value) {
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 || '';
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}

return {start, end};
};
export function getDefaultTitle({
value,
timeZone,
Expand Down Expand Up @@ -62,12 +82,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);
if (!alwaysShowAsAbsolute && presetSearch) {
return `${getPresetTitle(presetSearch.start, presetSearch.end, presets)}`;
}

const delimiter = ' — ';
Expand Down
Loading