Skip to content

Commit

Permalink
fix: data spreadsheet blur (carbon-design-system#5477)
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinGitHub authored Jun 18, 2024
1 parent 504841c commit de3a830
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export let DataSpreadsheet = React.forwardRef(
const [isEditing, setIsEditing] = useState(false);
const [cellEditorValue, setCellEditorValue] = useState('');
const [headerCellHoldActive, setHeaderCellHoldActive] = useState(false);
const isBlurSpreadsheet = useRef(false);
const [isActiveHeaderCellChanged, setIsActiveHeaderCellChanged] =
useState<boolean>(false);
const [activeCellInsideSelectionArea, setActiveCellInsideSelectionArea] =
Expand All @@ -190,6 +191,7 @@ export let DataSpreadsheet = React.forwardRef(
usePreviousValue({
activeCellCoordinates,
isEditing,
cellEditorValue,
}) || {};
const cellSizeValue = getCellSize(cellSize);
const cellEditorRef = useRef<HTMLTextAreaElement>();
Expand Down Expand Up @@ -309,9 +311,21 @@ export let DataSpreadsheet = React.forwardRef(
setIsActiveHeaderCellChanged((prev) => !prev);
}
}
// For when we edit and focus out of data spreadsheet
if (
isEditing &&
previousState.activeCellCoordinates &&
isBlurSpreadsheet.current
) {
setActiveCellContent(previousState.cellEditorValue);
isBlurSpreadsheet.current = false;
removeCellEditor();
}
}, [
isBlurSpreadsheet,
activeCellCoordinates,
previousState?.activeCellCoordinates,
previousState?.cellEditorValue,
updateData,
rows,
isEditing,
Expand Down Expand Up @@ -352,6 +366,7 @@ export let DataSpreadsheet = React.forwardRef(
});

useSpreadsheetOutsideClick({
isBlurSpreadsheet,
spreadsheetRef,
setActiveCellCoordinates,
setSelectionAreas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { removeCellSelections } from '../utils/removeCellSelections';

// Click outside useEffect for spreadsheet
export const useSpreadsheetOutsideClick = ({
isBlurSpreadsheet,
spreadsheetRef,
blockClass = `${pkg.prefix}--data-spreadsheet`,
setActiveCellCoordinates,
Expand All @@ -33,18 +34,19 @@ export const useSpreadsheetOutsideClick = ({
) {
return;
}
isBlurSpreadsheet.current = true;
setActiveCellCoordinates(null);
setSelectionAreas([]);
removeActiveCell();
removeCellSelections({ spreadsheetRef });
setContainerHasFocus(false);
removeCellEditor();
};
document.addEventListener('click', handleOutsideClick);
return () => {
document.removeEventListener('click', handleOutsideClick);
};
}, [
isBlurSpreadsheet,
spreadsheetRef,
removeActiveCell,
blockClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ActiveCellCoordinates {
}

export interface PrevState {
cellEditorValue?: string;
activeCellCoordinates?: ActiveCellCoordinates;
isEditing?: boolean;
selectionAreaData?: object[];
Expand Down

0 comments on commit de3a830

Please sign in to comment.