Skip to content

Commit 351a35a

Browse files
author
Amrit Borah
committed
fix: better table states
1 parent 098312a commit 351a35a

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

src/Shared/Components/Table/InternalTable.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const InternalTable = <
5858
clearFilters: userGivenUrlClearFilters,
5959
rowStartIconConfig,
6060
onRowClick,
61+
areFiltersApplied: userProvidedAreFiltersApplied,
6162
}: InternalTableProps<RowData, FilterVariant, AdditionalProps>) => {
6263
const {
6364
sortBy,
@@ -126,6 +127,7 @@ const InternalTable = <
126127
rows,
127128
filter,
128129
filterData,
130+
additionalProps,
129131
visibleColumns.find(({ field }) => field === sortBy)?.comparator,
130132
)
131133

@@ -140,10 +142,14 @@ const InternalTable = <
140142
// useAsync hook for 'rows' scenario
141143
const [_areRowsLoading, rowsResult, rowsError, reloadRows] = useAsync(
142144
handleFiltering,
143-
[searchKey, sortBy, sortOrder, rows, JSON.stringify(otherFilters), visibleColumns],
145+
[searchKey, filter, sortBy, sortOrder, rows, JSON.stringify(otherFilters), visibleColumns],
144146
!!rows,
145147
)
146148

149+
// NOTE: passing getRows to queryKey won't trigger a refetch
150+
// since it is a function
151+
const lastUpdatedGetRowsInstance = useMemo(() => new Date().toISOString(), [getRows])
152+
147153
// useAsync hook for 'getRows' scenario
148154
const {
149155
isFetching: _areGetRowsLoading,
@@ -160,8 +166,7 @@ const InternalTable = <
160166
searchKey,
161167
sortBy,
162168
sortOrder,
163-
// !TODO: functions in queryKey cannot trigger refetch
164-
// getRows,
169+
lastUpdatedGetRowsInstance,
165170
offset,
166171
pageSize,
167172
JSON.stringify(otherFilters),
@@ -191,7 +196,8 @@ const InternalTable = <
191196
}
192197

193198
if (!areFilteredRowsLoading && !filteredRows?.length && !loading) {
194-
return filtersVariant !== FiltersTypeEnum.NONE && areFiltersApplied ? (
199+
return filtersVariant !== FiltersTypeEnum.NONE &&
200+
(userProvidedAreFiltersApplied !== undefined ? userProvidedAreFiltersApplied : areFiltersApplied) ? (
195201
<GenericFilterEmptyState
196202
{...emptyStateConfig.noRowsForFilterConfig}
197203
handleClearFilters={userGivenUrlClearFilters ?? clearFilters}

src/Shared/Components/Table/TableContent.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ const TableContent = <
152152
? `${initialGridTemplateColumns} ${typeof rowOnHoverComponentWidth === 'number' ? `minmax(${rowOnHoverComponentWidth}px, 1fr)` : rowOnHoverComponentWidth}`
153153
: initialGridTemplateColumns
154154

155-
const gridTemplateColumns = isAnyRowExpandable
156-
? `${ACTION_GUTTER_SIZE}px ${gridTemplateColumnsWithoutExpandButton}`
157-
: gridTemplateColumnsWithoutExpandButton
155+
const gridTemplateColumns =
156+
isAnyRowExpandable || rowStartIconConfig
157+
? `${ACTION_GUTTER_SIZE}px ${gridTemplateColumnsWithoutExpandButton}`
158+
: gridTemplateColumnsWithoutExpandButton
158159

159160
useEffect(() => {
160161
const scrollEventHandler = () => {
@@ -248,21 +249,23 @@ const TableContent = <
248249
}
249250

250251
const renderRows = () => {
251-
if (loading) {
252+
if (loading && !visibleColumns.length) {
252253
return SHIMMER_DUMMY_ARRAY.map((shimmerRowLabel) => (
253254
<div
254255
key={shimmerRowLabel}
255-
className={`px-20 flexbox py-12 dc__gap-16 ${showSeparatorBetweenRows ? 'border__secondary--bottom' : ''}`}
256+
className={`px-20 flex left py-12 dc__gap-16 ${showSeparatorBetweenRows ? 'border__secondary--bottom' : ''}`}
256257
>
257-
{isBulkSelectionConfigured ? <div className="shimmer w-20" /> : null}
258+
{isBulkSelectionConfigured || rowStartIconConfig || isAnyRowExpandable ? (
259+
<div className="shimmer w-20" />
260+
) : null}
258261
{SHIMMER_DUMMY_ARRAY.map((shimmerCellLabel) => (
259262
<div key={shimmerCellLabel} className="shimmer w-200" />
260263
))}
261264
</div>
262265
))
263266
}
264267

265-
if (areFilteredRowsLoading) {
268+
if (areFilteredRowsLoading || (loading && visibleColumns.length)) {
266269
return SHIMMER_DUMMY_ARRAY.map((shimmerRowLabel) => (
267270
<div
268271
key={shimmerRowLabel}
@@ -271,6 +274,11 @@ const TableContent = <
271274
gridTemplateColumns,
272275
}}
273276
>
277+
{isBulkSelectionConfigured || rowStartIconConfig || isAnyRowExpandable ? (
278+
<div className="py-12 flex" aria-label="Loading...">
279+
<div className="shimmer h-16 w-20" />
280+
</div>
281+
) : null}
274282
{visibleColumns.map(({ label }) => (
275283
<div key={label} className="py-12 flex" aria-label="Loading...">
276284
<div className="shimmer h-16 w-100" />
@@ -364,7 +372,7 @@ const TableContent = <
364372
ariaLabel="Expand/Collapse row"
365373
showAriaLabelInTippy={false}
366374
variant={ButtonVariantType.borderLess}
367-
size={ComponentSizeType.xs}
375+
size={ComponentSizeType.xxs}
368376
style={ButtonStyleType.neutral}
369377
onClick={toggleExpandRow}
370378
/>
@@ -467,9 +475,11 @@ const TableContent = <
467475
ref={headerRef}
468476
className="bg__primary dc__min-width-fit-content px-20 border__secondary--bottom dc__position-sticky dc__zi-2 dc__top-0 generic-table__header"
469477
>
470-
{loading ? (
478+
{loading && !visibleColumns.length ? (
471479
<div className="flexbox py-12 dc__gap-16">
472-
{isBulkSelectionConfigured ? <div className="shimmer w-20" /> : null}
480+
{isBulkSelectionConfigured || rowStartIconConfig || isAnyRowExpandable ? (
481+
<div className="shimmer w-20" />
482+
) : null}
473483
{SHIMMER_DUMMY_ARRAY.map((label) => (
474484
<div key={label} className="shimmer w-200" />
475485
))}
@@ -498,13 +508,15 @@ const TableContent = <
498508
ariaLabel="Expand/Collapse all rows"
499509
showAriaLabelInTippy={false}
500510
variant={ButtonVariantType.borderLess}
501-
size={ComponentSizeType.xs}
511+
size={ComponentSizeType.xxs}
502512
style={ButtonStyleType.neutral}
503513
onClick={toggleExpandAll}
504514
/>
505515
</div>
506516
) : null}
507517

518+
{!isAnyRowExpandable && rowStartIconConfig && <div />}
519+
508520
{visibleColumns.map(
509521
(
510522
{

src/Shared/Components/Table/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ export type ViewWrapperProps<
237237
: {})
238238
>
239239

240-
type FilterConfig<FilterVariant extends FiltersTypeEnum, RowData extends unknown> = {
240+
type FilterConfig<
241+
FilterVariant extends FiltersTypeEnum,
242+
RowData extends unknown,
243+
AdditionalProps extends Record<string, any>,
244+
> = {
241245
filtersVariant: FilterVariant
242246
/**
243247
* Props for useUrlFilters/useStateFilters hooks
@@ -251,12 +255,14 @@ type FilterConfig<FilterVariant extends FiltersTypeEnum, RowData extends unknown
251255
*/
252256
filter: FilterVariant extends FiltersTypeEnum.NONE
253257
? null
254-
: (row: RowType<RowData>, filterData: UseFiltersReturnType) => boolean
258+
: (row: RowType<RowData>, filterData: UseFiltersReturnType, additionalProps: AdditionalProps) => boolean
255259
clearFilters?: FilterVariant extends FiltersTypeEnum.URL
256260
? () => void
257261
: FilterVariant extends FiltersTypeEnum.STATE
258262
? never
259263
: never
264+
265+
areFiltersApplied?: FilterVariant extends FiltersTypeEnum.NONE ? never : boolean
260266
}
261267

262268
export type InternalTableProps<
@@ -360,7 +366,7 @@ export type InternalTableProps<
360366
pageSizeOptions?: never
361367
}
362368
) &
363-
FilterConfig<FilterVariant, RowData>
369+
FilterConfig<FilterVariant, RowData, AdditionalProps>
364370

365371
export type UseResizableTableConfigWrapperProps<
366372
RowData extends unknown,

src/Shared/Components/Table/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ export const searchAndSortRows = <
4242
rows: TableProps<RowData, FilterVariant, AdditionalProps>['rows'],
4343
filter: TableProps<RowData, FilterVariant, AdditionalProps>['filter'],
4444
filterData: UseFiltersReturnType,
45+
additionalProps: AdditionalProps,
4546
comparator?: Column<RowData, FilterVariant, AdditionalProps>['comparator'],
4647
): Awaited<ReturnType<TableProps<RowData>['getRows']>> => {
4748
const { sortBy, sortOrder } = filterData ?? {}
4849

49-
const filteredRows = filter ? rows.filter((row) => filter(row, filterData)) : rows
50+
const filteredRows = filter ? rows.filter((row) => filter(row, filterData, additionalProps)) : rows
5051

5152
return {
5253
rows:

0 commit comments

Comments
 (0)