-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Table cell editing (#1399)
- Loading branch information
Showing
2 changed files
with
80 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useCallback, useState } from 'react'; | ||
import { TableProps } from './interfaces'; | ||
import { CancelableEventHandler, fireCancelableEvent } from '../internal/events'; | ||
|
||
export interface CellId { | ||
rowIndex: number; | ||
colIndex: number; | ||
} | ||
|
||
interface CellEditingProps { | ||
onCancel?: CancelableEventHandler; | ||
onSubmit?: TableProps.SubmitEditFunction<any>; | ||
} | ||
|
||
export function useCellEditing({ onCancel, onSubmit }: CellEditingProps) { | ||
const [currentEditCell, setCurrentEditCell] = useState<null | CellId>(null); | ||
const [lastSuccessfulEditCell, setLastSuccessfulEditCell] = useState<null | CellId>(null); | ||
const [currentEditLoading, setCurrentEditLoading] = useState(false); | ||
|
||
const startEdit = (cellId: CellId) => { | ||
setLastSuccessfulEditCell(null); | ||
setCurrentEditCell(cellId); | ||
}; | ||
|
||
const cancelEdit = useCallback(() => setCurrentEditCell(null), []); | ||
|
||
const completeEdit = (cellId: CellId, editCancelled: boolean) => { | ||
const eventCancelled = fireCancelableEvent(onCancel, {}); | ||
if (!eventCancelled) { | ||
setCurrentEditCell(null); | ||
if (!editCancelled) { | ||
setLastSuccessfulEditCell(cellId); | ||
} | ||
} | ||
}; | ||
|
||
const checkEditing = ({ rowIndex, colIndex }: CellId) => | ||
rowIndex === currentEditCell?.rowIndex && colIndex === currentEditCell.colIndex; | ||
|
||
const checkLastSuccessfulEdit = ({ rowIndex, colIndex }: CellId) => | ||
rowIndex === lastSuccessfulEditCell?.rowIndex && colIndex === lastSuccessfulEditCell.colIndex; | ||
|
||
const submitEdit = onSubmit | ||
? async (...args: Parameters<typeof onSubmit>) => { | ||
setCurrentEditLoading(true); | ||
try { | ||
await onSubmit(...args); | ||
} finally { | ||
setCurrentEditLoading(false); | ||
} | ||
} | ||
: undefined; | ||
|
||
return { | ||
isLoading: currentEditLoading, | ||
startEdit, | ||
cancelEdit, | ||
checkEditing, | ||
checkLastSuccessfulEdit, | ||
completeEdit, | ||
submitEdit, | ||
}; | ||
} |