Skip to content

Latest commit

 

History

History
117 lines (99 loc) · 3.96 KB

File metadata and controls

117 lines (99 loc) · 3.96 KB

import { ImportStatement } from '@sb/components/Import'; import { Canvas, Meta } from '@storybook/addon-docs/blocks'; import { DocsHeader, Footer } from '@sb/components'; import * as ComponentStories from './AnalyticalTableHooks.stories';

AnalyticalTable Plugin: useF2CellEdit

<ImportStatement moduleNames={['AnalyticalTableHooks']} packageName={'@ui5/webcomponents-react'} />

Since: v2.14.0

A plugin hook that enables F2-based cell editing for interactive elements inside a cell.

To ensure the hook works correctly, make sure that:

  • Each column containing interactive elements has the interactiveElementName property set. Note: This property is also used to describe the cell's content for screen readers.
  • The callback Ref returned by useF2CellEdit.useCallbackRef is attached to every interactive element within the cell.

The hook manages focus, keyboard navigation, and tabindex for cells with interactive content:

  • Pressing F2 moves focus between the cell container and its first interactive element.
  • Pressing Tab on a focused header cell moves focus to the body cell in the same column at the last focused body row (or the first row if none was focused).
  • Pressing Shift+Tab on a focused body cell moves focus back to the header cell of the same column.
  • Updates the cell's aria-label with the interactive element's name for accessibility.
  • Prevents standard navigation keys from interfering when editing a cell.

Example

Code

import type {
  AnalyticalTableCellInstance,
  AnalyticalTableColumnDefinition,
  InputDomRef,
  AnalyticalTablePropTypes,
} from '@ui5/webcomponents-react';
import { AnalyticalTableHooks, AnalyticalTable, Button, CheckBox, Input, Switch, Tag } from '@ui5/webcomponents-react';
import paperPlaneIcon from '@ui5/webcomponents-icons/dist/paper-plane';

const { useF2CellEdit } = AnalyticalTableHooks;

const columns: AnalyticalTableColumnDefinition[] = [
  {
    Header: 'Input',
    id: 'input',
    Cell: (props: AnalyticalTableCellInstance) => {
      const callbackRef = useF2CellEdit.useCallbackRef<InputDomRef>(props);
      return <Input ref={callbackRef} />;
    },
    interactiveElementName: 'Input',
  },
  {
    Header: 'Input & Button',
    id: 'input_btn',
    Cell: (props: AnalyticalTableCellInstance) => {
      const callbackRef = useF2CellEdit.useCallbackRef(props);
      return (
        <>
          <Input ref={callbackRef} />
          <Button ref={callbackRef} icon={paperPlaneIcon} tooltip="Submit" accessibleName="Submit" />
        </>
      );
    },
    interactiveElementName: 'Input and Button',
  },
  {
    Header: 'Text',
    accessor: 'name',
  },
  {
    Header: 'Button',
    id: 'btn',
    Cell: (props: AnalyticalTableCellInstance) => {
      const callbackRef = useF2CellEdit.useCallbackRef(props);
      return <Button ref={callbackRef}>Button</Button>;
    },
    interactiveElementName: () => 'Button',
  },
  {
    Header: 'Non-interactive custom content',
    accessor: 'friend.name',
    Cell: (props: AnalyticalTableCellInstance) => {
      return <Tag>{props.value}</Tag>;
    },
  },
  {
    Header: 'Switch or CheckBox',
    id: 'switch_checkbox',
    Cell: (props: AnalyticalTableCellInstance) => {
      const callbackRef = useF2CellEdit.useCallbackRef(props);
      if (props.row.index % 2) {
        return <CheckBox ref={callbackRef} accessibleName="Dummy CheckBox" />;
      }
      return <Switch ref={callbackRef} accessibleName="Dummy Switch" />;
    },
    interactiveElementName: (props: AnalyticalTableCellInstance) => {
      if (props.row.index % 2) {
        return 'CheckBox';
      }
      return 'Switch';
    },
  },
];

const tableHooks: AnalyticalTablePropTypes['tableHooks'] = [useF2CellEdit];

function TableWithInputs({ data }) {
  return <AnalyticalTable data={data} columns={columns} tableHooks={tableHooks} visibleRows={5} />;
}