@carbon/react
Version:
React components for the Carbon Design System
61 lines (60 loc) • 2.69 kB
TypeScript
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { type DataTableSortState } from './sortStates';
import type { DataTableCell } from '../DataTable';
export interface SortRowParams {
key: string;
sortDirection: DataTableSortState;
sortStates: Record<DataTableSortState, DataTableSortState>;
locale: string;
compare: (a: string | number, b: string | number, locale?: string) => number;
}
export type SortRowFn = (cellA: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
cellB: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
options: SortRowParams) => number;
interface Props {
locale?: string;
sortRow?: SortRowFn;
}
interface State<ColTypes extends any[]> {
rowIds: string[];
cellsById: Record<string, DataTableCell<ColTypes[number]>>;
initialRowOrder: string[];
sortHeaderKey: string | null;
sortDirection: DataTableSortState;
}
export declare const initialSortState: DataTableSortState;
/**
* Gets the next sort direction for a header.
*
* @param prevHeader - Key of the previously sorted header.
* @param currentHeader - Key of the currently selected header.
* @param prevState - Previous sort direction.
*/
export declare const getNextSortDirection: (prevHeader: string, currentHeader: string, prevState: DataTableSortState) => DataTableSortState;
/**
* Gets the next sort state.
*
* @param props - Component props.
* @param state - Current table state.
* @param key - Header key to sort by.
*/
export declare const getNextSortState: <ColTypes extends any[]>(// eslint-disable-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
props: Props, state: State<ColTypes>, { key }: {
key: string;
}) => Pick<State<ColTypes>, "sortHeaderKey" | "sortDirection" | "rowIds">;
/**
* Gets a sort state update.
*
* @param props - Component props.
* @param state - Current state of the table.
* @param key - Header key to sort by.
* @param sortDirection - Sort direction to apply.
*/
export declare const getSortedState: <ColTypes extends any[]>(// eslint-disable-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
{ locale, sortRow }: Props, { rowIds, cellsById, initialRowOrder }: State<ColTypes>, key: string, sortDirection: DataTableSortState) => Pick<State<ColTypes>, "rowIds" | "sortDirection" | "sortHeaderKey">;
export {};