Skip to content
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

fix: Don't pass through form field context to inputs in date range picker dropdown #1585

Merged
merged 1 commit into from
Oct 2, 2023
Merged
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
39 changes: 39 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,44 @@ 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', () => {
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
49 changes: 26 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,31 @@ 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}
/>
)}
{/* Reset form field context to prevent a wrapper form field from labelling all inputs inside the dropdown. */}
<FormFieldContext.Provider value={{}}>
avinashbot marked this conversation as resolved.
Show resolved Hide resolved
{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
Loading