Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 16 additions & 4 deletions packages/amis-ui/src/components/condition-builder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class QueryBuilder extends React.Component<
config = {...defaultConfig, ...this.props.config};

dragTarget?: HTMLElement;
dragContext?: {
value?: AMISConditionGroupValue;
onChange: (value: AMISConditionGroupValue) => void;
};
// dragNextSibling: Element | null;
ghost?: HTMLElement;
host: HTMLElement;
Expand All @@ -79,10 +83,15 @@ export class QueryBuilder extends React.Component<
lastMoveAt: number = 0;

@autobind
handleDragStart(e: React.DragEvent) {
handleDragStart(
e: React.DragEvent,
value: AMISConditionGroupValue | undefined,
onChange: (value: AMISConditionGroupValue) => void
) {
const {draggable = true} = this.props;
// draggable为false时不可拖拽
if (!draggable) return;
this.dragContext = {value, onChange};
const target = e.currentTarget;
const item = target.closest('[data-id]') as HTMLElement;
this.dragTarget = item;
Expand Down Expand Up @@ -182,7 +191,7 @@ export class QueryBuilder extends React.Component<

@autobind
handleDragDrop() {
const onChange = this.props.onChange;
const onChange = this.dragContext!.onChange;
const fromId = this.dragTarget!.getAttribute('data-id')!;
const toId = this.host.getAttribute('data-group-id')!;
const children = [].slice.call(this.ghost!.parentElement!.children);
Expand All @@ -193,7 +202,7 @@ export class QueryBuilder extends React.Component<
}

const toIndex = children.indexOf(this.ghost);
let value = this.props.value!;
let value = this.dragContext!.value!;

const indexes = findTreeIndex([value], item => item.id === fromId);

Expand Down Expand Up @@ -228,6 +237,7 @@ export class QueryBuilder extends React.Component<
// this.dragTarget.parentElement!.appendChild(this.dragTarget);
// }
delete this.dragTarget;
delete this.dragContext;
// delete this.dragNextSibling;
this.ghost!.parentElement?.removeChild(this.ghost!);
delete this.ghost;
Expand Down Expand Up @@ -327,7 +337,9 @@ export class QueryBuilder extends React.Component<
classnames={cx}
fieldClassName={fieldClassName}
removeable={false}
onDragStart={this.handleDragStart}
onDragStart={(e: React.DragEvent) =>
this.handleDragStart(e, normalizedValue, onChange)
}
showANDOR={showANDOR}
showNot={showNot}
data={data}
Expand Down
112 changes: 112 additions & 0 deletions packages/amis/__tests__/renderers/Form/conditionBuilderDrag.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';
import {fireEvent, render, waitFor} from '@testing-library/react';
import {ConditionBuilder} from 'amis-ui';

const fields = [
{label: 'AText', type: 'text' as const, name: 'a'},
{label: 'BText', type: 'text' as const, name: 'b'}
];

const initialValue = {
id: 'root',
conjunction: 'and' as const,
children: fields.map(field => ({
id: field.name,
left: {type: 'field' as const, field: field.name},
op: 'equal' as const,
right: field.name
}))
};

function dragFirstAfterSecond(container: HTMLElement) {
const handles = container.querySelectorAll('.cxd-CBGroupOrItem-dragbar');
expect(handles).toHaveLength(2);
const start = new MouseEvent('dragstart', {bubbles: true, clientY: 0});
Object.defineProperty(start, 'dataTransfer', {
value: {setDragImage: jest.fn()}
});
fireEvent(handles[0], start);
fireEvent(
handles[1],
new MouseEvent('dragover', {bubbles: true, clientY: 100})
);
fireEvent(handles[1], new MouseEvent('drop', {bubbles: true}));
fireEvent(handles[0], new MouseEvent('dragend', {bubbles: true}));
}

test('condition-builder sorts new picker conditions before confirmation', async () => {
const onChange = jest.fn();
const {baseElement, findByText} = render(
<ConditionBuilder
embed={false}
fields={fields}
placeholder="Configure conditions"
onChange={onChange}
/>
);

fireEvent.click(await findByText('Configure conditions'));
for (const field of fields) {
fireEvent.click(await findByText('添加条件'));
fireEvent.click(await findByText('请选择字段'));
fireEvent.click(await findByText(field.label));
}

dragFirstAfterSecond(baseElement);
expect(onChange).not.toHaveBeenCalled();
fireEvent.click(await findByText('确认'));
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
expect(
onChange.mock.calls[0][0].children.map((item: any) => item.left.field)
).toEqual(['b', 'a']);
});

test('condition-builder cancellation discards picker sorting', async () => {
const onChange = jest.fn();
const {baseElement, findByText} = render(
<ConditionBuilder
embed={false}
fields={fields}
value={initialValue}
onChange={onChange}
/>
);

fireEvent.click(baseElement.querySelector('.cxd-ResultBox')!);
await findByText('AText');
dragFirstAfterSecond(baseElement);
expect(onChange).not.toHaveBeenCalled();
const order = () =>
Array.from(
baseElement.querySelectorAll('.cxd-Modal .cxd-CBGroupOrItem')
).map(item => item.getAttribute('data-id'));
expect(order()).toEqual(['b', 'a']);
fireEvent.click(await findByText('取消'));
expect(onChange).not.toHaveBeenCalled();
expect(initialValue.children.map(item => item.left.field)).toEqual([
'a',
'b'
]);
await waitFor(() => expect(order()).toEqual([]));
fireEvent.click(baseElement.querySelector('.cxd-ResultBox')!);
await findByText('AText');
expect(order()).toEqual(['a', 'b']);
});

test('condition-builder embedded sorting updates immediately', async () => {
const onChange = jest.fn();
const {baseElement, findByText} = render(
<ConditionBuilder
fields={fields}
value={initialValue}
onChange={onChange}
/>
);

await findByText('AText');
dragFirstAfterSecond(baseElement);
expect(onChange).toHaveBeenCalledTimes(1);
expect(
onChange.mock.calls[0][0].children.map((item: any) => item.left.field)
).toEqual(['b', 'a']);
});
Loading