From a93a8b293b5523a9ee50bf4f06bb3893d14f6992 Mon Sep 17 00:00:00 2001 From: Andrei Zhaleznichenka Date: Fri, 17 Jan 2025 14:44:57 +0100 Subject: [PATCH] fix: Allows table cells be expandable and editable at the same time --- src/table/__tests__/expandable-rows.test.tsx | 5 +-- src/table/body-cell/index.tsx | 17 ++++---- src/table/body-cell/styles.scss | 44 ++++++++++++-------- src/table/body-cell/td-element.tsx | 12 ++++-- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/table/__tests__/expandable-rows.test.tsx b/src/table/__tests__/expandable-rows.test.tsx index e3356c4207..528a3acc25 100644 --- a/src/table/__tests__/expandable-rows.test.tsx +++ b/src/table/__tests__/expandable-rows.test.tsx @@ -283,7 +283,7 @@ describe('Expandable rows', () => { expect(table.findEditCellButton(1, 2)).not.toBe(null); }); - test('first column of expandable table cannot be editable', () => { + test('columns can be both editable and expandable', () => { const { table } = renderTable({ items: simpleItems, columnDefinitions: [ @@ -297,7 +297,6 @@ describe('Expandable rows', () => { onExpandableItemToggle: () => {}, }, }); - expect(table.findEditCellButton(1, 1)).toBe(null); - expect(table.findEditCellButton(1, 2)).not.toBe(null); + expect(table.findEditCellButton(1, 1)).not.toBe(null); }); }); diff --git a/src/table/body-cell/index.tsx b/src/table/body-cell/index.tsx index 8c0332fc86..6dd68e3e74 100644 --- a/src/table/body-cell/index.tsx +++ b/src/table/body-cell/index.tsx @@ -1,6 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import React, { useEffect, useRef, useState } from 'react'; +import clsx from 'clsx'; import { useInternalI18n } from '../../i18n/context'; import Icon from '../../icon/internal'; @@ -46,6 +47,8 @@ function TableCellEditable({ }; const isFocusMoveNeededRef = useRef(false); + const isExpandable = rest.level !== undefined; + useEffect(() => { if (!isEditing && editActivateRef.current && isFocusMoveNeededRef.current) { isFocusMoveNeededRef.current = false; @@ -81,9 +84,11 @@ function TableCellEditable({ nativeAttributes={tdNativeAttributes as TableTdElementProps['nativeAttributes']} isEditing={isEditing} hasSuccessIcon={showSuccessIcon && showIcon} - onClick={!isEditing ? onEditStart : undefined} + onClick={!isEditing && !isExpandable ? onEditStart : undefined} onMouseEnter={() => setHasHover(true)} onMouseLeave={() => setHasHover(false)} + onFocus={() => setHasFocus(true)} + onBlur={() => setHasFocus(false)} > {isEditing ? ( ({