Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BodyCell): prevent cell or row refocus when overlay clicked #7401

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions components/lib/datatable/BodyCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const BodyCell = React.memo((props) => {
const keyHelperRef = React.useRef(null);
const overlayEventListener = React.useRef(null);
const selfClick = React.useRef(false);
const focusTimeout = React.useRef(null);
const initFocusTimeout = React.useRef(null);
const editingRowDataStateRef = React.useRef(null);
const { ptm, ptmo, cx } = props.ptCallbacks;
Expand Down Expand Up @@ -65,12 +66,13 @@ export const BodyCell = React.memo((props) => {
const [bindDocumentClickListener, unbindDocumentClickListener] = useEventListener({
type: 'click',
listener: (e) => {
if (!selfClick.current && isOutsideClicked(e.target)) {
// #2666 for overlay components and outside is clicked
setTimeout(() => {
setTimeout(() => {
if (!selfClick.current && isOutsideClicked(e.target)) {
// #2666 for overlay components and outside is clicked

switchCellToViewMode(e, true);
}, 0);
}
}
}, 0);

selfClick.current = false;
},
Expand Down Expand Up @@ -228,13 +230,16 @@ export const BodyCell = React.memo((props) => {
};

const focusOnElement = () => {
if (editingState) {
const focusableEl = props.editMode === 'cell' ? DomHandler.getFirstFocusableElement(elementRef.current, ':not([data-pc-section="editorkeyhelperlabel"])') : DomHandler.findSingle(elementRef.current, '[data-p-row-editor-save="true"]');
clearTimeout(focusTimeout.current);
focusTimeout.current = setTimeout(() => {
if (editingState) {
const focusableEl = props.editMode === 'cell' ? DomHandler.getFirstFocusableElement(elementRef.current, ':not([data-pc-section="editorkeyhelperlabel"])') : DomHandler.findSingle(elementRef.current, '[data-p-row-editor-save="true"]');

focusableEl && focusableEl.focus();
}
focusableEl && focusableEl.focus();
}

keyHelperRef.current && (keyHelperRef.current.tabIndex = editingState ? -1 : 0);
keyHelperRef.current && (keyHelperRef.current.tabIndex = editingState ? -1 : 0);
}, 1);
};

const focusOnInit = () => {
Expand Down Expand Up @@ -506,7 +511,7 @@ export const BodyCell = React.memo((props) => {
if (props.editMode === 'cell' || props.editMode === 'row') {
focusOnElement();
}
});
}, [props.editMode, props.editing, editingState]); // eslint-disable-line react-hooks/exhaustive-deps

React.useEffect(() => {
if (props.editMode === 'row' && props.editing !== editingState) {
Expand Down
Loading