generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathindex.tsx
More file actions
158 lines (143 loc) · 5.74 KB
/
index.tsx
File metadata and controls
158 lines (143 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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';
import { useSingleTabStopNavigation } from '../../internal/context/single-tab-stop-navigation-context.js';
import { usePrevious } from '../../internal/hooks/use-previous';
import InternalLiveRegion from '../../live-region/internal';
import { TableProps } from '../interfaces';
import { DisabledInlineEditor } from './disabled-inline-editor';
import { InlineEditor } from './inline-editor';
import { TableTdElement, TableTdElementProps } from './td-element';
import styles from './styles.css.js';
const submitHandlerFallback = () => {
throw new Error('The function `handleSubmit` is required for editable columns');
};
export interface TableBodyCellProps<ItemType> extends TableTdElementProps {
column: TableProps.ColumnDefinition<ItemType>;
item: ItemType;
successfulEdit?: boolean;
onEditStart: () => void;
onEditEnd: (cancelled: boolean) => void;
submitEdit?: TableProps.SubmitEditFunction<ItemType>;
ariaLabels: TableProps['ariaLabels'];
}
function TableCellEditable<ItemType>({
item,
column,
isEditing,
onEditStart,
onEditEnd,
submitEdit,
ariaLabels,
successfulEdit = false,
...rest
}: TableBodyCellProps<ItemType>) {
const i18n = useInternalI18n('table');
const editActivateRef = useRef<HTMLButtonElement>(null);
const tdNativeAttributes = {
'data-inline-editing-active': isEditing.toString(),
};
const isFocusMoveNeededRef = useRef(false);
const isExpandableColumn = rest.level !== undefined;
useEffect(() => {
if (!isEditing && editActivateRef.current && isFocusMoveNeededRef.current) {
isFocusMoveNeededRef.current = false;
editActivateRef.current.focus();
}
}, [isEditing]);
// To improve the initial page render performance we only show the edit icon when necessary.
const [hasFocus, setHasFocus] = useState(false);
const prevSuccessfulEdit = usePrevious(successfulEdit);
const prevHasFocus = usePrevious(hasFocus);
const [showSuccessIcon, setShowSuccessIcon] = useState(false);
useEffect(() => {
// Hide the success icon after a successful edit, when cell loses focus.
if (successfulEdit && prevSuccessfulEdit && !hasFocus && prevHasFocus) {
setShowSuccessIcon(false);
}
// Show success icon right after a successful edit, when `successfulEdit` switches to true.
if (successfulEdit && !prevSuccessfulEdit) {
setShowSuccessIcon(true);
}
}, [hasFocus, successfulEdit, prevHasFocus, prevSuccessfulEdit]);
const { tabIndex: editActivateTabIndex } = useSingleTabStopNavigation(editActivateRef);
return (
<TableTdElement
{...rest}
nativeAttributes={tdNativeAttributes as TableTdElementProps['nativeAttributes']}
isEditing={isEditing}
hasSuccessIcon={showSuccessIcon && hasFocus}
onClick={!isEditing && !isExpandableColumn ? onEditStart : undefined}
onFocus={() => setHasFocus(true)}
onBlur={() => setHasFocus(false)}
>
{isEditing && column.editConfig ? (
<InlineEditor
ariaLabels={ariaLabels}
column={column}
item={item}
onEditEnd={options => {
setShowSuccessIcon(false);
isFocusMoveNeededRef.current = options.refocusCell;
onEditEnd(options.cancelled);
}}
submitEdit={submitEdit ?? submitHandlerFallback}
/>
) : (
<>
{column.cell(item)}
{showSuccessIcon && hasFocus && (
<>
<span
className={styles['body-cell-success']}
onMouseDown={e => {
// Prevent the editor's Button blur event to be fired when clicking the success icon.
// This prevents unfocusing the button and triggers the `TableTdElement` onClick event which initiates the edit mode.
e.preventDefault();
}}
>
<Icon name="status-positive" variant="success" ariaLabel={ariaLabels?.successfulEditLabel?.(column)} />
</span>
<InternalLiveRegion tagName="span" hidden={true}>
{i18n('ariaLabels.successfulEditLabel', ariaLabels?.successfulEditLabel?.(column))}
</InternalLiveRegion>
</>
)}
<div className={styles['body-cell-editor-wrapper']}>
<button
className={clsx(styles['body-cell-editor'], isExpandableColumn && styles['body-cell-editor-focusable'])}
aria-label={ariaLabels?.activateEditLabel?.(column, item)}
ref={editActivateRef}
onClick={!isEditing && isExpandableColumn ? onEditStart : undefined}
tabIndex={editActivateTabIndex}
>
<span className={styles['body-cell-editor-icon']}>
<Icon name="edit" />
</span>
</button>
</div>
</>
)}
</TableTdElement>
);
}
export function TableBodyCell<ItemType>(props: TableBodyCellProps<ItemType>) {
const editDisabledReason = props.column.editConfig?.disabledReason?.(props.item);
// Inline editing is deactivated for expandable column because editable cells are interactive
// and cannot include interactive content such as expand toggles.
if (editDisabledReason) {
return <DisabledInlineEditor editDisabledReason={editDisabledReason} {...props} />;
}
if (props.isEditable || props.isEditing) {
return <TableCellEditable {...props} />;
}
const { column, item } = props;
return (
<TableTdElement {...props} isEditable={false}>
{column.cell(item)}
</TableTdElement>
);
}