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

Visual Editor: Disallow to choose already selected fields #251

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion src/__mocks__/@grafana/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const Select = jest.fn(({ options, onChange, value, isMulti, isClearable, ...res
if (isMulti) {
onChange(options.filter((option: any) => event.target.values.includes(option.value)));
} else {
onChange(options.find((option: any) => option.value === event.target.value));
const option = options.find((option: any) => option.value === event.target.value);
option && onChange(option);
}
}
}}
Expand Down
20 changes: 20 additions & 0 deletions src/components/DatasetEditor/DatasetEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ describe('Dataset Editor', () => {
expect(selectors.item(true, 'A:Value')).not.toBeInTheDocument();
});

it('Should not allow selecting already selected fields', () => {
const { value, onChange } = createOnChangeHandler([{ name: 'Time', source: 'A' }]);

/**
* Render
*/
render(
getComponent({
value,
onChange,
})
);

/**
* Simulate select option doesn't exist
*/
fireEvent.change(selectors.newItemName(), { target: { value: 'A:Time' } });
expect(selectors.buttonAddNew()).toBeDisabled();
});

/**
* Items order
*/
Expand Down
26 changes: 15 additions & 11 deletions src/components/DatasetEditor/DatasetEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ export const DatasetEditor: React.FC<Props> = ({ value, onChange, data }) => {
* Available Field Options
*/
const availableFieldOptions = useMemo(() => {
return data.reduce((acc: SelectableValue[], dataFrame) => {
return acc.concat(
dataFrame.fields.map((field) => ({
value: `${dataFrame.refId}:${field.name}`,
fieldName: field.name,
label: `${dataFrame.refId ? `${dataFrame.refId}:` : ''}${field.name}`,
source: dataFrame.refId,
}))
);
}, []);
}, [data]);
return data
.reduce((acc: SelectableValue[], dataFrame) => {
return acc.concat(
dataFrame.fields.map((field) => ({
value: `${dataFrame.refId}:${field.name}`,
fieldName: field.name,
label: `${dataFrame.refId ? `${dataFrame.refId}:` : ''}${field.name}`,
source: dataFrame.refId,
}))
);
}, [])
.filter((field) => {
return !items.some((item) => item.name === field.fieldName && item.source === field.source);
});
}, [items, data]);

/**
* Add New Item
Expand Down
Loading