stores enum value within component - now usable within method form

This commit is contained in:
Mose Müller 2024-02-21 16:10:22 +01:00
parent f8926ea823
commit 22fd2d099d
2 changed files with 55 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import React, { useEffect, useRef } 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 { LevelName } from './NotificationsComponent'; import { LevelName } from './NotificationsComponent';
@ -23,16 +23,27 @@ type ColouredEnumComponentProps = {
export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentProps) => { export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentProps) => {
const { const {
name,
value, value,
docString, docString,
enumDict, enumDict,
readOnly, readOnly,
addNotification, addNotification,
changeCallback = () => {},
displayName, displayName,
id id
} = props; } = props;
let { changeCallback } = props;
if (changeCallback === undefined) {
changeCallback = (value: string) => {
setEnumValue(() => {
return value;
});
};
}
const renderCount = useRef(0); const renderCount = useRef(0);
const [enumValue, setEnumValue] = useState(value);
const fullAccessPath = [props.parentPath, props.name] const fullAccessPath = [props.parentPath, props.name]
.filter((element) => element) .filter((element) => element)
.join('.'); .join('.');
@ -42,6 +53,9 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro
}); });
useEffect(() => { useEffect(() => {
setEnumValue(() => {
return props.value;
});
addNotification(`${fullAccessPath} changed to ${value}.`); addNotification(`${fullAccessPath} changed to ${value}.`);
}, [props.value]); }, [props.value]);
@ -59,16 +73,18 @@ export const ColouredEnumComponent = React.memo((props: ColouredEnumComponentPro
{readOnly ? ( {readOnly ? (
// Display the Form.Control when readOnly is true // Display the Form.Control when readOnly is true
<Form.Control <Form.Control
value={value} value={enumValue}
name={name}
disabled={true} disabled={true}
style={{ backgroundColor: enumDict[value] }} style={{ backgroundColor: enumDict[enumValue] }}
/> />
) : ( ) : (
// Display the Form.Select when readOnly is false // Display the Form.Select when readOnly is false
<Form.Select <Form.Select
aria-label="coloured-enum-select" aria-label="coloured-enum-select"
value={value} value={enumValue}
style={{ backgroundColor: enumDict[value] }} name={name}
style={{ backgroundColor: enumDict[enumValue] }}
onChange={(event) => changeCallback(event.target.value)}> onChange={(event) => changeCallback(event.target.value)}>
{Object.entries(enumDict).map(([key]) => ( {Object.entries(enumDict).map(([key]) => (
<option key={key} value={key}> <option key={key} value={key}>

View File

@ -1,4 +1,4 @@
import React, { useEffect, useRef } 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 { LevelName } from './NotificationsComponent'; import { LevelName } from './NotificationsComponent';
@ -8,6 +8,7 @@ type EnumComponentProps = {
parentPath: string; parentPath: string;
value: string; value: string;
docString?: string; docString?: string;
readOnly: boolean;
enumDict: Record<string, string>; enumDict: Record<string, string>;
addNotification: (message: string, levelname?: LevelName) => void; addNotification: (message: string, levelname?: LevelName) => void;
changeCallback?: ( changeCallback?: (
@ -22,16 +23,27 @@ type EnumComponentProps = {
export const EnumComponent = React.memo((props: EnumComponentProps) => { export const EnumComponent = React.memo((props: EnumComponentProps) => {
const { const {
name,
value, value,
docString, docString,
enumDict, enumDict,
addNotification, addNotification,
changeCallback = () => {},
displayName, displayName,
id id,
readOnly
} = props; } = props;
let { changeCallback } = props;
if (changeCallback === undefined) {
changeCallback = (value: string) => {
setEnumValue(() => {
return value;
});
};
}
const renderCount = useRef(0); const renderCount = useRef(0);
const [enumValue, setEnumValue] = useState(value);
const fullAccessPath = [props.parentPath, props.name] const fullAccessPath = [props.parentPath, props.name]
.filter((element) => element) .filter((element) => element)
.join('.'); .join('.');
@ -55,16 +67,24 @@ export const EnumComponent = React.memo((props: EnumComponentProps) => {
{displayName} {displayName}
<DocStringComponent docString={docString} /> <DocStringComponent docString={docString} />
</InputGroup.Text> </InputGroup.Text>
<Form.Select
aria-label="Default select example" {readOnly ? (
value={value} // Display the Form.Control when readOnly is true
onChange={(event) => changeCallback(event.target.value)}> <Form.Control value={enumValue} name={name} disabled={true} />
{Object.entries(enumDict).map(([key, val]) => ( ) : (
<option key={key} value={key}> // Display the Form.Select when readOnly is false
{key} - {val} <Form.Select
</option> aria-label="example-select"
))} value={enumValue}
</Form.Select> name={name}
onChange={(event) => changeCallback(event.target.value)}>
{Object.entries(enumDict).map(([key, val]) => (
<option key={key} value={key}>
{key} - {val}
</option>
))}
</Form.Select>
)}
</Col> </Col>
</Row> </Row>
</div> </div>