adds support for quantities in slider component (passing object instead of number)

This commit is contained in:
Mose Müller 2023-12-13 10:35:28 +01:00
parent e643dd6f5c
commit 729f375901
2 changed files with 47 additions and 22 deletions

View File

@ -97,10 +97,10 @@ export const GenericComponent = React.memo(
parentPath={parentPath} parentPath={parentPath}
docString={attribute.doc} docString={attribute.doc}
readOnly={attribute.readonly} readOnly={attribute.readonly}
value={attribute.value['value']['value']} value={attribute.value['value']}
min={attribute.value['min']['value']} min={attribute.value['min']}
max={attribute.value['max']['value']} max={attribute.value['max']}
stepSize={attribute.value['step_size']['value']} stepSize={attribute.value['step_size']}
isInstantUpdate={isInstantUpdate} isInstantUpdate={isInstantUpdate}
addNotification={addNotification} addNotification={addNotification}
/> />

View File

@ -3,19 +3,19 @@ import { InputGroup, Form, Row, Col, Collapse, ToggleButton } from 'react-bootst
import { setAttribute } from '../socket'; import { setAttribute } from '../socket';
import { DocStringComponent } from './DocStringComponent'; import { DocStringComponent } from './DocStringComponent';
import { Slider } from '@mui/material'; import { Slider } from '@mui/material';
import { NumberComponent } from './NumberComponent'; import { NumberComponent, NumberObject } from './NumberComponent';
import { getIdFromFullAccessPath } from '../utils/stringUtils'; import { getIdFromFullAccessPath } from '../utils/stringUtils';
import { LevelName } from './NotificationsComponent'; import { LevelName } from './NotificationsComponent';
interface SliderComponentProps { interface SliderComponentProps {
name: string; name: string;
min: number; min: NumberObject;
max: number; max: NumberObject;
parentPath?: string; parentPath?: string;
value: number; value: NumberObject;
readOnly: boolean; readOnly: boolean;
docString: string; docString: string;
stepSize: number; stepSize: NumberObject;
isInstantUpdate: boolean; isInstantUpdate: boolean;
addNotification: (message: string, levelname?: LevelName) => void; addNotification: (message: string, levelname?: LevelName) => void;
} }
@ -30,7 +30,6 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
min, min,
max, max,
stepSize, stepSize,
readOnly,
docString, docString,
isInstantUpdate, isInstantUpdate,
addNotification addNotification
@ -71,6 +70,28 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
setAttribute(`${name}.${valueType}`, parentPath, newValue); setAttribute(`${name}.${valueType}`, parentPath, newValue);
}; };
const deconstructNumberDict = (
numberDict: NumberObject
): [number, boolean, string | null] => {
let numberMagnitude: number;
let numberUnit: string | null = null;
const numberReadOnly = numberDict.readonly;
if (numberDict.type === 'int' || numberDict.type === 'float') {
numberMagnitude = numberDict.value;
} else if (numberDict.type === 'Quantity') {
numberMagnitude = numberDict.value.magnitude;
numberUnit = numberDict.value.unit;
}
return [numberMagnitude, numberReadOnly, numberUnit];
};
const [valueMagnitude, valueReadOnly, valueUnit] = deconstructNumberDict(value);
const [minMagnitude, minReadOnly] = deconstructNumberDict(min);
const [maxMagnitude, maxReadOnly] = deconstructNumberDict(max);
const [stepSizeMagnitude, stepSizeReadOnly] = deconstructNumberDict(stepSize);
return ( return (
<div className="sliderComponent" id={id}> <div className="sliderComponent" id={id}>
{process.env.NODE_ENV === 'development' && ( {process.env.NODE_ENV === 'development' && (
@ -87,15 +108,15 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
style={{ margin: '0px 0px 10px 0px' }} style={{ margin: '0px 0px 10px 0px' }}
aria-label="Always visible" aria-label="Always visible"
// valueLabelDisplay="on" // valueLabelDisplay="on"
disabled={readOnly} disabled={valueReadOnly}
value={value} value={valueMagnitude}
onChange={(event, newNumber) => handleOnChange(event, newNumber)} onChange={(event, newNumber) => handleOnChange(event, newNumber)}
min={min} min={minMagnitude}
max={max} max={maxMagnitude}
step={stepSize} step={stepSizeMagnitude}
marks={[ marks={[
{ value: min, label: `${min}` }, { value: minMagnitude, label: `${minMagnitude}` },
{ value: max, label: `${max}` } { value: maxMagnitude, label: `${maxMagnitude}` }
]} ]}
/> />
</Col> </Col>
@ -105,9 +126,10 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
parentPath={parentPath} parentPath={parentPath}
name={`${name}.value`} name={`${name}.value`}
docString="" docString=""
readOnly={readOnly} readOnly={valueReadOnly}
type="float" type="float"
value={value} value={valueMagnitude}
unit={valueUnit}
showName={false} showName={false}
addNotification={() => null} addNotification={() => null}
/> />
@ -144,7 +166,8 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
<Form.Label>Min Value</Form.Label> <Form.Label>Min Value</Form.Label>
<Form.Control <Form.Control
type="number" type="number"
value={min} value={minMagnitude}
disabled={minReadOnly}
onChange={(e) => handleValueChange(Number(e.target.value), 'min')} onChange={(e) => handleValueChange(Number(e.target.value), 'min')}
/> />
</Col> </Col>
@ -153,7 +176,8 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
<Form.Label>Max Value</Form.Label> <Form.Label>Max Value</Form.Label>
<Form.Control <Form.Control
type="number" type="number"
value={max} value={maxMagnitude}
disabled={maxReadOnly}
onChange={(e) => handleValueChange(Number(e.target.value), 'max')} onChange={(e) => handleValueChange(Number(e.target.value), 'max')}
/> />
</Col> </Col>
@ -162,7 +186,8 @@ export const SliderComponent = React.memo((props: SliderComponentProps) => {
<Form.Label>Step Size</Form.Label> <Form.Label>Step Size</Form.Label>
<Form.Control <Form.Control
type="number" type="number"
value={stepSize} value={stepSizeMagnitude}
disabled={stepSizeReadOnly}
onChange={(e) => handleValueChange(Number(e.target.value), 'step_size')} onChange={(e) => handleValueChange(Number(e.target.value), 'step_size')}
/> />
</Col> </Col>