From fffe679bf0e623adf6237332f00fbb3c06670677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Wed, 21 Feb 2024 08:32:59 +0100 Subject: [PATCH] defines changeCallback function in GenericComponent and passes it to components (instead of setAttribute) The components do not use the setAttribute method themselves anymore. This way, you can provide the changeCallback function if you want and thus reuse the components. --- frontend/src/components/ButtonComponent.tsx | 19 ++++++++++++--- .../src/components/ColouredEnumComponent.tsx | 16 +++++++------ frontend/src/components/EnumComponent.tsx | 16 +++++++------ frontend/src/components/GenericComponent.tsx | 18 ++++++++++++++ frontend/src/components/NumberComponent.tsx | 13 ++++++---- frontend/src/components/SliderComponent.tsx | 14 +++++++---- frontend/src/components/StringComponent.tsx | 24 ++++++++++++++----- 7 files changed, 88 insertions(+), 32 deletions(-) diff --git a/frontend/src/components/ButtonComponent.tsx b/frontend/src/components/ButtonComponent.tsx index 4ae75d0..3718051 100644 --- a/frontend/src/components/ButtonComponent.tsx +++ b/frontend/src/components/ButtonComponent.tsx @@ -1,7 +1,6 @@ import React, { useContext, useEffect, useRef } from 'react'; import { WebSettingsContext } from '../WebSettings'; import { ToggleButton } from 'react-bootstrap'; -import { setAttribute } from '../socket'; import { DocStringComponent } from './DocStringComponent'; import { getIdFromFullAccessPath } from '../utils/stringUtils'; import { LevelName } from './NotificationsComponent'; @@ -14,10 +13,24 @@ type ButtonComponentProps = { docString: string; mapping?: [string, string]; // Enforce a tuple of two strings addNotification: (message: string, levelname?: LevelName) => void; + changeCallback?: ( + value: unknown, + attributeName?: string, + prefix?: string, + callback?: (ack: unknown) => void + ) => void; }; export const ButtonComponent = React.memo((props: ButtonComponentProps) => { - const { name, parentPath, value, readOnly, docString, addNotification } = props; + const { + name, + parentPath, + value, + readOnly, + docString, + addNotification, + changeCallback = () => {} + } = props; // const buttonName = props.mapping ? (value ? props.mapping[0] : props.mapping[1]) : name; const fullAccessPath = [parentPath, name].filter((element) => element).join('.'); const id = getIdFromFullAccessPath(fullAccessPath); @@ -39,7 +52,7 @@ export const ButtonComponent = React.memo((props: ButtonComponentProps) => { }, [props.value]); const setChecked = (checked: boolean) => { - setAttribute(name, parentPath, checked); + changeCallback(checked); }; return ( diff --git a/frontend/src/components/ColouredEnumComponent.tsx b/frontend/src/components/ColouredEnumComponent.tsx index f3e9a47..d559169 100644 --- a/frontend/src/components/ColouredEnumComponent.tsx +++ b/frontend/src/components/ColouredEnumComponent.tsx @@ -1,7 +1,6 @@ import React, { useContext, useEffect, useRef } from 'react'; import { WebSettingsContext } from '../WebSettings'; import { InputGroup, Form, Row, Col } from 'react-bootstrap'; -import { setAttribute } from '../socket'; import { DocStringComponent } from './DocStringComponent'; import { getIdFromFullAccessPath } from '../utils/stringUtils'; import { LevelName } from './NotificationsComponent'; @@ -14,6 +13,12 @@ type ColouredEnumComponentProps = { readOnly: boolean; enumDict: Record; addNotification: (message: string, levelname?: LevelName) => void; + changeCallback?: ( + value: unknown, + attributeName?: string, + prefix?: string, + callback?: (ack: unknown) => void + ) => void; }; export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentProps) => { @@ -24,7 +29,8 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro docString, enumDict, readOnly, - addNotification + addNotification, + changeCallback = () => {} } = props; const renderCount = useRef(0); const fullAccessPath = [parentPath, name].filter((element) => element).join('.'); @@ -44,10 +50,6 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro addNotification(`${parentPath}.${name} changed to ${value}.`); }, [props.value]); - const handleValueChange = (newValue: string) => { - setAttribute(name, parentPath, newValue); - }; - return (
{process.env.NODE_ENV === 'development' && ( @@ -72,7 +74,7 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro aria-label="coloured-enum-select" value={value} style={{ backgroundColor: enumDict[value] }} - onChange={(event) => handleValueChange(event.target.value)}> + onChange={(event) => changeCallback(event.target.value)}> {Object.entries(enumDict).map(([key]) => (