mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-06 13:30:41 +02:00
frontend: updates EnumComponent
- replaces type with SerializedEnum from types.ts - passing props instead of attribute directly
This commit is contained in:
parent
b1e6663c66
commit
c98f191d20
@ -2,52 +2,32 @@ import React, { useEffect } from "react";
|
|||||||
import { InputGroup, Form, Row, Col } from "react-bootstrap";
|
import { InputGroup, Form, Row, Col } from "react-bootstrap";
|
||||||
import { DocStringComponent } from "./DocStringComponent";
|
import { DocStringComponent } from "./DocStringComponent";
|
||||||
import { LevelName } from "./NotificationsComponent";
|
import { LevelName } from "./NotificationsComponent";
|
||||||
import { SerializedObject } from "../types/SerializedObject";
|
import { SerializedObject, SerializedEnum } from "../types/SerializedObject";
|
||||||
import { useRenderCount } from "../hooks/useRenderCount";
|
import { useRenderCount } from "../hooks/useRenderCount";
|
||||||
|
|
||||||
export interface EnumSerialization {
|
interface EnumComponentProps extends SerializedEnum {
|
||||||
type: "Enum" | "ColouredEnum";
|
|
||||||
full_access_path: string;
|
|
||||||
name: string;
|
|
||||||
value: string;
|
|
||||||
readonly: boolean;
|
|
||||||
doc: string | null;
|
|
||||||
enum: Record<string, string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface EnumComponentProps {
|
|
||||||
attribute: EnumSerialization;
|
|
||||||
addNotification: (message: string, levelname?: LevelName) => void;
|
addNotification: (message: string, levelname?: LevelName) => void;
|
||||||
displayName: string;
|
displayName: string;
|
||||||
id: 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) => {
|
export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
||||||
const { attribute, addNotification, displayName, id } = props;
|
|
||||||
const {
|
const {
|
||||||
full_access_path: fullAccessPath,
|
addNotification,
|
||||||
|
displayName,
|
||||||
|
id,
|
||||||
value,
|
value,
|
||||||
doc: docString,
|
full_access_path: fullAccessPath,
|
||||||
enum: enumDict,
|
enum: enumDict,
|
||||||
|
doc: docString,
|
||||||
readonly: readOnly,
|
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();
|
const renderCount = useRenderCount();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setEnumValue(() => {
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
addNotification(`${fullAccessPath} changed to ${value}.`);
|
addNotification(`${fullAccessPath} changed to ${value}.`);
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|
||||||
@ -65,11 +45,9 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
|||||||
// Display the Form.Control when readOnly is true
|
// Display the Form.Control when readOnly is true
|
||||||
<Form.Control
|
<Form.Control
|
||||||
style={
|
style={
|
||||||
attribute.type == "ColouredEnum"
|
props.type == "ColouredEnum" ? { backgroundColor: enumDict[value] } : {}
|
||||||
? { backgroundColor: enumDict[enumValue] }
|
|
||||||
: {}
|
|
||||||
}
|
}
|
||||||
value={attribute.type == "ColouredEnum" ? enumValue : enumDict[enumValue]}
|
value={props.type == "ColouredEnum" ? value : enumDict[value]}
|
||||||
name={fullAccessPath}
|
name={fullAccessPath}
|
||||||
disabled={true}
|
disabled={true}
|
||||||
/>
|
/>
|
||||||
@ -77,27 +55,25 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
|||||||
// Display the Form.Select when readOnly is false
|
// Display the Form.Select when readOnly is false
|
||||||
<Form.Select
|
<Form.Select
|
||||||
aria-label="example-select"
|
aria-label="example-select"
|
||||||
value={enumValue}
|
value={value}
|
||||||
name={fullAccessPath}
|
name={fullAccessPath}
|
||||||
style={
|
style={
|
||||||
attribute.type == "ColouredEnum"
|
props.type == "ColouredEnum" ? { backgroundColor: enumDict[value] } : {}
|
||||||
? { backgroundColor: enumDict[enumValue] }
|
|
||||||
: {}
|
|
||||||
}
|
}
|
||||||
onChange={(event) =>
|
onChange={(event) =>
|
||||||
changeCallback({
|
changeCallback({
|
||||||
type: attribute.type,
|
type: props.type,
|
||||||
name: attribute.name,
|
name: props.name,
|
||||||
enum: enumDict,
|
enum: enumDict,
|
||||||
value: event.target.value,
|
value: event.target.value,
|
||||||
full_access_path: fullAccessPath,
|
full_access_path: fullAccessPath,
|
||||||
readonly: attribute.readonly,
|
readonly: props.readonly,
|
||||||
doc: attribute.doc,
|
doc: props.doc,
|
||||||
})
|
})
|
||||||
}>
|
}>
|
||||||
{Object.entries(enumDict).map(([key, val]) => (
|
{Object.entries(enumDict).map(([key, val]) => (
|
||||||
<option key={key} value={key}>
|
<option key={key} value={key}>
|
||||||
{attribute.type == "ColouredEnum" ? key : val}
|
{props.type == "ColouredEnum" ? key : val}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</Form.Select>
|
</Form.Select>
|
||||||
|
@ -2,7 +2,7 @@ import React, { useContext } from "react";
|
|||||||
import { ButtonComponent } from "./ButtonComponent";
|
import { ButtonComponent } from "./ButtonComponent";
|
||||||
import { NumberComponent, NumberObject } from "./NumberComponent";
|
import { NumberComponent, NumberObject } from "./NumberComponent";
|
||||||
import { SliderComponent } from "./SliderComponent";
|
import { SliderComponent } from "./SliderComponent";
|
||||||
import { EnumComponent, EnumSerialization } from "./EnumComponent";
|
import { EnumComponent } from "./EnumComponent";
|
||||||
import { MethodComponent } from "./MethodComponent";
|
import { MethodComponent } from "./MethodComponent";
|
||||||
import { AsyncMethodComponent } from "./AsyncMethodComponent";
|
import { AsyncMethodComponent } from "./AsyncMethodComponent";
|
||||||
import { StringComponent } from "./StringComponent";
|
import { StringComponent } from "./StringComponent";
|
||||||
@ -16,7 +16,7 @@ import { WebSettingsContext } from "../WebSettings";
|
|||||||
import { updateValue } from "../socket";
|
import { updateValue } from "../socket";
|
||||||
import { DictComponent } from "./DictComponent";
|
import { DictComponent } from "./DictComponent";
|
||||||
import { parseFullAccessPath } from "../utils/stateUtils";
|
import { parseFullAccessPath } from "../utils/stateUtils";
|
||||||
import { SerializedObject } from "../types/SerializedObject";
|
import { SerializedEnum, SerializedObject } from "../types/SerializedObject";
|
||||||
|
|
||||||
interface GenericComponentProps {
|
interface GenericComponentProps {
|
||||||
attribute: SerializedObject;
|
attribute: SerializedObject;
|
||||||
@ -136,7 +136,7 @@ export const GenericComponent = React.memo(
|
|||||||
} else if (attribute.type === "Enum" || attribute.type === "ColouredEnum") {
|
} else if (attribute.type === "Enum" || attribute.type === "ColouredEnum") {
|
||||||
return (
|
return (
|
||||||
<EnumComponent
|
<EnumComponent
|
||||||
attribute={attribute as EnumSerialization}
|
{...(attribute as SerializedEnum)}
|
||||||
addNotification={addNotification}
|
addNotification={addNotification}
|
||||||
changeCallback={changeCallback}
|
changeCallback={changeCallback}
|
||||||
displayName={displayName}
|
displayName={displayName}
|
||||||
|
@ -36,11 +36,11 @@ type SerializedString = SerializedObjectBase & {
|
|||||||
type: "str";
|
type: "str";
|
||||||
};
|
};
|
||||||
|
|
||||||
type SerializedEnum = SerializedObjectBase & {
|
export type SerializedEnum = SerializedObjectBase & {
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
type: "Enum" | "ColouredEnum";
|
type: "Enum" | "ColouredEnum";
|
||||||
enum: Record<string, unknown>;
|
enum: Record<string, string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SerializedList = SerializedObjectBase & {
|
type SerializedList = SerializedObjectBase & {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user