Skip to content

Commit

Permalink
[EuiDatePicker] Fix clear button appearing when input is disabled (#8020
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cee-chen authored Sep 13, 2024
1 parent 6124602 commit 8bcab2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/eui/changelogs/upcoming/8020.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiDatePicker`'s `onClear` button to not appear when the input is `disabled`
19 changes: 19 additions & 0 deletions packages/eui/src/components/date_picker/date_picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ describe('EuiDatePicker', () => {
jest.restoreAllMocks();
});

test('onClear', () => {
const onClear = () => {};
const selected = moment();

const { queryByLabelText, rerender } = render(
<EuiDatePicker onClear={onClear} selected={selected} />
);
// Should render the clear button
expect(queryByLabelText('Clear input')).toBeInTheDocument();

// Should not render the clear button if the input is disabled
rerender(<EuiDatePicker onClear={onClear} selected={selected} disabled />);
expect(queryByLabelText('Clear input')).not.toBeInTheDocument();

// Should not render the clear button if no date is selected
rerender(<EuiDatePicker onClear={onClear} />);
expect(queryByLabelText('Clear input')).not.toBeInTheDocument();
});

test('compressed', () => {
const { container } = render(<EuiDatePicker compressed />);
// TODO: Should probably be a visual snapshot test
Expand Down
4 changes: 3 additions & 1 deletion packages/eui/src/components/date_picker/date_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ export const EuiDatePicker: FunctionComponent<EuiDatePickerProps> = ({
<span css={cssStyles} className={classes}>
<EuiFormControlLayout
icon={optionalIcon}
clear={selected && onClear ? { onClick: onClear } : undefined}
clear={
selected && !disabled && onClear ? { onClick: onClear } : undefined
}
isLoading={isLoading}
isInvalid={isInvalid}
isDisabled={disabled}
Expand Down

0 comments on commit 8bcab2a

Please sign in to comment.