From fef7d2563db443107707ef604b1cc9fdea38b868 Mon Sep 17 00:00:00 2001 From: Afsal K Date: Wed, 12 Jun 2024 11:49:10 +0530 Subject: [PATCH] refactor(useSortableColumns): implement types (#5464) --- .../src/components/Datagrid/types/index.ts | 1 + ...{useSortableColumns.js => useSortableColumns.tsx} | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) rename packages/ibm-products/src/components/Datagrid/{useSortableColumns.js => useSortableColumns.tsx} (93%) diff --git a/packages/ibm-products/src/components/Datagrid/types/index.ts b/packages/ibm-products/src/components/Datagrid/types/index.ts index df6fd1e07a..8652a31bc1 100644 --- a/packages/ibm-products/src/components/Datagrid/types/index.ts +++ b/packages/ibm-products/src/components/Datagrid/types/index.ts @@ -264,6 +264,7 @@ export interface DataGridState onVirtualScroll?: (evt?: boolean) => void; fetchMoreData?: () => void; loadMoreThreshold?: number; + onSort?: boolean; } // DatagridHeaderRow related types diff --git a/packages/ibm-products/src/components/Datagrid/useSortableColumns.js b/packages/ibm-products/src/components/Datagrid/useSortableColumns.tsx similarity index 93% rename from packages/ibm-products/src/components/Datagrid/useSortableColumns.js rename to packages/ibm-products/src/components/Datagrid/useSortableColumns.tsx index 55cac510ca..d820ac80dd 100644 --- a/packages/ibm-products/src/components/Datagrid/useSortableColumns.js +++ b/packages/ibm-products/src/components/Datagrid/useSortableColumns.tsx @@ -12,6 +12,8 @@ import { Button } from '@carbon/react'; import { ArrowUp, ArrowDown, ArrowsVertical } from '@carbon/react/icons'; import { SelectAll } from './Datagrid/DatagridSelectAll'; import { DatagridSlug } from './Datagrid/addons/Slug/DatagridSlug'; +import { Hooks, TableInstance } from 'react-table'; +import { DataGridState } from './types'; const blockClass = `${pkg.prefix}--datagrid`; @@ -21,9 +23,9 @@ const ordering = { NONE: 'NONE', }; -export const getNewSortOrder = (sortOrder) => { +export const getNewSortOrder = (sortOrder?: boolean | string) => { const order = { - newSortDesc: undefined, + newSortDesc: false, newOrder: ordering.NONE, }; if (sortOrder === false || sortOrder === ordering.DESC) { @@ -71,7 +73,7 @@ const getAriaPressedValue = (col) => { return 'false'; }; -const useSortableColumns = (hooks) => { +const useSortableColumns = (hooks: Hooks) => { const sortableVisibleColumns = (visibleColumns, { instance }) => { const { onSort, @@ -171,8 +173,8 @@ const useSortableColumns = (hooks) => { : [...sortableColumns]; }; - const sortInstanceProps = (instance) => { - const { onSort } = instance; + const sortInstanceProps = (instance: TableInstance) => { + const { onSort } = instance as DataGridState; Object.assign(instance, { manualSortBy: !!onSort, isTableSortable: true }); };