Skip to content
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/9059.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiInMemoryTable` not firing the `search.onChange` callback when `searchFormat` equals `text`.
Original file line number Diff line number Diff line change
Expand Up @@ -1505,5 +1505,69 @@ describe('EuiInMemoryTable', () => {
expect(tableContent).toHaveLength(1); // only 1 match
expect(tableContent[0]).toHaveTextContent(specialCharacterSearch);
});

it('fires the onChange callback', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests! 🙏

const items = [{ title: 'Hello' }, { title: 'Goodbye' }];
const columns = [{ field: 'title', name: 'Title' }];
const TEXT = 'Hello World!';
const mockOnChange = jest.fn();

const { getByTestSubject, container } = render(
<EuiInMemoryTable
items={items}
searchFormat="text"
search={{
box: { incremental: true, 'data-test-subj': 'searchbox' },
onChange: mockOnChange,
}}
columns={columns}
/>
);

fireEvent.keyUp(getByTestSubject('searchbox'), {
target: { value: TEXT },
});

const tableContent = container.querySelectorAll(
'.euiTableRowCell .euiTableCellContent'
);
expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledWith({
query: Query.parse(`"${TEXT}"`),
queryText: TEXT,
error: null,
});
expect(tableContent).toHaveLength(items.length); // all items
});

it('fires the onChange callback but handles internal state too', () => {
const items = [{ title: 'foo' }, { title: 'bar' }, { title: 'baz' }];
const columns = [{ field: 'title', name: 'Title' }];
const TEXT = 'ba';
const mockOnChange = jest.fn();
mockOnChange.mockReturnValue(true);

const { getByTestSubject, container } = render(
<EuiInMemoryTable
items={items}
searchFormat="text"
search={{
box: { incremental: true, 'data-test-subj': 'searchbox' },
onChange: mockOnChange,
}}
columns={columns}
/>
);

fireEvent.keyUp(getByTestSubject('searchbox'), {
target: { value: TEXT },
});

expect(mockOnChange).toHaveBeenCalledTimes(1);
const tableContent = container.querySelectorAll(
'.euiTableRowCell .euiTableCellContent'
);
expect(tableContent).toHaveLength(2); // 2 matches for "ba"
});
});
});
15 changes: 15 additions & 0 deletions packages/eui/src/components/basic_table/in_memory_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,21 @@ export class EuiInMemoryTable<T extends object = object> extends Component<
onPlainTextSearch = (searchValue: string) => {
const escapedQueryText = searchValue.replace(/["\\]/g, '\\$&');
const finalQuery = `"${escapedQueryText}"`;
const { search } = this.props;

if (isEuiSearchBarProps(search)) {
if (search.onChange) {
const shouldQueryInMemory = search.onChange({
query: EuiSearchBar.Query.parse(finalQuery),
queryText: escapedQueryText,
error: null,
});
if (!shouldQueryInMemory) {
return;
}
}
}

this.setState({
query: EuiSearchBar.Query.parse(finalQuery),
});
Expand Down