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 DatagridConfigurable editor allows to drag fields outside its list #9351

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 34 additions & 6 deletions packages/ra-ui-materialui/src/preferences/FieldToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ export const FieldToggle = props => {
// imperative DOM manipulations using the native Drag API
const selectedItem = event.target;
selectedItem.classList.add('drag-active');
const list = selectedItem.parentNode;
const list = selectedItem.closest('ul');
let dropItem =
document.elementFromPoint(x.current, y.current) === null
? selectedItem
: document.elementFromPoint(x.current, y.current);
: document.elementFromPoint(x.current, y.current).closest('li');

if (!dropItem) {
return;
}
if (dropItem.classList.contains('dragIcon')) {
dropItem = dropItem.parentNode;
}
if (dropItem === selectedItem) {
return;
}
if (list === dropItem.parentNode) {
if (list === dropItem.parentNode.closest('ul')) {
dropIndex.current = dropItem.dataset.index;
if (dropItem === selectedItem.nextSibling) {
dropItem = dropItem.nextSibling;
Expand All @@ -48,8 +52,31 @@ export const FieldToggle = props => {
};

const handleDragEnd = event => {
const selectedItem = event.target;
onMove(selectedItem.dataset.index, dropIndex.current);
const selectedItem = event.target as HTMLElement;
const list = selectedItem.closest('ul');
let dropItem =
document.elementFromPoint(x.current, y.current) === null
? selectedItem
: document.elementFromPoint(x.current, y.current).closest('li');

if (!dropItem) {
if (
y.current >
selectedItem.closest('ul').getBoundingClientRect().bottom
) {
dropItem = list.lastChild as HTMLElement;
} else {
dropItem = list.firstChild as HTMLElement;
}
}

console.log({ dropItem });
djhi marked this conversation as resolved.
Show resolved Hide resolved
if (dropItem && list === dropItem.closest('ul')) {
onMove(selectedItem.dataset.index, dropIndex.current);
} else {
event.preventDefault();
event.stopPropagation();
}
selectedItem.classList.remove('drag-active');
document.removeEventListener('dragover', handleDocumentDragOver);
};
Expand Down Expand Up @@ -97,9 +124,10 @@ export const FieldToggle = props => {
);
};

const Root = styled('div')(({ theme }) => ({
const Root = styled('li')(({ theme }) => ({
display: 'flex',
justifyContent: 'space-between',
paddingLeft: 0,
'& svg': {
cursor: 'move',
},
Expand Down
32 changes: 20 additions & 12 deletions packages/ra-ui-materialui/src/preferences/FieldsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,26 @@ export const FieldsSelector = ({

return (
<Box pt={0.5}>
{availableFields.map(field => (
<FieldToggle
key={field.index}
source={field.source}
label={field.label}
index={field.index}
selected={fields.includes(field.index)}
onToggle={handleToggle}
onMove={handleMove}
/>
))}
<Box display="flex" justifyContent="space-between" mx={-0.5} mt={1}>
<Box component="ul" sx={{ paddingInlineStart: 0, m: 0 }}>
{availableFields.map(field => (
<FieldToggle
key={field.index}
source={field.source}
label={field.label}
index={field.index}
selected={fields.includes(field.index)}
onToggle={handleToggle}
onMove={handleMove}
/>
))}
</Box>
<Box
onDrop={e => e.preventDefault()}
display="flex"
justifyContent="space-between"
mx={-0.5}
mt={1}
>
<Button size="small" onClick={handleHideAll}>
{translate('ra.inspector.hideAll', {
_: 'Hide All',
Expand Down
Loading