Two cells can be focused at the same time in the tables Fixes #1826 (#1866)

Co-authored-by: kramer <david.kramer@gmail.com>
Co-authored-by: Ayush Agrawal <54364088+AyushAgrawal-A2@users.noreply.github.com>
Co-authored-by: AyushAgrawal-A2 <ayushagl06@gmail.com>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
David Kramer
2023-10-13 16:37:36 +02:00
committed by GitHub
parent 2a9d94c5a2
commit d56c5fcbf6
6 changed files with 76 additions and 14 deletions

View File

@ -0,0 +1,26 @@
import { useRecoilCallback } from 'recoil';
import { currentTableCellInEditModePositionState } from '../states/currentTableCellInEditModePositionState';
import { isTableCellInEditModeFamilyState } from '../states/isTableCellInEditModeFamilyState';
export const useGetIsSomeCellInEditMode = () => {
return useRecoilCallback(
({ snapshot }) =>
() => {
const currentTableCellInEditModePosition = snapshot
.getLoadable(currentTableCellInEditModePositionState)
.valueOrThrow();
const isSomeCellInEditMode = snapshot
.getLoadable(
isTableCellInEditModeFamilyState(
currentTableCellInEditModePosition,
),
)
.valueOrThrow();
return isSomeCellInEditMode;
},
[],
);
};

View File

@ -0,0 +1,27 @@
import { useRecoilCallback } from 'recoil';
import { currentTableCellInEditModePositionState } from '../states/currentTableCellInEditModePositionState';
import { isTableCellInEditModeFamilyState } from '../states/isTableCellInEditModeFamilyState';
import { useSetSoftFocusOnCurrentTableCell } from '../table-cell/hooks/useSetSoftFocusOnCurrentTableCell';
export const useMoveSoftFocusToCurrentCellOnHover = () => {
const setSoftFocusOnCurrentTableCell = useSetSoftFocusOnCurrentTableCell();
return useRecoilCallback(
({ snapshot }) =>
() => {
const currentTableCellInEditModePosition = snapshot
.getLoadable(currentTableCellInEditModePositionState)
.valueOrThrow();
const isSomeCellInEditMode = snapshot.getLoadable(
isTableCellInEditModeFamilyState(currentTableCellInEditModePosition),
);
if (!isSomeCellInEditMode.contents) {
setSoftFocusOnCurrentTableCell();
}
},
[setSoftFocusOnCurrentTableCell],
);
};

View File

@ -8,6 +8,8 @@ import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
import { CellHotkeyScopeContext } from '../../contexts/CellHotkeyScopeContext'; import { CellHotkeyScopeContext } from '../../contexts/CellHotkeyScopeContext';
import { ColumnIndexContext } from '../../contexts/ColumnIndexContext'; import { ColumnIndexContext } from '../../contexts/ColumnIndexContext';
import { useGetIsSomeCellInEditMode } from '../../hooks/useGetIsSomeCellInEditMode';
import { useMoveSoftFocusToCurrentCellOnHover } from '../../hooks/useMoveSoftFocusToCurrentCellOnHover';
import { TableHotkeyScope } from '../../types/TableHotkeyScope'; import { TableHotkeyScope } from '../../types/TableHotkeyScope';
import { useCurrentTableCellEditMode } from '../hooks/useCurrentTableCellEditMode'; import { useCurrentTableCellEditMode } from '../hooks/useCurrentTableCellEditMode';
import { useIsSoftFocusOnCurrentTableCell } from '../hooks/useIsSoftFocusOnCurrentTableCell'; import { useIsSoftFocusOnCurrentTableCell } from '../hooks/useIsSoftFocusOnCurrentTableCell';
@ -58,8 +60,15 @@ export const TableCellContainer = ({
}: TableCellContainerProps) => { }: TableCellContainerProps) => {
const { isCurrentTableCellInEditMode } = useCurrentTableCellEditMode(); const { isCurrentTableCellInEditMode } = useCurrentTableCellEditMode();
const getIsSomeCellInEditMode = useGetIsSomeCellInEditMode();
const [isHovered, setIsHovered] = useState(false); const [isHovered, setIsHovered] = useState(false);
const moveSoftFocusToCurrentCellOnHover =
useMoveSoftFocusToCurrentCellOnHover();
const hasSoftFocus = useIsSoftFocusOnCurrentTableCell();
const setSoftFocusOnCurrentTableCell = useSetSoftFocusOnCurrentTableCell(); const setSoftFocusOnCurrentTableCell = useSetSoftFocusOnCurrentTableCell();
const { openTableCell } = useTableCell(); const { openTableCell } = useTableCell();
@ -70,7 +79,12 @@ export const TableCellContainer = ({
}; };
const handleContainerMouseEnter = () => { const handleContainerMouseEnter = () => {
setIsHovered(true); const isSomeCellInEditMode = getIsSomeCellInEditMode();
if (!isHovered && !isSomeCellInEditMode) {
setIsHovered(true);
moveSoftFocusToCurrentCellOnHover();
}
}; };
const handleContainerMouseLeave = () => { const handleContainerMouseLeave = () => {
@ -84,14 +98,12 @@ export const TableCellContainer = ({
const isEmpty = useIsFieldEmpty(); const isEmpty = useIsFieldEmpty();
const showButton = const showButton =
buttonIcon && !!buttonIcon &&
isHovered && hasSoftFocus &&
!isCurrentTableCellInEditMode && !isCurrentTableCellInEditMode &&
!editModeContentOnly && !editModeContentOnly &&
(!isFirstColumnCell || !isEmpty); (!isFirstColumnCell || !isEmpty);
const hasSoftFocus = useIsSoftFocusOnCurrentTableCell();
return ( return (
<CellHotkeyScopeContext.Provider <CellHotkeyScopeContext.Provider
value={editHotkeyScope ?? DEFAULT_CELL_SCOPE} value={editHotkeyScope ?? DEFAULT_CELL_SCOPE}
@ -123,7 +135,7 @@ export const TableCellContainer = ({
{showButton && ( {showButton && (
<TableCellButton onClick={handleButtonClick} Icon={buttonIcon} /> <TableCellButton onClick={handleButtonClick} Icon={buttonIcon} />
)} )}
<TableCellDisplayMode isHovered={isHovered}> <TableCellDisplayMode>
{editModeContentOnly ? editModeContent : nonEditModeContent} {editModeContentOnly ? editModeContent : nonEditModeContent}
</TableCellDisplayMode> </TableCellDisplayMode>
</> </>

View File

@ -19,7 +19,7 @@ const StyledEditableCellDisplayModeOuterContainer = styled.div<
padding-right: ${({ theme }) => theme.spacing(1)}; padding-right: ${({ theme }) => theme.spacing(1)};
width: 100%; width: 100%;
${(props) => ${(props) =>
props.softFocus || props.isHovered props.softFocus
? `background: ${props.theme.background.transparent.secondary}; ? `background: ${props.theme.background.transparent.secondary};
border-radius: ${props.theme.border.radius.sm}; border-radius: ${props.theme.border.radius.sm};
outline: 1px solid ${props.theme.font.color.extraLight};` outline: 1px solid ${props.theme.font.color.extraLight};`
@ -39,14 +39,12 @@ export const TableCellDisplayContainer = ({
softFocus, softFocus,
onClick, onClick,
scrollRef, scrollRef,
isHovered,
}: React.PropsWithChildren<EditableCellDisplayContainerProps>) => ( }: React.PropsWithChildren<EditableCellDisplayContainerProps>) => (
<StyledEditableCellDisplayModeOuterContainer <StyledEditableCellDisplayModeOuterContainer
data-testid={ data-testid={
softFocus ? 'editable-cell-soft-focus-mode' : 'editable-cell-display-mode' softFocus ? 'editable-cell-soft-focus-mode' : 'editable-cell-display-mode'
} }
onClick={onClick} onClick={onClick}
isHovered={isHovered}
softFocus={softFocus} softFocus={softFocus}
ref={scrollRef} ref={scrollRef}
> >

View File

@ -7,8 +7,7 @@ import { TableCellDisplayContainer } from './TableCellDisplayContainer';
export const TableCellDisplayMode = ({ export const TableCellDisplayMode = ({
children, children,
isHovered, }: React.PropsWithChildren<unknown>) => {
}: React.PropsWithChildren<unknown> & { isHovered?: boolean }) => {
const setSoftFocusOnCurrentCell = useSetSoftFocusOnCurrentTableCell(); const setSoftFocusOnCurrentCell = useSetSoftFocusOnCurrentTableCell();
const isFieldInputOnly = useIsFieldInputOnly(); const isFieldInputOnly = useIsFieldInputOnly();
@ -24,7 +23,7 @@ export const TableCellDisplayMode = ({
}; };
return ( return (
<TableCellDisplayContainer isHovered={isHovered} onClick={handleClick}> <TableCellDisplayContainer onClick={handleClick}>
{children} {children}
</TableCellDisplayContainer> </TableCellDisplayContainer>
); );

View File

@ -1,9 +1,9 @@
import { useRecoilCallback, useRecoilState } from 'recoil'; import { useRecoilCallback, useSetRecoilState } from 'recoil';
import { isDragSelectionStartEnabledState } from '../states/internal/isDragSelectionStartEnabledState'; import { isDragSelectionStartEnabledState } from '../states/internal/isDragSelectionStartEnabledState';
export const useDragSelect = () => { export const useDragSelect = () => {
const [, setIsDragSelectionStartEnabled] = useRecoilState( const setIsDragSelectionStartEnabled = useSetRecoilState(
isDragSelectionStartEnabledState, isDragSelectionStartEnabledState,
); );