From 615587dc0db98dff59208cb4d011a22118269c4d Mon Sep 17 00:00:00 2001 From: Andrew Holloway Date: Thu, 20 Feb 2025 12:08:51 -0600 Subject: [PATCH] feat(DataTable): export sort direction enum with types - export utility object with sort directions - export type derived from the sort directions - use the type and object in the component --- src/components/DataTable/DataTable.tsx | 19 ++++++++++++------- src/components/DataTable/index.ts | 1 + src/index.ts | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/components/DataTable/DataTable.tsx b/src/components/DataTable/DataTable.tsx index eabf9e156..c0264b43f 100644 --- a/src/components/DataTable/DataTable.tsx +++ b/src/components/DataTable/DataTable.tsx @@ -121,7 +121,7 @@ export type DataTableHeaderCellProps = EDSBase & { * * **Default is `"default"`**. */ - sortDirection?: SortDirectionsType; + sortDirection?: DataTableSortDirection; }; // Used to augment the data model shown in the table with a provided status column type @@ -129,9 +129,13 @@ type StatusColumn = { status?: Extract; }; -const SORT_DIRECTIONS = ['ascending', 'descending', 'default'] as const; +export const SORT_DIRECTION = { + ascending: 'ascending', + descending: 'descending', + default: 'default', +}; -export type SortDirectionsType = (typeof SORT_DIRECTIONS)[number]; +export type DataTableSortDirection = keyof typeof SORT_DIRECTION; // TODO: support cellformat to apply padding and alignment value export type DataTableDataCellProps = DataTableHeaderCellProps & { @@ -359,20 +363,20 @@ export function DataTable({ * Sort button utilities */ function sortAttrs( - sort: SortDirectionsType, + sort: DataTableSortDirection, ): Pick { switch (sort) { - case 'ascending': + case SORT_DIRECTION.ascending: return { 'aria-label': 'Sort Descending', icon: 'arrow-up', }; - case 'descending': + case SORT_DIRECTION.descending: return { 'aria-label': 'Remove sort', icon: 'arrow-down', }; - case 'default': + case SORT_DIRECTION.default: default: return { 'aria-label': 'Sort Ascending', @@ -656,3 +660,4 @@ DataTable.DataCell = DataTableDataCell; DataTable.StatusCell = DataTableStatusCell; DataTable.StatusHeaderCell = DataTableStatusHeaderCell; DataTable.__StatusColumnId__ = 'status' as const; +DataTable.SORT_DIRECTION = SORT_DIRECTION; diff --git a/src/components/DataTable/index.ts b/src/components/DataTable/index.ts index b7ded8ec5..d8c91ef85 100644 --- a/src/components/DataTable/index.ts +++ b/src/components/DataTable/index.ts @@ -3,6 +3,7 @@ export type { DataTableProps, DataTableRowProps, DataTableWithStatus, + DataTableSortDirection, } from './DataTable'; // Re-export Tanstack hooks and functions to consumers diff --git a/src/index.ts b/src/index.ts index d305e50b4..4ac4713af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,6 +54,7 @@ export { default as TabGroup } from './components/TabGroup'; export { default as TextareaField } from './components/TextareaField'; export { default as ToastNotification } from './components/ToastNotification'; export { default as Tooltip } from './components/Tooltip'; +export { default as VisualPageIndicator } from './components/VisualPageIndicator'; /** * 2.x prop type exports @@ -67,6 +68,7 @@ export type { DataTableProps, DataTableRowProps, DataTableWithStatus, + DataTableSortDirection, } from './components/DataTable'; // TODO(next-major): Remove the below types at the next major release @@ -78,4 +80,3 @@ export type { AppNotificationProps as AppNotificationV2Props } from './component */ // https://headlessui.com/v1/react/transition export { Transition } from '@headlessui/react'; -export { default as VisualPageIndicator } from './components/VisualPageIndicator';