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: Datatable text will be selected when resized #5733

Merged
merged 2 commits into from
Jan 11, 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
24 changes: 21 additions & 3 deletions components/lib/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export const DataTable = React.forwardRef((inProps, ref) => {
const dropPosition = React.useRef(null);
const styleElement = React.useRef(null);
const responsiveStyleElement = React.useRef(null);
const beforeResizeStyleElement = React.useRef(null);

const columnWidthsState = React.useRef(null);
const tableWidthState = React.useRef(null);
const resizeColumn = React.useRef(null);
Expand Down Expand Up @@ -504,6 +506,7 @@ export const DataTable = React.forwardRef((inProps, ref) => {
};

const onColumnResizeStart = (e) => {
createBeforeResizeStyleElement();
const { originalEvent: event, column } = e;
const containerLeft = DomHandler.getOffset(elementRef.current).left;

Expand All @@ -519,7 +522,7 @@ export const DataTable = React.forwardRef((inProps, ref) => {
const containerLeft = DomHandler.getOffset(elementRef.current).left;

elementRef.current.setAttribute('data-p-unselectable-text', true);
!ptCallbacks.isUnstyled() && DomHandler.addClass(elementRef.current, 'p-unselectable-text');

resizeHelperRef.current.style.height = elementRef.current.offsetHeight + 'px';
resizeHelperRef.current.style.top = 0 + 'px';
resizeHelperRef.current.style.left = event.pageX - containerLeft + elementRef.current.scrollLeft + 'px';
Expand Down Expand Up @@ -579,8 +582,7 @@ export const DataTable = React.forwardRef((inProps, ref) => {
resizeColumn.current = null;
resizeColumnElement.current = null;
elementRef.current.setAttribute('data-p-unselectable-text', 'true');
!ptCallbacks.isUnstyled() && DomHandler.removeClass(elementRef.current, 'p-unselectable-text');

destroyBeforeResizeStyleElement();
unbindColumnResizeEvents();
};

Expand Down Expand Up @@ -790,6 +792,17 @@ export const DataTable = React.forwardRef((inProps, ref) => {
}
};

const createBeforeResizeStyleElement = () => {
beforeResizeStyleElement.current = DomHandler.createInlineStyle((context && context.nonce) || PrimeReact.nonce, context && context.styleContainer);
let innerHTML = `
[data-pc-name="datatable"][${attributeSelector.current}] {
user-select:none;
}
`;

beforeResizeStyleElement.current.innerHTML = innerHTML;
};

const createStyleElement = () => {
styleElement.current = DomHandler.createInlineStyle((context && context.nonce) || PrimeReact.nonce, context && context.styleContainer);
};
Expand Down Expand Up @@ -843,6 +856,10 @@ export const DataTable = React.forwardRef((inProps, ref) => {
styleElement.current = DomHandler.removeInlineStyle(styleElement.current);
};

const destroyBeforeResizeStyleElement = () => {
beforeResizeStyleElement.current = DomHandler.removeInlineStyle(beforeResizeStyleElement.current);
};

const onPageChange = (e) => {
clearEditingMetaData();

Expand Down Expand Up @@ -1468,6 +1485,7 @@ export const DataTable = React.forwardRef((inProps, ref) => {
unbindColumnResizeEvents();
destroyStyleElement();
destroyResponsiveStyle();
destroyBeforeResizeStyleElement();
});

React.useImperativeHandle(ref, () => ({
Expand Down
Loading