fixes search input

This commit is contained in:
MinamiFunakoshiTR 2025-03-31 15:01:45 -07:00
parent a5b93e5f53
commit 9774939d08
Failed to extract signature
3 changed files with 48 additions and 29 deletions

View file

@ -6,15 +6,23 @@
interface Props {
/** The placeholder text that appears in the search box.*/
searchPlaceholder?: string;
onsearch?: (newValue: string) => void;
}
let { searchPlaceholder = 'Search...' }: Props = $props();
let { searchPlaceholder = 'Search...', onsearch }: Props = $props();
let value = $state('');
let active = $derived(value !== '');
function clear() {
value = '';
if (onsearch) onsearch(value); // Call the prop to update the parent when cleared
}
function oninput(event: Event) {
value = (event.target as HTMLInputElement).value;
if (onsearch) onsearch(value); // Update the parent on every input change
}
</script>
@ -22,12 +30,14 @@
<div class="search--icon absolute">
<MagnifyingGlass />
</div>
<input
id="search--input"
class="search--input body-caption pl-8"
type="text"
placeholder={searchPlaceholder}
bind:value
{oninput}
/>
<div
class="search--x absolute"
@ -72,4 +82,4 @@
}
}
}
</style>
</style>

View file

@ -1,5 +1,5 @@
<!-- @component `Table` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-text-elements-table--docs) -->
<script lang="ts" generics="T">
<script lang="ts">
/** Import local helpers */
import Block from '../Block/Block.svelte';
import Pagination from './components/Pagination.svelte';
@ -69,7 +69,7 @@
pageSize = 25,
searchable = false,
searchPlaceholder = 'Search in table',
filterField,
filterField = '',
filterLabel,
sortable = false,
sortField = Object.keys(data[0])[0],
@ -116,8 +116,16 @@
showAll = !showAll;
}
function handleSearchInput(event) {
searchText = event.detail.value;
// function handleSearchInput(event: CustomEvent<string>) {
// searchText = event.detail;
// console.log('searchText', searchText);
// pageNumber = 1;
// }
/** Filters table data based on the input value in the search bar */
function handleSearchInput(newSearchText: string) {
searchText = newSearchText;
pageNumber = 1;
}
@ -156,9 +164,21 @@
}
}
/** Add the `searchStr` field to data */
let searchableData = $derived.by(() => {
return data.map((d) => {
return {
...d,
searchStr: includedFields
.map((field) => d[field])
.join(' ')
.toLowerCase(),
};
});
});
/** Set up the data pipeline */
let filteredData = $derived.by(() =>
filterArray(data, searchText, filterField, filterValue)
filterArray(searchableData, searchText, filterField, filterValue)
);
let sortedData = $derived.by(() =>
@ -174,19 +194,6 @@
}
return sortedData;
});
/** Add the `searchStr` field to data */
let searchableData = $derived.by(() => {
return data.map((d) => {
return {
...d,
searchStr: includedFields
.map((field) => d[field])
.join(' ')
.toLowerCase(),
};
});
});
</script>
<Block {width} {id} class="fmy-6 {cls}">
@ -212,10 +219,7 @@
{/if}
{#if searchable}
<div class="table--header--search">
<SearchInput
bind:searchPlaceholder
on:search={handleSearchInput}
/>
<SearchInput {searchPlaceholder} onsearch={handleSearchInput} />
</div>
{/if}
</nav>

View file

@ -1,8 +1,12 @@
export function filterArray<T extends { searchStr: string }>(
data: T[],
type FilterableDatum<T extends Record<string, unknown>> = T & {
searchStr: string;
};
export function filterArray<T extends Record<string, unknown>>(
data: FilterableDatum<T>[],
searchText: string,
filterField: keyof T,
filterValue: T[keyof T]
filterField: keyof FilterableDatum<T>,
filterValue: FilterableDatum<T>[keyof FilterableDatum<T>]
) {
if (searchText) {
data = data.filter((item) => {
@ -12,13 +16,14 @@ export function filterArray<T extends { searchStr: string }>(
if (filterValue) {
data = data.filter((item) => {
if (!filterField) return true; // or handle the undefined case as appropriate
return item[filterField] === filterValue;
});
}
return data;
}
export function paginateArray<T>(
array: T[],
pageSize: number,