Skip to content

Commit

Permalink
#638 Press enter in new row creates new row
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps committed Nov 7, 2023
1 parent ee96969 commit b2481fd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
26 changes: 20 additions & 6 deletions browser/data-browser/src/components/TableEditor/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface CellProps {
role?: string;
onClearCell?: () => void;
onEnterEditModeWithCharacter?: (key: string) => void;
onEditNextRow?: () => void;
}

interface IndexCellProps extends CellProps {
Expand All @@ -39,6 +40,7 @@ export function Cell({
align,
role,
onEnterEditModeWithCharacter = () => undefined,
onEditNextRow,
}: React.PropsWithChildren<CellProps>): JSX.Element {
const ref = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -123,16 +125,28 @@ export function Cell({

activeCellRef.current = ref.current;

const unregister = registerEventListener(
TableEvent.EnterEditModeWithCharacter,
onEnterEditModeWithCharacter,
);
const unregisters = [
registerEventListener(
TableEvent.EnterEditModeWithCharacter,
onEnterEditModeWithCharacter,
),
registerEventListener(TableEvent.InteractionsFired, interactions => {
if (
interactions.includes(KeyboardInteraction.EditNextRow) &&
isActive
) {
onEditNextRow?.();
}
}),
];

return () => {
unregister();
for (const unregister of unregisters) {
unregister();
}
};
}
}, [isActive, onEnterEditModeWithCharacter]);
}, [isActive, onEnterEditModeWithCharacter, onEditNextRow]);

return (
<CellWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function FancyTableInner<T>({
<VisuallyHidden id={ariaUsageId}>
<p>{ARIA_TABLE_USAGE}</p>
</VisuallyHidden>
{/* @ts-ignore */}
<Table
aria-labelledby={labelledBy}
aria-rowcount={itemCount}
Expand Down Expand Up @@ -234,6 +235,7 @@ interface TableProps {
totalContentHeight: number;
}

// @ts-ignore
const Table = styled.div.attrs<TableProps>(p => ({
style: {
'--table-template-columns': p.gridTemplateColumns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export enum TableEvent {
EnterEditModeWithCharacter = 'enterEditModeWithCharacter',
ClearCell = 'clearCell',
ClearRow = 'clearRow',
InteractionsFired = 'interactionsFired',
}

export type TableEventHandlers = {
enterEditModeWithCharacter: (key: string) => void;
clearCell: () => void;
clearRow: (index: number) => void;
interactionsFired: (interactions: KeyboardInteraction[]) => void;
};

export enum CursorMode {
Expand Down Expand Up @@ -55,7 +57,8 @@ export interface TableEditorContext {
registerEventListener<T extends TableEvent>(
event: T,
cb: TableEventHandlers[T],
);
): () => void;
emitInteractionsFired(interactions: KeyboardInteraction[]): void;
}

const initial = {
Expand All @@ -80,7 +83,8 @@ const initial = {
clearCell: () => undefined,
clearRow: (_: number) => undefined,
enterEditModeWithCharacter: (_: string) => undefined,
registerEventListener: () => undefined,
registerEventListener: () => () => undefined,
emitInteractionsFired: () => undefined,
};

const TableEditorContext = React.createContext<TableEditorContext>(initial);
Expand Down Expand Up @@ -146,6 +150,13 @@ export function TableEditorContextProvider({
[eventManager],
);

const emitInteractionsFired = useCallback(
(interactions: KeyboardInteraction[]) => {
eventManager.emit(TableEvent.InteractionsFired, interactions);
},
[eventManager],
);

const context = useMemo(
() => ({
tableRef,
Expand All @@ -170,6 +181,7 @@ export function TableEditorContextProvider({
clearCell,
clearRow,
enterEditModeWithCharacter,
emitInteractionsFired,
}),
[
disabledKeyboardInteractions,
Expand All @@ -182,6 +194,7 @@ export function TableEditorContextProvider({
setMultiSelectCorner,
isDragging,
cursorMode,
emitInteractionsFired,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ export function useTableEditorKeyboardNavigation(
multiSelectCornerColumn,
setActiveCell,
listRef,
emitInteractionsFired,
} = tableContext;

const hasControlLock = useHasControlLock();

const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
if (hasControlLock || tableHeaderHasFocus(headerRef)) {
console.warn('Control lock enabled, can not use keyboard.');

return;
}

Expand Down Expand Up @@ -102,6 +101,8 @@ export function useTableEditorKeyboardNavigation(

handler.handler(context);
}

emitInteractionsFired(handlers.map(h => h.id));
},
[
disabledKeyboardInteractions,
Expand All @@ -114,6 +115,7 @@ export function useTableEditorKeyboardNavigation(
commands.undo,
commands.expand,
hasControlLock,
emitInteractionsFired,
],
);

Expand Down
23 changes: 22 additions & 1 deletion browser/data-browser/src/views/TablePage/TableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function TableCell({
const store = useStore();

const [markForInvalidate, setMarkForInvalidate] = useState(false);
const { setActiveCell } = useTableEditorContext();
const { addItemsToHistoryStack } = useContext(TablePageContext);
const [save, savePending] = useDebouncedCallback(
async () => {
Expand Down Expand Up @@ -116,21 +117,41 @@ export function TableCell({
[onChange, dataType],
);

const handleEditNextRow = useCallback(() => {
if (markForInvalidate && !savePending) {
startTransition(() => {
setMarkForInvalidate(false);
invalidateTable?.();
setTimeout(() => {
setActiveCell(rowIndex + 1, columnIndex);
}, 0);
});
}
}, [
markForInvalidate,
savePending,
invalidateTable,
setActiveCell,
rowIndex,
columnIndex,
]);

useEffect(() => {
if (markForInvalidate && !isEditing && !savePending) {
startTransition(() => {
setMarkForInvalidate(false);
invalidateTable?.();
});
}
}, [isEditing, markForInvalidate, savePending]);
}, [isEditing, markForInvalidate, savePending, invalidateTable]);

return (
<Cell
rowIndex={rowIndex}
columnIndex={columnIndex}
align={alignment}
onEnterEditModeWithCharacter={handleEnterEditModeWithCharacter}
onEditNextRow={handleEditNextRow}
>
{isEditing ? (
<Editor.Edit
Expand Down

0 comments on commit b2481fd

Please sign in to comment.