sql: extract frontend code into separate package (#81109)

* sql: extract frontend code into separate package

* updated package version
This commit is contained in:
Gábor Farkas
2024-01-26 11:38:29 +01:00
committed by GitHub
parent 2febbec758
commit 29e8a355cb
87 changed files with 187 additions and 104 deletions

View File

@ -0,0 +1,59 @@
import React, { useCallback } from 'react';
import { SelectableValue, toOption } from '@grafana/data';
import { AccessoryButton, EditorList, InputGroup } from '@grafana/experimental';
import { Select } from '@grafana/ui';
import { QueryEditorGroupByExpression } from '../../expressions';
import { SQLExpression } from '../../types';
import { setGroupByField } from '../../utils/sql.utils';
interface GroupByRowProps {
sql: SQLExpression;
onSqlChange: (sql: SQLExpression) => void;
columns?: Array<SelectableValue<string>>;
}
export function GroupByRow({ sql, columns, onSqlChange }: GroupByRowProps) {
const onGroupByChange = useCallback(
(item: Array<Partial<QueryEditorGroupByExpression>>) => {
// As new (empty object) items come in, we need to make sure they have the correct type
const cleaned = item.map((v) => setGroupByField(v.property?.name));
const newSql = { ...sql, groupBy: cleaned };
onSqlChange(newSql);
},
[onSqlChange, sql]
);
return (
<EditorList
items={sql.groupBy!}
onChange={onGroupByChange}
renderItem={makeRenderColumn({
options: columns,
})}
/>
);
}
function makeRenderColumn({ options }: { options?: Array<SelectableValue<string>> }) {
const renderColumn = function (
item: Partial<QueryEditorGroupByExpression>,
onChangeItem: (item: QueryEditorGroupByExpression) => void,
onDeleteItem: () => void
) {
return (
<InputGroup>
<Select
value={item.property?.name ? toOption(item.property.name) : null}
aria-label="Group by"
options={options}
menuShouldPortal
onChange={({ value }) => value && onChangeItem(setGroupByField(value))}
/>
<AccessoryButton aria-label="Remove group by column" icon="times" variant="secondary" onClick={onDeleteItem} />
</InputGroup>
);
};
return renderColumn;
}