Skip to content

Commit

Permalink
refactor(typescript): Convert TableCell, TableExpandRow, TableExpande…
Browse files Browse the repository at this point in the history
…dRow, TableRow to tsx (#13297)

* refactor: fix ReactAttr missing some attributes

* refactor: conversion of table rows and cell

* refactor: update usages of ReactAttr

* refactor: update ReactAttr uses

* chore: this reverts commit 2a1e342

* Revert "refactor: fix ReactAttr missing some attributes"

This reverts commit 6f4b640.

* refactor: adjust types after revert

* chore: merge

* refactor: fix TableRow props type

* refactor: update wrapComponent typing

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
mbarrer and kodiakhq[bot] authored Mar 20, 2023
1 parent 843ff10 commit 888b0c0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import { TdHTMLAttributes } from 'react';
import wrapComponent from '../../tools/wrapComponent';

const TableCell = wrapComponent({
export type TableCellProps = TdHTMLAttributes<HTMLTableCellElement>;

const TableCell: React.FC<TableCellProps> = wrapComponent({
name: 'TableCell',
type: 'td',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,40 @@

import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import React, { MouseEventHandler, PropsWithChildren } from 'react';
import { ChevronRight } from '@carbon/icons-react';
import TableCell from './TableCell';
import { usePrefix } from '../../internal/usePrefix';
import { TableRowProps } from './TableRow';

interface TableExpandRowProps extends PropsWithChildren<TableRowProps> {
/**
* Specify the string read by a voice reader when the expand trigger is
* focused
*/
ariaLabel: string;

/**
* The id of the matching th node in the table head. Addresses a11y concerns outlined here: https://www.ibm.com/able/guidelines/ci162/info_and_relationships.html and https://www.w3.org/TR/WCAG20-TECHS/H43
*/
expandHeader?: string;

/**
* The description of the chevron right icon, to be put in its SVG `<title>` element.
*/
expandIconDescription?: string;

/**
* Specify whether this row is expanded or not. This helps coordinate data
* attributes so that `TableExpandRow` and `TableExpandedRow` work together
*/
isExpanded: boolean;

/**
* Hook for when a listener initiates a request to expand the given row
*/
onExpand: MouseEventHandler<HTMLButtonElement>;
}

const TableExpandRow = ({
ariaLabel,
Expand All @@ -22,7 +52,7 @@ const TableExpandRow = ({
isSelected,
expandHeader = 'expand',
...rest
}) => {
}: TableExpandRowProps) => {
const prefix = usePrefix();
const className = cx(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ import PropTypes from 'prop-types';
import React, { useRef } from 'react';
import TableCell from './TableCell';
import { usePrefix } from '../../internal/usePrefix';
import { ReactAttr } from '../../types/common';

interface TableExpandedRowProps extends ReactAttr<HTMLTableRowElement> {
/**
* The width of the expanded row's internal cell
*/
colSpan: number;
}

const TableExpandedRow = ({
className: customClassName,
children,
colSpan,
...rest
}) => {
const rowRef = useRef(null);
}: TableExpandedRowProps) => {
const rowRef = useRef<HTMLTableRowElement>(null);
const prefix = usePrefix();
const className = cx(`${prefix}--expandable-row`, customClassName);

const toggleParentHoverClass = (eventType) => {
const toggleParentHoverClass = (eventType: 'enter' | 'leave') => {
if (rowRef && rowRef.current && rowRef.current.previousElementSibling) {
const parentNode = rowRef.current.previousElementSibling;
if (eventType === 'enter') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ import PropTypes from 'prop-types';
import omit from 'lodash.omit';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix';
import { ReactAttr } from '../../types/common';

const TableRow = (props) => {
export interface TableRowProps extends ReactAttr<HTMLTableRowElement> {
/**
* Specify an optional className to be applied to the container node
*/
className?: string;
/**
* Specify if the row is selected
*/
isSelected?: boolean;
}

const TableRow = (props: TableRowProps) => {
const prefix = usePrefix();
// Remove unnecessary props if provided to this component, these are
// only useful in `TableExpandRow`
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/tools/wrapComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import PropTypes from 'prop-types';
import cx from 'classnames';
import { usePrefix } from '../internal/usePrefix';

/**
* @param {{ name: string, type: string, className?: string | (prefix: string) => string }} props
* @returns
*/
const wrapComponent = ({ name, className: getClassName, type }) => {
/**
*
* @param {{ className?: string, [x: string]: any}} param0
* @returns
*/
function Component({ className: baseClassName, ...other }) {
const prefix = usePrefix();
const componentClass = cx(
Expand Down

0 comments on commit 888b0c0

Please sign in to comment.