diff --git a/frontend/src/components/ColouredEnumComponent.tsx b/frontend/src/components/ColouredEnumComponent.tsx deleted file mode 100644 index f83bc1b..0000000 --- a/frontend/src/components/ColouredEnumComponent.tsx +++ /dev/null @@ -1,100 +0,0 @@ -import React, { useEffect, useRef, useState } from 'react'; -import { InputGroup, Form, Row, Col } from 'react-bootstrap'; -import { DocStringComponent } from './DocStringComponent'; -import { LevelName } from './NotificationsComponent'; - -type ColouredEnumComponentProps = { - name: string; - parentPath: string; - value: string; - docString?: string; - readOnly: boolean; - enumDict: Record; - addNotification: (message: string, levelname?: LevelName) => void; - changeCallback?: ( - value: unknown, - attributeName?: string, - prefix?: string, - callback?: (ack: unknown) => void - ) => void; - displayName: string; - id: string; -}; - -export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentProps) => { - const { - name, - value, - docString, - enumDict, - readOnly, - addNotification, - displayName, - id - } = props; - let { changeCallback } = props; - if (changeCallback === undefined) { - changeCallback = (value: string) => { - setEnumValue(() => { - return value; - }); - }; - } - - const renderCount = useRef(0); - const [enumValue, setEnumValue] = useState(value); - - const fullAccessPath = [props.parentPath, props.name] - .filter((element) => element) - .join('.'); - - useEffect(() => { - renderCount.current++; - }); - - useEffect(() => { - setEnumValue(() => { - return props.value; - }); - addNotification(`${fullAccessPath} changed to ${value}.`); - }, [props.value]); - - return ( -
- {process.env.NODE_ENV === 'development' && ( -
Render count: {renderCount.current}
- )} - - - - {displayName} - - - {readOnly ? ( - // Display the Form.Control when readOnly is true - - ) : ( - // Display the Form.Select when readOnly is false - changeCallback(event.target.value)}> - {Object.entries(enumDict).map(([key]) => ( - - ))} - - )} - - -
- ); -}); diff --git a/frontend/src/components/EnumComponent.tsx b/frontend/src/components/EnumComponent.tsx index f8ef4bd..a8f9604 100644 --- a/frontend/src/components/EnumComponent.tsx +++ b/frontend/src/components/EnumComponent.tsx @@ -1,63 +1,58 @@ import React, { useEffect, useRef, useState } from 'react'; import { InputGroup, Form, Row, Col } from 'react-bootstrap'; import { DocStringComponent } from './DocStringComponent'; +import { SerializedValue } from './GenericComponent'; import { LevelName } from './NotificationsComponent'; -type EnumComponentProps = { +export type EnumSerialization = { + type: 'Enum' | 'ColouredEnum'; + full_access_path: string; name: string; - parentPath: string; value: string; - docString?: string; - readOnly: boolean; - enumDict: Record; + readonly: boolean; + doc?: string | null; + enum: Record; +}; + +type EnumComponentProps = { + attribute: EnumSerialization; addNotification: (message: string, levelname?: LevelName) => void; - changeCallback?: ( - value: unknown, - attributeName?: string, - prefix?: string, - callback?: (ack: unknown) => void - ) => void; displayName: string; id: string; + changeCallback?: (value: SerializedValue, callback?: (ack: unknown) => void) => void; }; export const EnumComponent = React.memo((props: EnumComponentProps) => { + const { attribute, addNotification, displayName, id } = props; const { - name, + full_access_path: fullAccessPath, value, - docString, - enumDict, - addNotification, - displayName, - id, - readOnly - } = props; + doc: docString, + enum: enumDict, + readonly: readOnly + } = attribute; let { changeCallback } = props; if (changeCallback === undefined) { - changeCallback = (value: string) => { + changeCallback = (value: SerializedValue) => { setEnumValue(() => { - return value; + return String(value.value); }); }; } const renderCount = useRef(0); const [enumValue, setEnumValue] = useState(value); - const fullAccessPath = [props.parentPath, props.name] - .filter((element) => element) - .join('.'); - useEffect(() => { renderCount.current++; }); useEffect(() => { setEnumValue(() => { - return props.value; + return value; }); addNotification(`${fullAccessPath} changed to ${value}.`); - }, [props.value]); + }, [value]); return (
@@ -73,17 +68,35 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => { {readOnly ? ( // Display the Form.Control when readOnly is true - + ) : ( // Display the Form.Select when readOnly is false changeCallback(event.target.value)}> + name={fullAccessPath} + style={ + attribute.type == 'ColouredEnum' + ? { backgroundColor: enumDict[enumValue] } + : {} + } + onChange={(event) => + changeCallback({ + type: attribute.type, + name: attribute.name, + enum: enumDict, + value: event.target.value, + full_access_path: fullAccessPath, + readonly: true + }) + }> {Object.entries(enumDict).map(([key, val]) => ( ))} diff --git a/frontend/src/components/GenericComponent.tsx b/frontend/src/components/GenericComponent.tsx index 98b2e0c..ed2afe0 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 } from './NumberComponent'; import { SliderComponent } from './SliderComponent'; -import { EnumComponent } from './EnumComponent'; +import { EnumComponent, EnumSerialization } from './EnumComponent'; import { MethodComponent } from './MethodComponent'; import { AsyncMethodComponent } from './AsyncMethodComponent'; import { StringComponent } from './StringComponent'; @@ -10,7 +10,6 @@ import { ListComponent } from './ListComponent'; import { DataServiceComponent, DataServiceJSON } from './DataServiceComponent'; import { DeviceConnectionComponent } from './DeviceConnection'; import { ImageComponent } from './ImageComponent'; -import { ColouredEnumComponent } from './ColouredEnumComponent'; import { LevelName } from './NotificationsComponent'; import { getIdFromFullAccessPath } from '../utils/stringUtils'; import { WebSettingsContext } from '../WebSettings'; @@ -133,14 +132,10 @@ export const GenericComponent = React.memo( id={id} /> ); - } else if (attribute.type === 'Enum') { + } else if (attribute.type === 'Enum' || attribute.type === 'ColouredEnum') { return ( ); - } else if (attribute.type === 'ColouredEnum') { - return ( - - ); } else { return
{fullAccessPath}
; }