) => {
) => {
const inputSelector = '[role="combobox"]';
const optionSelector = (index: number) => `[role="option"]:nth-of-type(${index + 1})`;
+const clearIconSelector = '.fui-Combobox__clearIcon';
describe('TimePicker', () => {
[
@@ -143,6 +146,22 @@ describe('TimePicker', () => {
cy.get('#selected-time-text').should('have.text', '10:30');
});
});
+
+ describe('clearable', () => {
+ it(`${name} should clear input on clear icon click`, () => {
+ mount();
+ cy.get(inputSelector).click().realPress('Enter').get(inputSelector).should('have.value', '10:00');
+ cy.get(clearIconSelector).click().get(inputSelector).should('have.value', '');
+ cy.get('#selected-time-text').should('have.text', '');
+ });
+
+ it(`freeform ${name} should clear input on clear icon click`, () => {
+ mount();
+ cy.get(inputSelector).click().type('14{enter}').get(inputSelector).should('have.value', '14:00');
+ cy.get(clearIconSelector).click().get(inputSelector).should('have.value', '');
+ cy.get('#selected-time-text').should('have.text', '');
+ });
+ });
});
});
diff --git a/packages/react-components/react-timepicker-compat/src/components/TimePicker/useTimePicker.tsx b/packages/react-components/react-timepicker-compat/src/components/TimePicker/useTimePicker.tsx
index 2b74601cb9d3c..9fab065c81f0c 100644
--- a/packages/react-components/react-timepicker-compat/src/components/TimePicker/useTimePicker.tsx
+++ b/packages/react-components/react-timepicker-compat/src/components/TimePicker/useTimePicker.tsx
@@ -83,8 +83,13 @@ export const useTimePicker_unstable = (props: TimePickerProps, ref: React.Ref(null);
const handleOptionSelect: ComboboxProps['onOptionSelect'] = useEventCallback((e, data) => {
- if (freeform && data.optionValue === undefined) {
+ if (
+ freeform &&
+ data.optionValue === undefined &&
+ !(rest.clearable && e.type === 'click' && e.currentTarget === clearIconRef.current)
+ ) {
// Combobox clears selection when input value not matching any option; but we allow this case in freeform TimePicker.
return;
}
@@ -117,8 +122,15 @@ export const useTimePicker_unstable = (props: TimePickerProps, ref: React.Ref