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
19 changes: 18 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,35 @@ function Table<T, ColumnKey extends string = string, FilterKey extends string =
}) as TableHandle<T, ColumnKey, FilterKey>;
});

const originalDataLength = data?.length ?? 0;

// Check if filters are applied (not default values)
const hasActiveFilters = filters
? (Object.keys(currentFilters) as FilterKey[]).some((key) => {
const filterValue = currentFilters[key];
const defaultValue = filters[key]?.default;
return filterValue !== defaultValue;
})
: false;

const hasSearchString = activeSearchString.trim().length > 0;
const isEmptyResult = processedData.length === 0 && originalDataLength > 0 && (hasSearchString || hasActiveFilters);

// eslint-disable-next-line react/jsx-no-constructed-context-values
const contextValue: TableContextValue<T, ColumnKey, FilterKey> = {
listRef,
listProps,
processedData,
originalDataLength: data?.length ?? 0,
originalDataLength,
columns,
filterConfig: filters,
activeFilters: currentFilters,
activeSorting,
activeSearchString,
tableMethods,
hasActiveFilters,
hasSearchString,
isEmptyResult,
};

return <TableContext.Provider value={contextValue as unknown as TableContextValue<unknown, string>}>{children}</TableContext.Provider>;
Expand Down
14 changes: 1 addition & 13 deletions src/components/Table/TableBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,9 @@ type TableBodyProps = ViewProps & {
function TableBody<T>({contentContainerStyle, ...props}: TableBodyProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {processedData: filteredAndSortedData, originalDataLength, activeSearchString, activeFilters, filterConfig, listProps} = useTableContext<T>();
const {processedData: filteredAndSortedData, activeSearchString, listProps, hasActiveFilters, hasSearchString, isEmptyResult} = useTableContext<T>();
const {ListEmptyComponent, contentContainerStyle: listContentContainerStyle, ...restListProps} = listProps ?? {};

// Check if filters are applied (not default values)
const hasActiveFilters = filterConfig
? Object.keys(activeFilters).some((key) => {
const filterValue = activeFilters[key];
const defaultValue = filterConfig?.[key]?.default;
return filterValue !== defaultValue;
})
: false;

const hasSearchString = activeSearchString.trim().length > 0;
const isEmptyResult = filteredAndSortedData.length === 0 && originalDataLength > 0 && (hasSearchString || hasActiveFilters);

// Determine the message based on what caused the empty result
const getEmptyMessage = () => {
if (hasSearchString) {
Expand Down
12 changes: 12 additions & 0 deletions src/components/Table/TableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ type TableContextValue<T, ColumnKey extends string = string, FilterKey extends s

/** Methods exposed by the Table component for programmatic control. */
tableMethods: TableMethods<ColumnKey, FilterKey>;

/** Whether any filters differ from their default values. */
hasActiveFilters: boolean;

/** Whether search string is not empty. */
hasSearchString: boolean;

/** Whether the table has an empty result caused by search or filters. */
isEmptyResult: boolean;
};

const defaultTableContextValue: TableContextValue<unknown, string> = {
Expand All @@ -57,6 +66,9 @@ const defaultTableContextValue: TableContextValue<unknown, string> = {
tableMethods: {} as TableMethods<string, string>,
filterConfig: undefined,
listProps: {} as SharedListProps<unknown>,
hasActiveFilters: false,
hasSearchString: false,
isEmptyResult: false,
};

const TableContext = createContext(defaultTableContextValue);
Expand Down
13 changes: 10 additions & 3 deletions src/components/Table/TableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const NUMBER_OF_TOGGLES_BEFORE_RESET = 2;
/**
* Props for the TableHeader component.
*/
type TableHeaderProps = ViewProps;
type TableHeaderProps = ViewProps & {
/** Hide table header when search returns no results. */
shouldHideHeaderWhenEmptySearch?: boolean;
};

/**
* Renders the table header row with sortable column headers.
Expand All @@ -45,9 +48,13 @@ type TableHeaderProps = ViewProps;
* </Table>
* ```
*/
function TableHeader<T, ColumnKey extends string = string>({style, ...props}: TableHeaderProps) {
function TableHeader<T, ColumnKey extends string = string>({style, shouldHideHeaderWhenEmptySearch = true, ...props}: TableHeaderProps) {
const styles = useThemeStyles();
const {columns} = useTableContext<T, ColumnKey>();
const {columns, isEmptyResult} = useTableContext<T, ColumnKey>();

if (shouldHideHeaderWhenEmptySearch && isEmptyResult) {
return null;
}

return (
<View
Expand Down
Loading