diff --git a/frontend/src/components/EnumComponent.tsx b/frontend/src/components/EnumComponent.tsx index 5ddf26d..a16785f 100644 --- a/frontend/src/components/EnumComponent.tsx +++ b/frontend/src/components/EnumComponent.tsx @@ -2,52 +2,32 @@ import React, { useEffect } from "react"; import { InputGroup, Form, Row, Col } from "react-bootstrap"; import { DocStringComponent } from "./DocStringComponent"; import { LevelName } from "./NotificationsComponent"; -import { SerializedObject } from "../types/SerializedObject"; +import { SerializedObject, SerializedEnum } from "../types/SerializedObject"; import { useRenderCount } from "../hooks/useRenderCount"; -export interface EnumSerialization { - type: "Enum" | "ColouredEnum"; - full_access_path: string; - name: string; - value: string; - readonly: boolean; - doc: string | null; - enum: Record; -} - -interface EnumComponentProps { - attribute: EnumSerialization; +interface EnumComponentProps extends SerializedEnum { addNotification: (message: string, levelname?: LevelName) => void; displayName: string; id: string; - changeCallback?: (value: SerializedObject, callback?: (ack: unknown) => void) => void; + changeCallback: (value: SerializedObject, callback?: (ack: unknown) => void) => void; } export const EnumComponent = React.memo((props: EnumComponentProps) => { - const { attribute, addNotification, displayName, id } = props; const { - full_access_path: fullAccessPath, + addNotification, + displayName, + id, value, - doc: docString, + full_access_path: fullAccessPath, enum: enumDict, + doc: docString, readonly: readOnly, - } = attribute; + changeCallback, + } = props; - let { changeCallback } = props; - if (changeCallback === undefined) { - changeCallback = (value: SerializedObject) => { - setEnumValue(() => { - return String(value.value); - }); - }; - } - const [enumValue, setEnumValue] = useState(value); const renderCount = useRenderCount(); useEffect(() => { - setEnumValue(() => { - return value; - }); addNotification(`${fullAccessPath} changed to ${value}.`); }, [value]); @@ -65,11 +45,9 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => { // Display the Form.Control when readOnly is true @@ -77,27 +55,25 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => { // Display the Form.Select when readOnly is false changeCallback({ - type: attribute.type, - name: attribute.name, + type: props.type, + name: props.name, enum: enumDict, value: event.target.value, full_access_path: fullAccessPath, - readonly: attribute.readonly, - doc: attribute.doc, + readonly: props.readonly, + doc: props.doc, }) }> {Object.entries(enumDict).map(([key, val]) => ( ))} diff --git a/frontend/src/components/GenericComponent.tsx b/frontend/src/components/GenericComponent.tsx index 77c8d49..49f5543 100644 --- a/frontend/src/components/GenericComponent.tsx +++ b/frontend/src/components/GenericComponent.tsx @@ -2,7 +2,7 @@ import React, { useContext } from "react"; import { ButtonComponent } from "./ButtonComponent"; import { NumberComponent, NumberObject } from "./NumberComponent"; import { SliderComponent } from "./SliderComponent"; -import { EnumComponent, EnumSerialization } from "./EnumComponent"; +import { EnumComponent } from "./EnumComponent"; import { MethodComponent } from "./MethodComponent"; import { AsyncMethodComponent } from "./AsyncMethodComponent"; import { StringComponent } from "./StringComponent"; @@ -16,7 +16,7 @@ import { WebSettingsContext } from "../WebSettings"; import { updateValue } from "../socket"; import { DictComponent } from "./DictComponent"; import { parseFullAccessPath } from "../utils/stateUtils"; -import { SerializedObject } from "../types/SerializedObject"; +import { SerializedEnum, SerializedObject } from "../types/SerializedObject"; interface GenericComponentProps { attribute: SerializedObject; @@ -136,7 +136,7 @@ export const GenericComponent = React.memo( } else if (attribute.type === "Enum" || attribute.type === "ColouredEnum") { return ( ; + enum: Record; }; type SerializedList = SerializedObjectBase & {