import React, { useState } from 'react'; import i18next from 'i18next'; import PropTypes from 'prop-types'; import { ThemeProvider } from 'styled-components'; import { getTheme } from '../utils/theme'; import IconPlus from '../images/icon-plus-black.svg'; import IconMinus from '../images/icon-minus-black.svg'; import { Input, InputContainer, Text, Icon, ArrowsContainer, Arrow, MultipleInputContainer, } from './styledComponents'; import { Props } from './interfaces'; const renderSingleInput = ( min: number, value: number | string, onChange, isSingleInputHovered: boolean, setIsSingleInputHovered: Function, changeValue, height: string, borderStyle: string, borderRadius: string, width?: string ) => { return ( setIsSingleInputHovered(true)} onMouseLeave={() => setIsSingleInputHovered(false)} > {isSingleInputHovered && ( changeValue('+', value, '')}> changeValue('-', value, '')}> )} ); }; const renderMultipleInput = ( min: number, limitValues: { minValue: number | string; maxValue: number | string; }, onChange, isInputMinHovered: boolean, setIsInputMinHovered: Function, isInputMaxHovered: boolean, setIsInputMaxHovered: Function, changeValue, height: string, borderStyle: string, borderRadius: string, width?: string ) => { return ( setIsInputMinHovered(true)} onMouseLeave={() => setIsInputMinHovered(false)} > onChange(e, e.target.id)} value={limitValues.minValue} /> {isInputMinHovered && ( changeValue('+', limitValues.minValue, 'min')} > changeValue('-', limitValues.minValue, 'min')} > )} {i18next.t('GENERAL_AND')} setIsInputMaxHovered(true)} onMouseLeave={() => setIsInputMaxHovered(false)} > onChange(e, e.target.id)} value={limitValues.maxValue} /> {isInputMaxHovered && ( changeValue('+', limitValues.maxValue, 'max')} > changeValue('-', limitValues.maxValue, 'max')} > )} ); }; const InputNumber = (props: Props): JSX.Element => { const { value, limitValues, onChange, min, display, height, width, borderStyle, borderRadius, theme, } = props; const updatedTheme = getTheme(theme, 'inputnumber'); const [isSingleInputHovered, setIsSingleInputHovered] = useState(false); const [isInputMinHovered, setIsInputMinHovered] = useState(false); const [isInputMaxHovered, setIsInputMaxHovered] = useState(false); const changeValue = (type, initialValue, id) => { const updatedValue = type === '+' ? initialValue + 1 : Math.max(min, initialValue - 1); onChange({ target: { value: updatedValue } }, id); }; return ( {(display === 'multiple' && renderMultipleInput( min, limitValues, onChange, isInputMinHovered, setIsInputMinHovered, isInputMaxHovered, setIsInputMaxHovered, changeValue, height, borderStyle, borderRadius, width )) || renderSingleInput( min, value, onChange, isSingleInputHovered, setIsSingleInputHovered, changeValue, height, borderStyle, borderRadius, width )} ); }; InputNumber.propTypes = { // eslint-disable-next-line react/forbid-prop-types theme: PropTypes.objectOf(PropTypes.any), min: PropTypes.number, value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), limitValues: PropTypes.shape({ minValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), maxValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), }), onChange: PropTypes.func.isRequired, display: PropTypes.string, height: PropTypes.string, width: PropTypes.string, borderStyle: PropTypes.string, borderRadius: PropTypes.string, }; InputNumber.defaultProps = { value: '', min: 0, display: 'single', limitValues: { minValue: '', maxValue: '' }, height: '30px', width: null, borderStyle: '1px solid #dbdbdb', borderRadius: '4px', theme: null, }; export default InputNumber;