From 104c4a7630c60ad364fedc89b838b14b51e91a74 Mon Sep 17 00:00:00 2001 From: An Date: Thu, 2 Jun 2022 12:48:07 -0400 Subject: [PATCH] TestData: Add schema-based form for simulation (#49637) * add inputs for corresponding schema * change input to default value * reformat inline fields and keep local state * add comment on state * show input on default --- .../components/SimulationQueryEditor.tsx | 53 +++++------ .../components/SimulationSchemaForm.tsx | 89 +++++++++++++++++++ 2 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx diff --git a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx index 7fa456f2cb6..fef2b7c00dc 100644 --- a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx @@ -1,23 +1,14 @@ -import React, { FormEvent, useMemo } from 'react'; +import React, { FormEvent, useMemo, useState } from 'react'; import { useAsync } from 'react-use'; import { DataFrameJSON, SelectableValue } from '@grafana/data'; -import { - InlineField, - InlineFieldRow, - Button, - FieldSet, - InlineSwitch, - Input, - Label, - Select, - Form, - TextArea, -} from '@grafana/ui'; +import { InlineField, InlineFieldRow, InlineSwitch, Input, Label, Select } from '@grafana/ui'; import { EditorProps } from '../QueryEditor'; import { SimulationQuery } from '../types'; +import { SimulationSchemaForm } from './SimulationSchemaForm'; + // Type string `json:"type"` // Name string `json:"name"` // Description string `json:"description"` @@ -31,12 +22,12 @@ interface SimInfo { forward: boolean; config: DataFrameJSON; } -interface FormDTO { - config: string; -} + export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { const simQuery = query.sim ?? ({} as SimulationQuery); const simKey = simQuery.key ?? ({} as typeof simQuery.key); + // keep track of updated config state to pass down to form + const [cfgValue, setCfgValue] = useState>({}); // This only changes once const info = useAsync(async () => { @@ -63,7 +54,9 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { if (simKey.uid) { path += '/' + simKey.uid; } - return (await ds.getResource('sim/' + path))?.config; + let config = (await ds.getResource('sim/' + path))?.config; + setCfgValue(config.value); + return config; }, [simKey.type, simKey.tick, simKey.uid]); const onUpdateKey = (key: typeof simQuery.key) => { @@ -91,14 +84,16 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { const onToggleLast = () => { onChange({ ...query, sim: { ...simQuery, last: !simQuery.last } }); }; - const onSubmitChange = (data: FormDTO) => { + + const onSchemaFormChange = (config: Record) => { let path = simKey.type + '/' + simKey.tick + 'hz'; if (simKey.uid) { path += '/' + simKey.uid; } - ds.postResource('sim/' + path, JSON.parse(data.config)); + ds.postResource('sim/' + path, config).then((res) => { + setCfgValue(res.config); + }); }; - return ( <> @@ -112,7 +107,6 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { /> - @@ -139,18 +133,11 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { -
-
- {({ register }) => ( -
-