Skip to content

Commit

Permalink
Update element helpers (#570)
Browse files Browse the repository at this point in the history
* replace removed from context for helpers and added to execute code

* test added

* execute helpers updated; test:cii updated

* migration added

* upd test coverage

* Update CHANGELOG.md

* Update CHANGELOG.md

* Updates

---------

Co-authored-by: Mikhail Volkov <[email protected]>
  • Loading branch information
vitPinchuk and mikhail-vl authored Dec 23, 2024
1 parent 7fbfabf commit 48a9876
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 149 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

## 5.0.0 (IN PROGRESS)

### Breaking changes

- Dashboard variables in Show If, Disabled and Options replace automatically. Function `replaceVariables` is not required and removed.

### Features / Enhancements

- Updated change value behavior for custom option (#562)
- Added form-element of type color (#561)
- Updated new lines in Text Area and Code Editor (#563)
- Added support variables in data sources (#564)
- Updated packages for Code Editor (#569)
- Updated element helpers (#570)

## 4.9.0 (2024-11-16)

Expand Down
18 changes: 7 additions & 11 deletions provisioning/dashboards/panels.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 3,
"id": 4,
"links": [],
"panels": [
{
Expand Down Expand Up @@ -120,7 +120,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -245,7 +244,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -346,7 +344,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -494,7 +491,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -641,7 +637,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -822,7 +817,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -869,7 +863,7 @@
"elements": [
{
"id": "amount",
"showIf": "const test = context.grafana.replaceVariables('$var');\nreturn test === 'test'",
"showIf": "const test = '$var'\nreturn test === 'test'",
"title": "Amount",
"type": "number",
"uid": "16cf7be7-c7b0-4742-afa8-e069f5e06558",
Expand Down Expand Up @@ -905,14 +899,17 @@
"id": "select",
"options": [
{
"id": "max",
"label": "Max",
"value": "max"
},
{
"id": "min",
"label": "Min",
"value": "min"
},
{
"id": "avg",
"label": "Average",
"value": "avg"
}
Expand All @@ -932,10 +929,12 @@
"id": "radio",
"options": [
{
"id": "plus",
"label": "Plus",
"value": "plus"
},
{
"id": "minus",
"label": "Minus",
"value": "minus"
}
Expand Down Expand Up @@ -1039,7 +1038,6 @@
"width": 0,
"widthMode": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -1251,7 +1249,6 @@
"width": 0,
"widthMode": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down Expand Up @@ -1358,7 +1355,6 @@
},
"updateEnabled": "auto"
},
"pluginVersion": "4.9.0",
"targets": [
{
"refId": "A"
Expand Down
8 changes: 4 additions & 4 deletions src/components/CustomButtonsRow/CustomButtonsRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ export const CustomButtonsRow: React.FC<Props> = ({ elements, initial, replaceVa
const visibleElements = useMemo(() => {
return elements
.filter((element) => {
return element.helpers.showIf({ elements, replaceVariables });
return element.helpers.showIf({ elements });
})
.map((element) => ({
...element,
disabled: element.helpers.disableIf({ elements, replaceVariables }),
options: element.helpers.getOptions({ elements, replaceVariables, data }),
disabled: element.helpers.disableIf({ elements }),
options: element.helpers.getOptions({ elements, data }),
}));
}, [data, elements, replaceVariables]);
}, [data, elements]);

return (
<>
Expand Down
6 changes: 3 additions & 3 deletions src/components/FormElements/FormElements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ export const FormElements: React.FC<Props> = ({
const visibleElements = useMemo(() => {
return elements
.filter((element) => {
return element.helpers.showIf({ elements, replaceVariables });
return element.helpers.showIf({ elements });
})
.map((element) => ({
...element,
title: replaceVariables(element.title),
disabled: element.helpers.disableIf({ elements, replaceVariables }),
options: element.helpers.getOptions({ elements, replaceVariables, data }),
disabled: element.helpers.disableIf({ elements }),
options: element.helpers.getOptions({ elements, data }),
}));
}, [data, elements, replaceVariables]);

Expand Down
3 changes: 2 additions & 1 deletion src/components/FormElementsEditor/FormElementsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const FormElementsEditor: React.FC<Props> = ({ value, onChange, context }
onChangeElementOption,
onElementRemove,
} = useFormLayout({
replaceVariables: context.replaceVariables,
onChangeElementsOption: onChange,
value,
});
Expand Down Expand Up @@ -187,7 +188,7 @@ export const FormElementsEditor: React.FC<Props> = ({ value, onChange, context }
</div>

<hr />
<NewElement elements={elements} onSave={onElementAdd} />
<NewElement elements={elements} onSave={onElementAdd} replaceVariables={context.replaceVariables} />
</div>
);
};
6 changes: 2 additions & 4 deletions src/components/FormPanel/FormPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const FormPanel: React.FC<Props> = ({
getSection,
getAllSections,
} = useFormLayout({
replaceVariables: replaceVariables,
onChangeElementsOption: onChangeOptions,
value: options.elements,
isAutoSave: false,
Expand Down Expand Up @@ -1187,10 +1188,7 @@ export const FormPanel: React.FC<Props> = ({
/**
* Skip hidden element
*/
if (
('hidden' in element && element.hidden) ||
!element.helpers.showIf({ elements, replaceVariables })
) {
if (('hidden' in element && element.hidden) || !element.helpers.showIf({ elements })) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/InitialFieldsEditor/InitialFieldsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const InitialFieldsEditor: React.FC<Props> = ({ value, onChange, context
* Form Elements State
*/
const { elements, isChanged, onSaveUpdates, onChangeElement } = useFormLayout({
replaceVariables: context.replaceVariables,
onChangeElementsOption: onChange,
value,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const LayoutSectionsEditor: React.FC<Props> = ({ value, onChange, context
* Sections
*/
const { sections, onChangeSections } = useFormLayout({
replaceVariables: context.replaceVariables,
onChangeSectionsOption: onChange,
layoutSections: value,
});
Expand Down
13 changes: 9 additions & 4 deletions src/components/NewElement/NewElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectableValue } from '@grafana/data';
import { InterpolateFunction, SelectableValue } from '@grafana/data';
import { Alert, Button, CollapsableSection, InlineField, Input, Select } from '@grafana/ui';
import React, { ChangeEvent, useCallback, useState } from 'react';

Expand All @@ -19,12 +19,17 @@ interface Props {
* On Save
*/
onSave: (element: LocalFormElement) => void;

/**
* Template variables interpolation function
*/
replaceVariables?: InterpolateFunction;
}

/**
* New Element
*/
export const NewElement: React.FC<Props> = ({ onSave, elements }) => {
export const NewElement: React.FC<Props> = ({ onSave, elements, replaceVariables }) => {
/**
* States
*/
Expand All @@ -51,13 +56,13 @@ export const NewElement: React.FC<Props> = ({ onSave, elements }) => {
/**
* Save Element
*/
onSave(getElementWithNewType(toLocalFormElement(newElement), newElement.type));
onSave(getElementWithNewType(toLocalFormElement(newElement, replaceVariables), newElement.type));

/**
* Reset input values
*/
setNewElement(FORM_ELEMENT_DEFAULT);
}, [elements, newElement, onSave]);
}, [elements, newElement, onSave, replaceVariables]);

return (
<CollapsableSection
Expand Down
16 changes: 10 additions & 6 deletions src/hooks/useFormLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventBusSrv, SelectableValue } from '@grafana/data';
import { EventBusSrv, InterpolateFunction, SelectableValue } from '@grafana/data';
import { useCallback, useEffect, useRef, useState } from 'react';

import { FormElement, LayoutSection, LayoutSectionWithElements, LocalFormElement } from '../types';
Expand Down Expand Up @@ -30,7 +30,9 @@ export const useFormLayout = ({
value,
isAutoSave = true,
layoutSections,
replaceVariables,
}: {
replaceVariables?: InterpolateFunction;
onChangeElementsOption?: (elements: FormElement[]) => void;
value?: FormElement[];
isAutoSave?: boolean;
Expand All @@ -43,7 +45,7 @@ export const useFormLayout = ({
* States
*/
const [elements, setElements, elementsRef] = useMutableState<LocalFormElement[]>(
normalizeElementsForLocalState(value)
normalizeElementsForLocalState(value, replaceVariables)
);
const [sections, setSections, sectionsRef] = useMutableState<LayoutSection[]>(layoutSections || []);
const [isChanged, setIsChanged] = useState(false);
Expand Down Expand Up @@ -209,18 +211,20 @@ export const useFormLayout = ({
*/
const setFormValue = useCallback(
(objectValues: Record<string, unknown>) => {
onChangeElements(setFormValueHandler(elementsRef.current, normalizeElementsForLocalState(value), objectValues));
onChangeElements(
setFormValueHandler(elementsRef.current, normalizeElementsForLocalState(value, replaceVariables), objectValues)
);
},
[elementsRef, onChangeElements, value]
[elementsRef, onChangeElements, replaceVariables, value]
);

/**
* Update local elements
*/
useEffect(() => {
setElements(normalizeElementsForLocalState(value));
setElements(normalizeElementsForLocalState(value, replaceVariables));
setIsChanged(false);
}, [setElements, value]);
}, [replaceVariables, setElements, value]);

/**
* Add new section
Expand Down
Loading

0 comments on commit 48a9876

Please sign in to comment.