mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-20 12:23:02 +00:00
feat(toolbar-search): add shouldFilterRows prop
This commit is contained in:
parent
a419307c91
commit
83c83ea3f2
1 changed files with 40 additions and 1 deletions
|
@ -16,6 +16,18 @@
|
||||||
/** Set to `true` to disable the search bar */
|
/** Set to `true` to disable the search bar */
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to `true` to filter table rows using the search value.
|
||||||
|
*
|
||||||
|
* If `true`, the default search excludes `id`, `cells` fields and
|
||||||
|
* only does a basic comparison on string and number type cell values.
|
||||||
|
*
|
||||||
|
* To implement your own client-side filtering, pass a function
|
||||||
|
* that accepts a row and value and returns a boolean.
|
||||||
|
* @type {boolean | ((rows: import("./DataTable.svelte").DataTableRow, value: number | string) => boolean)}
|
||||||
|
*/
|
||||||
|
export let shouldFilterRows = false;
|
||||||
|
|
||||||
/** Specify the tabindex */
|
/** Specify the tabindex */
|
||||||
export let tabindex = "0";
|
export let tabindex = "0";
|
||||||
|
|
||||||
|
@ -25,9 +37,36 @@
|
||||||
*/
|
*/
|
||||||
export let ref = null;
|
export let ref = null;
|
||||||
|
|
||||||
import { tick } from "svelte";
|
import { tick, getContext } from "svelte";
|
||||||
import Search from "../Search/Search.svelte";
|
import Search from "../Search/Search.svelte";
|
||||||
|
|
||||||
|
const { tableRows } = getContext("DataTable");
|
||||||
|
|
||||||
|
$: originalRows = tableRows ? [...$tableRows] : [];
|
||||||
|
$: if (shouldFilterRows) {
|
||||||
|
let rows = originalRows;
|
||||||
|
|
||||||
|
if (value.trim().length > 0) {
|
||||||
|
if (shouldFilterRows === true) {
|
||||||
|
rows = rows.filter((row) => {
|
||||||
|
return Object.entries(row)
|
||||||
|
.filter(([key]) => !["cells", "id"].includes(key))
|
||||||
|
.some(([key, _value]) => {
|
||||||
|
if (typeof _value === "string" || typeof _value === "number") {
|
||||||
|
return (_value + "")
|
||||||
|
?.toLowerCase()
|
||||||
|
.includes(value.trim().toLowerCase());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else if (typeof shouldFilterRows === "function") {
|
||||||
|
rows = rows.filter((row) => shouldFilterRows(row, value) ?? false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tableRows.set(rows);
|
||||||
|
}
|
||||||
|
|
||||||
async function expandSearch() {
|
async function expandSearch() {
|
||||||
if (disabled || persistent || expanded) return;
|
if (disabled || persistent || expanded) return;
|
||||||
expanded = true;
|
expanded = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue