Skip to content

Commit

Permalink
[frontend] remove input control
Browse files Browse the repository at this point in the history
  • Loading branch information
delemaf committed Nov 26, 2024
1 parent d5aaf77 commit b8cab07
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions opencti-platform/opencti-front/src/components/InputScaleField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, useRef, useState } from 'react';
import React, { FunctionComponent, useState } from 'react';
import { Field, FieldProps } from 'formik';
import { Grid, MenuItem, Select, SelectChangeEvent, Slider } from '@mui/material';
import SimpleTextField from './SimpleTextField';
Expand Down Expand Up @@ -40,8 +40,6 @@ const InputScaleField: FunctionComponent<InputScaleFieldProps & FieldProps> = ({
marks: defaultMarks,
scale,
} = useLevel(entityType, attributeName, value);
const debounceTimeout = useRef<NodeJS.Timeout | null>(null);
const debounceInterval = 200;
const min = scale?.min ? scale.min.value : 0;
const defaultMaxValue = scale?.max ? scale.max.value : 0;
const max = maxLimit !== undefined && Number.isFinite(maxLimit) && maxLimit <= defaultMaxValue
Expand All @@ -54,36 +52,11 @@ const InputScaleField: FunctionComponent<InputScaleFieldProps & FieldProps> = ({
background: `${color}`,
},
};
const handleSliderChange = (event: SelectChangeEvent) => {
const handleValueChange = (event: SelectChangeEvent) => {
setFieldValue(name, event.target.value);
onSubmit?.(name, event.target.value);
};

const handleInputChange = (_: string, v: string) => {
const inputValue = parseInt(v, 10);

if (!Number.isNaN(inputValue)) {
const clampedValue = Math.min(Math.max(inputValue, min), max);

if (debounceTimeout.current) {
clearTimeout(debounceTimeout.current);
}

debounceTimeout.current = setTimeout(() => {
setFieldValue(name, clampedValue.toString());
onSubmit?.(name, clampedValue.toString());
}, debounceInterval);
} else {
if (debounceTimeout.current) {
clearTimeout(debounceTimeout.current);
}
debounceTimeout.current = setTimeout(() => {
setFieldValue(name, '0');
onSubmit?.(name, '0');
}, debounceInterval);
}
};

const currentLevel = buildScaleLevel(value, scale);

const [initialValue] = useState(value);
Expand All @@ -102,7 +75,7 @@ const InputScaleField: FunctionComponent<InputScaleFieldProps & FieldProps> = ({
onSubmit={onSubmit}
onFocus={onFocus}
disabled={finalDisabled}
onChange={handleInputChange}
onChange={handleValueChange}
helpertext={
editContext ? <SubscriptionFocus context={editContext} fieldName={name} /> : undefined
}
Expand All @@ -113,7 +86,7 @@ const InputScaleField: FunctionComponent<InputScaleFieldProps & FieldProps> = ({
fullWidth
labelId={name}
value={currentLevel.level.value?.toString() ?? ''}
onChange={handleSliderChange}
onChange={handleValueChange}
disabled={finalDisabled}
sx={{ marginTop: 2 }}
>
Expand Down

0 comments on commit b8cab07

Please sign in to comment.