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

[EuiSearchBar][FieldValueSelectionFilter] Fix autoClose: false to always be respected regardless of multiSelect #7806

Merged
merged 4 commits into from
Jun 4, 2024
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
3 changes: 3 additions & 0 deletions packages/eui/changelogs/upcoming/7806.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiSearchBar`'s filter configs to always respect `autoClose: false`
2 changes: 1 addition & 1 deletion packages/eui/src-docs/src/views/search_bar/props_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export const propsInfo = {
},
autoClose: {
description:
'Should the dropdown close after the user selects a value. Ignored if multiSelect is true.',
'Should the dropdown close after the user selects a value. If not explicitly passed, will auto-close for single selection and remain open for multi-selection.',
required: false,
defaultValue: { value: 'true' },
type: { name: 'boolean' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,119 @@ describe('FieldValueSelectionFilter', () => {
});
});

describe('auto-close testing', () => {
const FieldValueSelectionFilterWithState = ({
autoClose,
multiSelect,
}: {
autoClose: undefined | boolean;
multiSelect: 'or' | boolean;
}) => {
const [query, setQuery] = useState(Query.parse(''));
const onChange = (newQuery: Query) => setQuery(newQuery);

const props: FieldValueSelectionFilterProps = {
...requiredProps,
index: 0,
onChange,
query,
config: {
type: 'field_value_selection',
field: 'tag',
name: 'Tag',
multiSelect,
autoClose,
options: staticOptions,
},
};

return <FieldValueSelectionFilter {...props} />;
};
const selectFilter = () => {
// Open popover
cy.get('button').click();
cy.get('.euiPopover__panel').should('exist');

// Select filter option
cy.get('li[role="option"][title="feature"]')
.should('have.attr', 'aria-checked', 'false')
.click();
};

describe('undefined', () => {
it('multi select: does not close popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={undefined}
multiSelect={true}
/>
);
selectFilter();
cy.get('.euiPopover__panel').should('exist');
});

it('single select: closes popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={undefined}
multiSelect={false}
/>
);
selectFilter();
cy.get('.euiPopover__panel').should('not.exist');
});
});

describe('false', () => {
it('multi select: never closes popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={false}
multiSelect={true}
/>
);
selectFilter();
cy.get('.euiPopover__panel').should('exist');
});

it('single select: never closes popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={false}
multiSelect={false}
/>
);
selectFilter();
cy.get('.euiPopover__panel').should('exist');
});
});

describe('true', () => {
it('multi select: always closes popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={true}
multiSelect={true}
/>
);
selectFilter();
cy.get('.euiPopover__panel').should('not.exist');
});

it('single select: always closes popover', () => {
cy.mount(
<FieldValueSelectionFilterWithState
autoClose={true}
multiSelect={false}
/>
);

selectFilter();
cy.get('.euiPopover__panel').should('not.exist');
});
});
});

it('has inactive filters, field is global', () => {
const props: FieldValueSelectionFilterProps = {
...requiredProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,35 +252,37 @@ export class FieldValueSelectionFilter extends Component<
) {
const multiSelect = this.resolveMultiSelect();
const {
config: { autoClose = true, operator = Operator.EQ },
config: { autoClose, operator = Operator.EQ },
} = this.props;

// we're closing popover only if the user can only select one item... if the
// user can select more, we'll leave it open so she can continue selecting

if (!multiSelect && autoClose) {
// If the consumer explicitly sets `autoClose`, always defer to that.
// Otherwise, default to auto-closing for single selections and leaving the
// popover open for multi-select (so users can continue selecting options)
const shouldClosePopover = autoClose ?? !multiSelect;
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
if (shouldClosePopover) {
this.closePopover();
}

if (!multiSelect) {
const query = checked
? this.props.query
.removeSimpleFieldClauses(field)
.addSimpleFieldValue(field, value, true, operator)
: this.props.query.removeSimpleFieldClauses(field);

this.props.onChange(query);
} else if (multiSelect === 'or') {
const query = checked
? this.props.query.addOrFieldValue(field, value, true, operator)
: this.props.query.removeOrFieldValue(field, value);

this.props.onChange(query);
} else {
if (multiSelect === 'or') {
const query = checked
? this.props.query.addOrFieldValue(field, value, true, operator)
: this.props.query.removeOrFieldValue(field, value);

this.props.onChange(query);
} else {
const query = checked
? this.props.query.addSimpleFieldValue(field, value, true, operator)
: this.props.query.removeSimpleFieldValue(field, value);

this.props.onChange(query);
}
const query = checked
? this.props.query.addSimpleFieldValue(field, value, true, operator)
: this.props.query.removeSimpleFieldValue(field, value);

this.props.onChange(query);
}
}

Expand Down
Loading