mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 08:20:02 +02:00
restructuring EnumComponent (now for both Enum and ColouredEnum)
This commit is contained in:
parent
2ce4c9ce9b
commit
6397307690
@ -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<string, string>;
|
|
||||||
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 (
|
|
||||||
<div className={'component enumComponent'} id={id}>
|
|
||||||
{process.env.NODE_ENV === 'development' && (
|
|
||||||
<div>Render count: {renderCount.current}</div>
|
|
||||||
)}
|
|
||||||
<Row>
|
|
||||||
<Col className="d-flex align-items-center">
|
|
||||||
<InputGroup.Text>
|
|
||||||
{displayName}
|
|
||||||
<DocStringComponent docString={docString} />
|
|
||||||
</InputGroup.Text>
|
|
||||||
{readOnly ? (
|
|
||||||
// Display the Form.Control when readOnly is true
|
|
||||||
<Form.Control
|
|
||||||
value={enumValue}
|
|
||||||
name={name}
|
|
||||||
disabled={true}
|
|
||||||
style={{ backgroundColor: enumDict[enumValue] }}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
// Display the Form.Select when readOnly is false
|
|
||||||
<Form.Select
|
|
||||||
aria-label="coloured-enum-select"
|
|
||||||
value={enumValue}
|
|
||||||
name={name}
|
|
||||||
style={{ backgroundColor: enumDict[enumValue] }}
|
|
||||||
onChange={(event) => changeCallback(event.target.value)}>
|
|
||||||
{Object.entries(enumDict).map(([key]) => (
|
|
||||||
<option key={key} value={key}>
|
|
||||||
{key}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</Form.Select>
|
|
||||||
)}
|
|
||||||
</Col>
|
|
||||||
</Row>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
});
|
|
@ -1,63 +1,58 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef, useState } 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 { SerializedValue } from './GenericComponent';
|
||||||
import { LevelName } from './NotificationsComponent';
|
import { LevelName } from './NotificationsComponent';
|
||||||
|
|
||||||
type EnumComponentProps = {
|
export type EnumSerialization = {
|
||||||
|
type: 'Enum' | 'ColouredEnum';
|
||||||
|
full_access_path: string;
|
||||||
name: string;
|
name: string;
|
||||||
parentPath: string;
|
|
||||||
value: string;
|
value: string;
|
||||||
docString?: string;
|
readonly: boolean;
|
||||||
readOnly: boolean;
|
doc?: string | null;
|
||||||
enumDict: Record<string, string>;
|
enum: Record<string, string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type EnumComponentProps = {
|
||||||
|
attribute: EnumSerialization;
|
||||||
addNotification: (message: string, levelname?: LevelName) => void;
|
addNotification: (message: string, levelname?: LevelName) => void;
|
||||||
changeCallback?: (
|
|
||||||
value: unknown,
|
|
||||||
attributeName?: string,
|
|
||||||
prefix?: string,
|
|
||||||
callback?: (ack: unknown) => void
|
|
||||||
) => void;
|
|
||||||
displayName: string;
|
displayName: string;
|
||||||
id: string;
|
id: string;
|
||||||
|
changeCallback?: (value: SerializedValue, 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 {
|
||||||
name,
|
full_access_path: fullAccessPath,
|
||||||
value,
|
value,
|
||||||
docString,
|
doc: docString,
|
||||||
enumDict,
|
enum: enumDict,
|
||||||
addNotification,
|
readonly: readOnly
|
||||||
displayName,
|
} = attribute;
|
||||||
id,
|
|
||||||
readOnly
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
let { changeCallback } = props;
|
let { changeCallback } = props;
|
||||||
if (changeCallback === undefined) {
|
if (changeCallback === undefined) {
|
||||||
changeCallback = (value: string) => {
|
changeCallback = (value: SerializedValue) => {
|
||||||
setEnumValue(() => {
|
setEnumValue(() => {
|
||||||
return value;
|
return String(value.value);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const renderCount = useRef(0);
|
const renderCount = useRef(0);
|
||||||
const [enumValue, setEnumValue] = useState(value);
|
const [enumValue, setEnumValue] = useState(value);
|
||||||
|
|
||||||
const fullAccessPath = [props.parentPath, props.name]
|
|
||||||
.filter((element) => element)
|
|
||||||
.join('.');
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
renderCount.current++;
|
renderCount.current++;
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setEnumValue(() => {
|
setEnumValue(() => {
|
||||||
return props.value;
|
return value;
|
||||||
});
|
});
|
||||||
addNotification(`${fullAccessPath} changed to ${value}.`);
|
addNotification(`${fullAccessPath} changed to ${value}.`);
|
||||||
}, [props.value]);
|
}, [value]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'component enumComponent'} id={id}>
|
<div className={'component enumComponent'} id={id}>
|
||||||
@ -73,17 +68,35 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
|
|||||||
|
|
||||||
{readOnly ? (
|
{readOnly ? (
|
||||||
// Display the Form.Control when readOnly is true
|
// Display the Form.Control when readOnly is true
|
||||||
<Form.Control value={enumDict[enumValue]} name={name} disabled={true} />
|
<Form.Control
|
||||||
|
value={enumDict[enumValue]}
|
||||||
|
name={fullAccessPath}
|
||||||
|
disabled={true}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
// 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={enumValue}
|
||||||
name={name}
|
name={fullAccessPath}
|
||||||
onChange={(event) => changeCallback(event.target.value)}>
|
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]) => (
|
{Object.entries(enumDict).map(([key, val]) => (
|
||||||
<option key={key} value={key}>
|
<option key={key} value={key}>
|
||||||
{val}
|
{attribute.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 } from './NumberComponent';
|
import { NumberComponent } from './NumberComponent';
|
||||||
import { SliderComponent } from './SliderComponent';
|
import { SliderComponent } from './SliderComponent';
|
||||||
import { EnumComponent } from './EnumComponent';
|
import { EnumComponent, EnumSerialization } 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';
|
||||||
@ -10,7 +10,6 @@ import { ListComponent } from './ListComponent';
|
|||||||
import { DataServiceComponent, DataServiceJSON } from './DataServiceComponent';
|
import { DataServiceComponent, DataServiceJSON } from './DataServiceComponent';
|
||||||
import { DeviceConnectionComponent } from './DeviceConnection';
|
import { DeviceConnectionComponent } from './DeviceConnection';
|
||||||
import { ImageComponent } from './ImageComponent';
|
import { ImageComponent } from './ImageComponent';
|
||||||
import { ColouredEnumComponent } from './ColouredEnumComponent';
|
|
||||||
import { LevelName } from './NotificationsComponent';
|
import { LevelName } from './NotificationsComponent';
|
||||||
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
import { getIdFromFullAccessPath } from '../utils/stringUtils';
|
||||||
import { WebSettingsContext } from '../WebSettings';
|
import { WebSettingsContext } from '../WebSettings';
|
||||||
@ -133,14 +132,10 @@ export const GenericComponent = React.memo(
|
|||||||
id={id}
|
id={id}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
} else if (attribute.type === 'Enum') {
|
} else if (attribute.type === 'Enum' || attribute.type === 'ColouredEnum') {
|
||||||
return (
|
return (
|
||||||
<EnumComponent
|
<EnumComponent
|
||||||
fullAccessPath={fullAccessPath}
|
attribute={attribute as EnumSerialization}
|
||||||
docString={attribute.doc}
|
|
||||||
value={String(attribute.value)}
|
|
||||||
readOnly={attribute.readonly}
|
|
||||||
enumDict={attribute.enum}
|
|
||||||
addNotification={addNotification}
|
addNotification={addNotification}
|
||||||
changeCallback={changeCallback}
|
changeCallback={changeCallback}
|
||||||
displayName={displayName}
|
displayName={displayName}
|
||||||
@ -230,16 +225,6 @@ export const GenericComponent = React.memo(
|
|||||||
format={attribute.value['format']['value'] as string}
|
format={attribute.value['format']['value'] as string}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
} else if (attribute.type === 'ColouredEnum') {
|
|
||||||
return (
|
|
||||||
<ColouredEnumComponent
|
|
||||||
attribute={attribute}
|
|
||||||
addNotification={addNotification}
|
|
||||||
displayName={displayName}
|
|
||||||
id={id}
|
|
||||||
changeCallback={changeCallback}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return <div key={fullAccessPath}>{fullAccessPath}</div>;
|
return <div key={fullAccessPath}>{fullAccessPath}</div>;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user