Skip to content

Commit

Permalink
fix: Ensure date picker relative unit select has valid default value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster authored Oct 11, 2024
1 parent 6f2be48 commit 50a9c9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ describe('Date range picker', () => {
expect(getCustomRelativeRangeUnits(wrapper)).toEqual(['hours', 'minutes']);
});

test('ensures default unit is a valid one specified from list', () => {
const { wrapper } = renderDateRangePicker({
...defaultProps,
rangeSelectorMode: 'relative-only',
relativeOptions: [],
customRelativeRangeUnits: ['hour', 'day'],
});
wrapper.findTrigger().click();
expect(wrapper.findDropdown()!.findCustomRelativeRangeUnit()!.getElement()).toHaveTextContent('hours');
});

test('warns about (and ignores) invalid custom units', () => {
const { wrapper } = renderDateRangePicker({
...defaultProps,
Expand Down
19 changes: 11 additions & 8 deletions src/date-range-picker/relative-range/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ export default function RelativeRangePicker({
return NaN;
});

const initialCustomTimeUnit = dateOnly ? 'day' : 'minute';
const [customUnitOfTime, setCustomUnitOfTime] = useState<DateRangePickerProps.TimeUnit>(
initialRange?.unit ?? initialCustomTimeUnit
);

const showRadioControl = clientOptions.length > 0;
const showCustomControls = clientOptions.length === 0 || selectedRadio === CUSTOM_OPTION_SELECT_KEY;

let finalUnits = dateOnly ? dayUnits : units;
if (customUnits) {
finalUnits = customUnits.filter(unit => {
Expand All @@ -110,6 +102,17 @@ export default function RelativeRangePicker({
});
}

let initialCustomTimeUnit: DateRangePickerProps.TimeUnit = dateOnly ? 'day' : 'minute';
if (!finalUnits.includes(initialCustomTimeUnit)) {
initialCustomTimeUnit = finalUnits[0];
}
const [customUnitOfTime, setCustomUnitOfTime] = useState<DateRangePickerProps.TimeUnit>(
initialRange?.unit ?? initialCustomTimeUnit
);

const showRadioControl = clientOptions.length > 0;
const showCustomControls = clientOptions.length === 0 || selectedRadio === CUSTOM_OPTION_SELECT_KEY;

return (
<div>
<InternalSpaceBetween size="xs" direction="vertical">
Expand Down

0 comments on commit 50a9c9e

Please sign in to comment.