From 6365afaec238a79410c7d1cb89e423faceff20dc Mon Sep 17 00:00:00 2001 From: Andrei Zhaleznichenka Date: Fri, 15 Nov 2024 14:08:57 +0100 Subject: [PATCH] using actual table for demo --- pages/table/cell-permutations.page.tsx | 386 ++++++----------------- src/table/__tests__/header-cell.test.tsx | 1 + src/table/body-cell/index.tsx | 5 +- src/table/no-data-cell.tsx | 2 +- 4 files changed, 101 insertions(+), 293 deletions(-) diff --git a/pages/table/cell-permutations.page.tsx b/pages/table/cell-permutations.page.tsx index 93ecf0b0f3..560eed95ed 100644 --- a/pages/table/cell-permutations.page.tsx +++ b/pages/table/cell-permutations.page.tsx @@ -1,6 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { useContext, useRef, useState } from 'react'; +import React, { useContext, useState } from 'react'; import { range } from 'lodash'; import { @@ -15,15 +15,9 @@ import { Slider, SpaceBetween, StatusIndicator, + Table, TableProps, } from '~components'; -import { useMergeRefs } from '~components/internal/hooks/use-merge-refs'; -import { TableBodyCell, TableBodyCellProps } from '~components/table/body-cell'; -import { TableHeaderCell, TableHeaderCellProps } from '~components/table/header-cell'; -import { NoDataCell } from '~components/table/no-data-cell'; -import { TableLoaderCell, TableLoaderCellProps } from '~components/table/progressive-loading/loader-cell'; -import { TableBodySelectionCell, TableHeaderSelectionCell } from '~components/table/selection/selection-cell'; -import { StickyColumnsModel, useStickyColumns } from '~components/table/sticky-columns'; import AppContext, { AppContextType } from '../app/app-context'; import ScreenshotArea from '../utils/screenshot-area'; @@ -34,7 +28,6 @@ type PageContext = React.Context< verticalAlignTop?: boolean; sortingDisabled?: boolean; resizableColumns?: boolean; - resizableColumnWidth?: string; isExpandable?: boolean; isExpanded?: boolean; isEditable?: boolean; @@ -154,15 +147,6 @@ export default function InlineEditorPermutations() { - - setUrlParams({ resizableColumnWidth: event.detail.value.toString() })} - min={50} - max={300} - /> - - - Standard cells}> - + Actual table demo}> + ); } -function StandardCellsTable() { +function TableCellsDemo() { const { settings } = usePageSettings(); - const containerRefObject = useRef(null); - const containerRef = useMergeRefs(containerRefObject, settings.stickyState.refs.wrapper); - const tableRefObject = useRef(null); - const tableRef = useMergeRefs(tableRefObject, settings.stickyState.refs.table); - return ( -
- - - - - - {settings.tableEmpty || settings.tableLoading ? ( - - - - ) : ( - <> - - - - - - - - - - - - - )} - -
-
- ); -} + const [editedValues, setEditedValues] = useState>({}); -function TextHeaders( - props: Partial> & { - stickyState: StickyColumnsModel; - resizableColumnWidth?: number; - isExpandable?: boolean; - hasSelection?: boolean; - multiSelection?: boolean; - } -) { - return ( - - {props.hasSelection && ( - {}} - columnId="selection" - getSelectAllProps={ - props.multiSelection - ? () => ({ - name: 'select-all', - disabled: false, - selectionType: 'multi', - indeterminate: true, - checked: false, - onChange: () => {}, - }) - : undefined - } - /> - )} - {columns.map(index => ( - - {`Header cell content ${index}${index === 8 ? ' with longer text' : ''}`} - - ))} - - ); -} + const items = [0, 1, 2, 3, 4, 5, 6, 7]; + const itemLevels = [1, 2, 3, 4, 5, 1, 2, 3]; + const itemChildren: Record = { 0: [1], 1: [2], 2: [3], 3: [4], 5: [6], 6: [7] }; + const itemLoading = new Map([ + [3, 'pending'], + [6, 'error'], + [null, 'loading'], + ]); + const selectedItems = [3, 5, 6]; -function TextColumns( - props: Partial> & { - level: number; - stickyState: StickyColumnsModel; - isExpandable?: boolean; - isExpanded?: boolean; - multiSelection?: boolean; - } -) { - return ( - - {props.hasSelection && ( - {}, - }} - /> - )} - {columns.map(index => ( - - {`Body cell content ${index}${index === 1 ? ` (L=${props.level})` : ''}${index === 8 ? ' with longer text' : ''}`} - - ))} - - ); -} + const columnDefinitions: TableProps.ColumnDefinition[] = columns.map(index => { + const columnId = index.toString(); + const cellContent = (item: number) => + editedValues[`${columnId}:${item}`] ?? + `Body cell content ${item}:${index}${index === 1 ? ` (L=${itemLevels[item]})` : ''}${index === 8 ? ' with longer text' : ''}`; + return { + id: columnId, + header: `Header cell content ${index}${index === 8 ? ' with longer text' : ''}`, + sortingField: index === 2 ? 'field-1' : index === 3 ? 'field-2' : undefined, + activeSorting: index === 3, + cell: cellContent, + verticalAlign: settings.verticalAlign, + editConfig: settings.isEditable + ? { + ariaLabel: 'Edit dialog aria label', + editIconAriaLabel: 'Edit icon label', + errorIconAriaLabel: 'Edit error icon label', + validation(_item, value = '') { + if (value.trim() && value.toLowerCase().includes('content')) { + return 'Must not include "content"'; + } + }, + editingCell(item, { currentValue, setValue }: TableProps.CellContext) { + return ( + setValue(event.detail.value)} + /> + ); + }, + } + : undefined, + }; + }); -function LoaderColumns( - props: Partial> & { - level: number; - stickyState: StickyColumnsModel; - loadingStatus: TableProps.LoadingStatus; + let expandableRows: undefined | TableProps.ExpandableRows = undefined; + if (settings.isExpandable) { + expandableRows = { + getItemChildren: item => itemChildren[item] ?? [], + isItemExpandable: item => !!itemChildren[item], + expandedItems: settings.isExpanded ? items : [], + onExpandableItemToggle: () => {}, + }; } -) { - return ( - - {props.hasSelection && ( - - )} - {columns.map(index => ( - } - renderLoaderLoading={() => Loading more} - renderLoaderError={() => Error when loading more} - /> - ))} - - ); -} -function PlaygroundHeaderCell( - props: Partial> & { - children: React.ReactNode; - colIndex: number; - resizableColumnWidth?: number; - stickyState: StickyColumnsModel; - sortable?: boolean; - activeSorting?: boolean; + let selectionType: undefined | TableProps.SelectionType = undefined; + if (settings.hasSelection) { + selectionType = settings.multiSelection ? 'multi' : 'single'; } -) { - const column: TableProps.ColumnDefinition = { - id: props.colIndex.toString(), - header: props.children, - cell: () => '', - sortingField: props.sortable ? 'field' : undefined, - }; - return ( -