diff --git a/apps/www/examples/shield-ts/assets.tsx b/apps/www/examples/shield-ts/assets.tsx index f1a8c791..cad8d2c2 100644 --- a/apps/www/examples/shield-ts/assets.tsx +++ b/apps/www/examples/shield-ts/assets.tsx @@ -3,6 +3,8 @@ import { Checkbox, DataTable, Flex, + Label, + Switch, Text, useTable, } from "@raystack/apsara"; @@ -133,15 +135,21 @@ export const columns: ApsaraColumnDef[] = [ ]; export const Assets = () => { + const [isLoading, setIsLoading] = React.useState(false); + + function onSwitchChange() { + setIsLoading((prev) => !prev); + } + return ( - // @ts-ignore - + @@ -151,12 +159,22 @@ export const Assets = () => { ); }; -const AssetsHeader = () => { +const AssetsHeader = ({ onSwitchChange }) => { const { filteredColumns, table } = useTable(); const isFiltered = filteredColumns.length > 0; return ( - - Assets + + + Assets + + + + + {isFiltered ? : } diff --git a/packages/raystack/table/datatable.tsx b/packages/raystack/table/datatable.tsx index e8f932b2..40b58f53 100644 --- a/packages/raystack/table/datatable.tsx +++ b/packages/raystack/table/datatable.tsx @@ -23,6 +23,7 @@ import { ReactElement, ReactNode, useEffect, + useMemo, useState, } from "react"; import { Flex } from "~/flex"; @@ -96,24 +97,33 @@ function DataTableRoot({ const { filteredColumns, addFilterColumn, removeFilterColumn, resetColumns } = useTableColumn(); - const columnWithCustomFilter = columns.map((col) => { - // @ts-ignore; - const colId: string = col.id || col?.accessorKey; - if (colId && tableCustomFilter.hasOwnProperty(colId)) { - col.filterFn = tableCustomFilter[colId]; - } + const columnWithCustomFilter = useMemo( + () => + columns.map((col) => { + const colId = col.id || (col?.accessorKey as string); + const filterFn = + colId && tableCustomFilter.hasOwnProperty(colId) + ? tableCustomFilter[colId] + : undefined; - col.cell = isLoading - ? () => ( - - ) - : col.cell; - return col; - }); + const cell = isLoading + ? () => ( + + ) + : col.cell; + + return { + ...col, + cell, + filterFn, + }; + }), + [isLoading, columns, tableCustomFilter] + ); useEffect(() => { if (onStateChange) {