Skip to content

Commit

Permalink
feat(DataTable): export sort direction enum with types
Browse files Browse the repository at this point in the history
- export utility object with sort directions
- export type derived from the sort directions
- use the type and object in the component
  • Loading branch information
booc0mtaco committed Feb 20, 2025
1 parent 8a312e8 commit 615587d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,21 @@ 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
type StatusColumn = {
status?: Extract<Status, 'critical' | 'favorable' | 'warning'>;
};

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 & {
Expand Down Expand Up @@ -359,20 +363,20 @@ export function DataTable<T>({
* Sort button utilities
*/
function sortAttrs(
sort: SortDirectionsType,
sort: DataTableSortDirection,
): Pick<ButtonProps, 'aria-label' | 'icon'> {
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',
Expand Down Expand Up @@ -656,3 +660,4 @@ DataTable.DataCell = DataTableDataCell;
DataTable.StatusCell = DataTableStatusCell;
DataTable.StatusHeaderCell = DataTableStatusHeaderCell;
DataTable.__StatusColumnId__ = 'status' as const;
DataTable.SORT_DIRECTION = SORT_DIRECTION;
1 change: 1 addition & 0 deletions src/components/DataTable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type {
DataTableProps,
DataTableRowProps,
DataTableWithStatus,
DataTableSortDirection,
} from './DataTable';

// Re-export Tanstack hooks and functions to consumers
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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';

0 comments on commit 615587d

Please sign in to comment.