Skip to content

Commit 81e2fb9

Browse files
chore: types for TableExpandHeader props (#13985)
Co-authored-by: Francine Lucca <40550942+francinelucca@users.noreply.github.com>
1 parent b4b08b0 commit 81e2fb9

1 file changed

Lines changed: 74 additions & 9 deletions

File tree

packages/react/src/components/DataTable/TableExpandHeader.js renamed to packages/react/src/components/DataTable/TableExpandHeader.tsx

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,66 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import { ChevronRight } from '@carbon/icons-react';
89
import cx from 'classnames';
9-
import PropTypes from 'prop-types';
10-
import requiredIfGivenPropIsTruthy from '../../prop-types/requiredIfGivenPropIsTruthy';
11-
import deprecate from '../../prop-types/deprecate';
10+
import PropTypes, { Validator } from 'prop-types';
1211
import React from 'react';
13-
import { ChevronRight } from '@carbon/icons-react';
1412
import { usePrefix } from '../../internal/usePrefix';
13+
import deprecate from '../../prop-types/deprecate';
14+
import requiredIfGivenPropIsTruthy from '../../prop-types/requiredIfGivenPropIsTruthy';
15+
import { ReactAttr } from '../../types/common';
16+
17+
type TableExpandHeaderPropsBase = {
18+
/**
19+
* Specify the string read by a voice reader when the expand trigger is
20+
* focused
21+
*/
22+
ariaLabel?: string;
23+
/**
24+
* @deprecated The enableExpando prop is being replaced by `enableToggle`
25+
*/
26+
enableExpando?: false | undefined;
27+
/**
28+
* Specify whether an expand all button should be displayed
29+
*/
30+
enableToggle?: false | undefined;
31+
/**
32+
* The description of the chevron right icon, to be put in its SVG `<title>` element.
33+
*/
34+
expandIconDescription?: string;
35+
/**
36+
* Specify whether this row is expanded or not. This helps coordinate data
37+
* attributes so that `TableExpandRow` and `TableExpandedRow` work together
38+
*/
39+
isExpanded?: boolean;
40+
/**
41+
* Hook for when a listener initiates a request to expand the given row
42+
*/
43+
onExpand?(event: React.MouseEvent<HTMLButtonElement>): void;
44+
} & ReactAttr<HTMLTableCellElement>;
45+
46+
type TableExpandHeaderPropsWithToggle = Omit<
47+
TableExpandHeaderPropsBase,
48+
'ariaLabel' | 'enableToggle' | 'onExpand'
49+
> & {
50+
enableToggle: true;
51+
ariaLabel: string;
52+
onExpand(event: React.MouseEvent<HTMLButtonElement>): void;
53+
};
54+
55+
type TableExpandHeaderPropsWithExpando = Omit<
56+
TableExpandHeaderPropsBase,
57+
'ariaLabel' | 'enableExpando' | 'onExpand'
58+
> & {
59+
enableExpando: true;
60+
ariaLabel: string;
61+
onExpand(event: React.MouseEvent<HTMLButtonElement>): void;
62+
};
63+
64+
export type TableExpandHeaderProps =
65+
| TableExpandHeaderPropsWithToggle
66+
| TableExpandHeaderPropsWithExpando
67+
| TableExpandHeaderPropsBase;
1568

1669
const TableExpandHeader = ({
1770
ariaLabel,
@@ -24,7 +77,7 @@ const TableExpandHeader = ({
2477
expandIconDescription,
2578
children,
2679
...rest
27-
}) => {
80+
}: TableExpandHeaderProps) => {
2881
const prefix = usePrefix();
2982
const className = cx(`${prefix}--table-expand`, headerClassName);
3083
const previousValue = isExpanded ? 'collapsed' : undefined;
@@ -60,8 +113,14 @@ TableExpandHeader.propTypes = {
60113
* focused
61114
*/
62115
ariaLabel: PropTypes.oneOfType([
63-
requiredIfGivenPropIsTruthy('enableExpando', PropTypes.string),
64-
requiredIfGivenPropIsTruthy('enableToggle', PropTypes.string),
116+
requiredIfGivenPropIsTruthy(
117+
'enableExpando',
118+
PropTypes.string
119+
) as Validator<any>,
120+
requiredIfGivenPropIsTruthy(
121+
'enableToggle',
122+
PropTypes.string
123+
) as Validator<any>,
65124
]),
66125

67126
children: PropTypes.node,
@@ -101,8 +160,14 @@ TableExpandHeader.propTypes = {
101160
* Hook for when a listener initiates a request to expand the given row
102161
*/
103162
onExpand: PropTypes.oneOfType([
104-
requiredIfGivenPropIsTruthy('enableExpando', PropTypes.func),
105-
requiredIfGivenPropIsTruthy('enableToggle', PropTypes.func),
163+
requiredIfGivenPropIsTruthy(
164+
'enableExpando',
165+
PropTypes.func
166+
) as Validator<any>,
167+
requiredIfGivenPropIsTruthy(
168+
'enableToggle',
169+
PropTypes.func
170+
) as Validator<any>,
106171
]),
107172
};
108173

0 commit comments

Comments
 (0)