ToolJet/frontend/src/Editor/Components/NumberInput.jsx
Mike 89f56410bd
On Change added to NumberInput (#4198)
* On Change added to NumberInput

* fixes lint errors

Co-authored-by: arpitnath <arpitnath42@gmail.com>
2022-10-18 12:58:30 +05:30

55 lines
1.6 KiB
JavaScript

import React, { useEffect } from 'react';
export const NumberInput = function NumberInput({
height,
properties,
styles,
setExposedVariable,
component,
fireEvent,
}) {
const { visibility, borderRadius } = styles;
const [value, setValue] = React.useState(parseInt(properties.value));
const handleChange = (e) => {
if (
!isNaN(parseInt(properties.minValue)) &&
!isNaN(parseInt(properties.maxValue)) &&
parseInt(properties.minValue) > parseInt(properties.maxValue)
) {
setValue(parseInt(properties.maxValue));
} else if (!isNaN(parseInt(properties.maxValue)) && parseInt(e.target.value) > parseInt(properties.maxValue)) {
setValue(parseInt(properties.maxValue));
} else if (!isNaN(parseInt(properties.minValue)) && parseInt(e.target.value) < parseInt(properties.minValue)) {
setValue(parseInt(properties.minValue));
} else {
setValue(parseInt(e.target.value));
}
fireEvent('onChange');
};
useEffect(() => {
setValue(parseInt(properties.value));
}, [properties.value]);
useEffect(() => {
if (!isNaN(value)) {
setExposedVariable('value', value);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
return (
<input
disabled={styles.disabledState}
onChange={handleChange}
type="number"
className="form-control"
placeholder={properties.placeholder}
style={{ height, display: visibility ? '' : 'none', borderRadius: `${borderRadius}px` }}
value={value}
data-cy={`draggable-widget-${String(component.name).toLowerCase()}`}
/>
);
};