From a9831b6b7fc24479f25590b4d3c16fc2c07db2f2 Mon Sep 17 00:00:00 2001 From: Eric NGUYEN Date: Tue, 13 Sep 2022 17:47:31 +0200 Subject: [PATCH] Implement pseudo dynamic input --- .../ContainerProperties/ContainerForm.tsx | 51 ++++++++-------- src/Components/InputGroup/TextInputGroup.tsx | 59 +++++++++++++++++++ 2 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 src/Components/InputGroup/TextInputGroup.tsx diff --git a/src/Components/ContainerProperties/ContainerForm.tsx b/src/Components/ContainerProperties/ContainerForm.tsx index 88ebaf7..b28a1ba 100644 --- a/src/Components/ContainerProperties/ContainerForm.tsx +++ b/src/Components/ContainerProperties/ContainerForm.tsx @@ -7,6 +7,7 @@ import { ISymbolModel } from '../../Interfaces/ISymbolModel'; import { SHOW_BORROWER_DIMENSIONS, SHOW_CHILDREN_DIMENSIONS, SHOW_SELF_DIMENSIONS } from '../../utils/default'; import { ApplyWidthMargin, ApplyXMargin, RemoveWidthMargin, RemoveXMargin, RestoreX, TransformX } from '../../utils/svg'; import { InputGroup } from '../InputGroup/InputGroup'; +import { TextInputGroup } from '../InputGroup/TextInputGroup'; import { RadioGroupButtons } from '../RadioGroupButtons/RadioGroupButtons'; import { Select } from '../Select/Select'; @@ -20,7 +21,7 @@ function GetCSSInputs(properties: IContainerProperties, onChange: (key: string, value: string | number | boolean, type: PropertyType) => void): JSX.Element[] { const groupInput: JSX.Element[] = []; for (const key in properties.style) { - groupInput.push( onChange(key, event.target.value, PropertyType.Style)} />); + onChange={(value) => onChange(key, value, PropertyType.Style)} />); } return groupInput; } @@ -60,15 +61,15 @@ export function ContainerForm(props: IContainerFormProps): JSX.Element { type='string' value={props.properties.type} isDisabled={true} /> - props.onChange('displayedText', event.target.value)} /> - props.onChange('displayedText', value)} /> + props.onChange( + onChange={(value) => props.onChange( 'x', ApplyXMargin( RestoreX( - Number(event.target.value), + Number(value), props.properties.width, props.properties.xPositionReference ), props.properties.margin.left ) )} /> - props.onChange('y', Number(event.target.value) + (props.properties.margin?.top ?? 0))} /> - props.onChange('y', Number(value) + (props.properties.margin?.top ?? 0))} /> + props.onChange('minWidth', Number(event.target.value))} /> - props.onChange('minWidth', Number(value))} /> + props.onChange('maxWidth', Number(event.target.value))} /> - props.onChange('maxWidth', Number(value))} /> + props.onChange('width', ApplyWidthMargin(Number(event.target.value), props.properties.margin.left, props.properties.margin.right))} + onChange={(value) => props.onChange('width', ApplyWidthMargin(Number(value), props.properties.margin.left, props.properties.margin.right))} isDisabled={props.properties.isFlex} /> - props.onChange('height', Number(event.target.value) - (props.properties.margin?.top ?? 0) - (props.properties.margin?.bottom ?? 0))} /> - props.onChange('height', Number(value) - (props.properties.margin?.top ?? 0) - (props.properties.margin?.bottom ?? 0))} /> + props.onChange('left', Number(event.target.value), PropertyType.Margin)} /> - props.onChange('left', Number(value), PropertyType.Margin)} /> + props.onChange('bottom', Number(event.target.value), PropertyType.Margin)} /> - props.onChange('bottom', Number(value), PropertyType.Margin)} /> + props.onChange('top', Number(event.target.value), PropertyType.Margin)} /> - props.onChange('top', Number(value), PropertyType.Margin)} /> + props.onChange('right', Number(event.target.value), PropertyType.Margin)} /> + onChange={(value) => props.onChange('right', Number(value), PropertyType.Margin)} /> void +} + +const className = 'input-group'; + +export function TextInputGroup(props: ITextInputGroupProps): JSX.Element { + const [value, setValue] = React.useState(props.value); + + React.useEffect(() => { + setValue(props.value); + }, [props.value]); + + function OnKeyUp(event: React.KeyboardEvent): void { + switch (event.key) { + case 'Enter': + props.onChange(value); + break; + case 'Escape': + setValue(props.value); + break; + } + } + + return <> + + setValue(event.target.value)} + onKeyUp={OnKeyUp} + min={props.min} + max={props.max} + disabled={props.isDisabled} /> + ; +}