frontend: implementing distinction between int and float NumberComponent

This commit is contained in:
Mose Müller 2023-08-02 12:06:20 +02:00
parent 50162cdc82
commit 2a6574fea1
2 changed files with 18 additions and 36 deletions

View File

@ -133,23 +133,12 @@ const App = () => {
/> />
</div> </div>
); );
} else if (value.type === 'int') { } else if (value.type === 'float' || value.type === 'int') {
return (
<div key={key}>
<NumberComponent
name={key}
parent_path="DataService"
docString={value.doc}
readOnly={value.readonly}
value={Number(value.value)}
/>
</div>
);
} else if (value.type === 'float') {
return ( return (
<div key={key}> <div key={key}>
<NumberComponent <NumberComponent
name={key} name={key}
type={value.type}
parent_path="DataService" parent_path="DataService"
docString={value.doc} docString={value.doc}
readOnly={value.readonly} readOnly={value.readonly}

View File

@ -1,38 +1,19 @@
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { import { Form, InputGroup, Button } from 'react-bootstrap';
OverlayTrigger,
Badge,
Tooltip,
Form,
InputGroup,
Button
} from 'react-bootstrap';
import { socket } from '../socket'; import { socket } from '../socket';
import { DocStringComponent } from './DocStringComponent'; import { DocStringComponent } from './DocStringComponent';
// TODO: add button functionality // TODO: add button functionality
interface ButtonComponentProps { interface NumberComponentProps {
name: string; name: string;
type: 'float' | 'int';
parent_path?: string; parent_path?: string;
value: number; value: number;
readOnly: boolean; readOnly: boolean;
docString: string; docString: string;
} }
const handleNumericKey = (key: string, value: string, selectionStart: number) => {
// Check if a number key or a decimal point key is pressed
if (key === '.' && value.includes('.')) {
// Check if value already contains a decimal. If so, ignore input.
// eslint-disable-next-line no-console
console.warn('Number already contains decimal! Ignoring...');
return { value, selectionStart };
}
// Add the new key at the cursor's position
const newValue = value.slice(0, selectionStart) + key + value.slice(selectionStart);
return { value: newValue, selectionStart: selectionStart + 1 };
};
// TODO: highlight the digit that is being changed by setting both selectionStart and // TODO: highlight the digit that is being changed by setting both selectionStart and
// selectionEnd // selectionEnd
const handleArrowKey = ( const handleArrowKey = (
@ -116,7 +97,7 @@ const handleDeleteKey = (
return { value, selectionStart }; return { value, selectionStart };
}; };
export const NumberComponent = React.memo((props: ButtonComponentProps) => { export const NumberComponent = React.memo((props: NumberComponentProps) => {
const renderCount = useRef(0); const renderCount = useRef(0);
// Create a state for the cursor position // Create a state for the cursor position
const [cursorPosition, setCursorPosition] = useState(null); const [cursorPosition, setCursorPosition] = useState(null);
@ -139,6 +120,18 @@ export const NumberComponent = React.memo((props: ButtonComponentProps) => {
const { name, parent_path, value, readOnly, docString } = props; const { name, parent_path, value, readOnly, docString } = props;
const handleNumericKey = (key: string, value: string, selectionStart: number) => {
// Check if a number key or a decimal point key is pressed
if (key === '.' && (value.includes('.') || props.type === 'int')) {
// Check if value already contains a decimal. If so, ignore input.
// eslint-disable-next-line no-console
console.warn('Invalid input! Ignoring...');
return { value, selectionStart };
}
// Add the new key at the cursor's position
const newValue = value.slice(0, selectionStart) + key + value.slice(selectionStart);
return { value: newValue, selectionStart: selectionStart + 1 };
};
const handleKeyDown = (event) => { const handleKeyDown = (event) => {
const { key, target } = event; const { key, target } = event;
if (key === 'F5' || key === 'ArrowRight' || key === 'ArrowLeft') { if (key === 'F5' || key === 'ArrowRight' || key === 'ArrowLeft') {