Skip to content
Open
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
10 changes: 8 additions & 2 deletions apps/www/src/components/datatable-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ export const columns: DataTableColumnDef<Payment, unknown>[] = [
value: 'failed'
}
],
filterType: 'multiselect',
filterType: 'select',
enableColumnFilter: true,
enableHiding: true
enableHiding: true,
defaultFilterValue: '',
filterProps: {
select: {
autocomplete: true
}
}
},
{
accessorKey: 'email',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type DataTableColumnDef<TData, TValue> = ColumnDef<TData, TValue> & {
enableHiding?: boolean;
defaultHidden?: boolean;
filterOptions?: FilterSelectOption[];
defaultFilterValue?: unknown;
filterProps?: {
select?: BaseSelectProps;
};
Expand Down
5 changes: 3 additions & 2 deletions packages/raystack/components/data-table/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ export function useFilters<TData, TValue>() {
const dataType = getDataType({ filterType, dataType: columnDef.dataType });
const defaultFilter = filterOperators[filterType][0];
const defaultValue =
filterType === FilterType.date
columnDef.defaultFilterValue ??
(filterType === FilterType.date
? new Date()
: filterType === FilterType.select
? options[0].value
: '';
: '');
Comment on lines +21 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Guard select fallback against empty options to prevent runtime error.

At Line 25, options[0].value will throw when filterOptions is empty and no defaultFilterValue is provided. Please make this access safe.

💡 Proposed fix
-    const defaultValue =
-      columnDef.defaultFilterValue ??
-      (filterType === FilterType.date
-        ? new Date()
-        : filterType === FilterType.select
-          ? options[0].value
-          : '');
+    const defaultValue =
+      columnDef.defaultFilterValue ??
+      (filterType === FilterType.date
+        ? new Date()
+        : filterType === FilterType.select
+          ? (options[0]?.value ?? '')
+          : '');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
columnDef.defaultFilterValue ??
(filterType === FilterType.date
? new Date()
: filterType === FilterType.select
? options[0].value
: '';
: '');
const defaultValue =
columnDef.defaultFilterValue ??
(filterType === FilterType.date
? new Date()
: filterType === FilterType.select
? (options[0]?.value ?? '')
: '');
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/raystack/components/data-table/hooks/useFilters.tsx` around lines 21
- 26, The fallback for select filters uses options[0].value which will throw if
options is empty; in the useFilters hook replace that direct access (the
expression that chooses between columnDef.defaultFilterValue, FilterType.date,
FilterType.select) with a safe check that only reads options[0].value when
options && options.length > 0, otherwise fall back to an empty string (or
columnDef.defaultFilterValue if present); update the expression around
FilterType.select to use a guarded lookup (e.g., options?.length ?
options[0].value : '') so the code in useFilters.tsx referencing FilterType and
options won't crash when filterOptions is empty.


updateTableQuery(query => {
return {
Expand Down
Loading