Skip to content

Commit

Permalink
fix: Don't pass through form field context to inputs in date range pi…
Browse files Browse the repository at this point in the history
…cker dropdown
  • Loading branch information
avinashbot committed Sep 29, 2023
1 parent d8b04de commit 90cbf7b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
41 changes: 41 additions & 0 deletions src/date-range-picker/__tests__/date-range-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { act, render } from '@testing-library/react';
import Mockdate from 'mockdate';
import createWrapper from '../../../lib/components/test-utils/dom';
import DateRangePicker, { DateRangePickerProps } from '../../../lib/components/date-range-picker';
import FormField from '../../../lib/components/form-field';
import DateRangePickerWrapper from '../../../lib/components/test-utils/dom/date-range-picker';
import { NonCancelableEventHandler } from '../../../lib/components/internal/events';
import { i18nStrings } from './i18n-strings';
Expand Down Expand Up @@ -83,6 +84,46 @@ describe('Date range picker', () => {
const { wrapper } = renderDateRangePicker({ ...defaultProps, controlId: 'test' });
expect(wrapper.findTrigger().getElement()).toHaveAttribute('id', 'test');
});

test('does not pass through form field context to dropdown elements', () => {
// Rather than checking the exact strings, we can just check the number of aria-labelledbys that were concatenated.

const { container } = render(
<FormField label="Label">
<DateRangePicker {...defaultProps} />
</FormField>
);
const wrapper = createWrapper(container).findDateRangePicker()!;
act(() => wrapper.openDropdown());
const dropdown = wrapper.findDropdown()!;

expect(dropdown.findRelativeRangeRadioGroup()!.getElement()).toHaveAccessibleName(
i18nStrings.relativeRangeSelectionHeading
);

dropdown.findRelativeRangeRadioGroup()?.findButtons().at(-1)!.findNativeInput().click();
expect(dropdown.findCustomRelativeRangeDuration()!.findNativeInput().getElement()).toHaveAccessibleName(
i18nStrings.customRelativeRangeDurationLabel
);
expect(dropdown.findCustomRelativeRangeUnit()!.findTrigger().getElement()).toHaveAccessibleName(
[i18nStrings.customRelativeRangeUnitLabel, 'minutes'].join(' ')
);

changeMode(wrapper, 'absolute');

expect(dropdown.findStartDateInput()!.findNativeInput()!.getElement()).toHaveAccessibleName(
i18nStrings.startDateLabel
);
expect(dropdown.findStartTimeInput()!.findNativeInput()!.getElement()).toHaveAccessibleName(
i18nStrings.startTimeLabel
);
expect(dropdown.findEndDateInput()!.findNativeInput()!.getElement()).toHaveAccessibleName(
i18nStrings.endDateLabel
);
expect(dropdown.findEndTimeInput()!.findNativeInput()!.getElement()).toHaveAccessibleName(
i18nStrings.endTimeLabel
);
});
});

test('opens relative range mode by default', () => {
Expand Down
48 changes: 25 additions & 23 deletions src/date-range-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Dropdown from '../internal/components/dropdown';
import { useFocusTracker } from '../internal/hooks/use-focus-tracker';
import { useMobile } from '../internal/hooks/use-mobile';
import ButtonTrigger from '../internal/components/button-trigger';
import { useFormFieldContext } from '../internal/context/form-field-context';
import { FormFieldContext, useFormFieldContext } from '../internal/context/form-field-context';
import InternalIcon from '../icon/internal';
import { normalizeTimeOffset, shiftTimeOffset } from './time-offset';
import useBaseComponent from '../internal/hooks/use-base-component';
Expand Down Expand Up @@ -274,28 +274,30 @@ const DateRangePicker = React.forwardRef(
expandToViewport={expandToViewport}
dropdownId={dropdownId}
>
{isDropDownOpen && (
<DateRangePickerDropdown
startOfWeek={startOfWeek}
locale={normalizedLocale}
isSingleGrid={isSingleGrid}
onDropdownClose={() => closeDropdown(true)}
value={value}
showClearButton={showClearButton}
isDateEnabled={isDateEnabled}
i18nStrings={i18nStrings}
onClear={onClear}
onApply={onApply}
relativeOptions={relativeOptions}
isValidRange={isValidRange}
dateOnly={dateOnly}
timeInputFormat={timeInputFormat}
rangeSelectorMode={rangeSelectorMode}
ariaLabelledby={ariaLabelledby}
ariaDescribedby={ariaDescribedby}
customAbsoluteRangeControl={customAbsoluteRangeControl}
/>
)}
<FormFieldContext.Provider value={{}}>
{isDropDownOpen && (
<DateRangePickerDropdown
startOfWeek={startOfWeek}
locale={normalizedLocale}
isSingleGrid={isSingleGrid}
onDropdownClose={() => closeDropdown(true)}
value={value}
showClearButton={showClearButton}
isDateEnabled={isDateEnabled}
i18nStrings={i18nStrings}
onClear={onClear}
onApply={onApply}
relativeOptions={relativeOptions}
isValidRange={isValidRange}
dateOnly={dateOnly}
timeInputFormat={timeInputFormat}
rangeSelectorMode={rangeSelectorMode}
ariaLabelledby={ariaLabelledby}
ariaDescribedby={ariaDescribedby}
customAbsoluteRangeControl={customAbsoluteRangeControl}
/>
)}
</FormFieldContext.Provider>
</Dropdown>
</div>
);
Expand Down

0 comments on commit 90cbf7b

Please sign in to comment.