2023-02-19 09:53:57 +01:00
|
|
|
import { VariablesButton } from '@/features/variables'
|
2021-12-23 13:49:24 +01:00
|
|
|
import {
|
|
|
|
NumberInputProps,
|
|
|
|
NumberInput,
|
|
|
|
NumberInputField,
|
|
|
|
NumberInputStepper,
|
|
|
|
NumberIncrementStepper,
|
|
|
|
NumberDecrementStepper,
|
2023-02-19 09:53:57 +01:00
|
|
|
HStack,
|
|
|
|
FormControl,
|
|
|
|
FormLabel,
|
2021-12-23 13:49:24 +01:00
|
|
|
} from '@chakra-ui/react'
|
2023-02-19 09:53:57 +01:00
|
|
|
import { Variable, VariableString } from 'models'
|
2022-03-09 15:12:00 +01:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-03-25 11:28:49 +01:00
|
|
|
import { useDebouncedCallback } from 'use-debounce'
|
2022-06-22 07:36:11 +02:00
|
|
|
import { env } from 'utils'
|
2023-02-19 09:53:57 +01:00
|
|
|
import { MoreInfoTooltip } from '../MoreInfoTooltip'
|
2021-12-23 13:49:24 +01:00
|
|
|
|
2023-02-19 09:53:57 +01:00
|
|
|
type Value<HasVariable> = HasVariable extends undefined | true
|
|
|
|
? number | VariableString
|
|
|
|
: number
|
|
|
|
|
|
|
|
type Props<HasVariable extends boolean> = {
|
|
|
|
defaultValue?: Value<HasVariable>
|
2023-01-26 18:23:09 +01:00
|
|
|
debounceTimeout?: number
|
2023-02-19 09:53:57 +01:00
|
|
|
withVariableButton?: HasVariable
|
|
|
|
label?: string
|
|
|
|
moreInfoTooltip?: string
|
|
|
|
isRequired?: boolean
|
|
|
|
onValueChange: (value?: Value<HasVariable>) => void
|
|
|
|
} & Omit<NumberInputProps, 'defaultValue' | 'value' | 'onChange' | 'isRequired'>
|
2023-01-26 18:23:09 +01:00
|
|
|
|
2023-02-19 09:53:57 +01:00
|
|
|
export const SmartNumberInput = <HasVariable extends boolean>({
|
|
|
|
defaultValue,
|
2021-12-23 13:49:24 +01:00
|
|
|
onValueChange,
|
2023-02-19 09:53:57 +01:00
|
|
|
withVariableButton,
|
2022-03-10 17:32:14 +01:00
|
|
|
debounceTimeout = 1000,
|
2023-02-19 09:53:57 +01:00
|
|
|
label,
|
|
|
|
moreInfoTooltip,
|
|
|
|
isRequired,
|
2021-12-23 13:49:24 +01:00
|
|
|
...props
|
2023-02-19 09:53:57 +01:00
|
|
|
}: Props<HasVariable>) => {
|
|
|
|
const [value, setValue] = useState(defaultValue?.toString() ?? '')
|
|
|
|
|
|
|
|
const onValueChangeDebounced = useDebouncedCallback(
|
2022-03-25 11:28:49 +01:00
|
|
|
onValueChange,
|
2022-08-08 08:21:36 +02:00
|
|
|
env('E2E_TEST') === 'true' ? 0 : debounceTimeout
|
2022-03-09 15:12:00 +01:00
|
|
|
)
|
|
|
|
|
2022-03-25 14:59:40 +01:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
2023-02-19 09:53:57 +01:00
|
|
|
onValueChangeDebounced.flush()
|
2022-03-25 14:59:40 +01:00
|
|
|
},
|
2023-02-19 09:53:57 +01:00
|
|
|
[onValueChangeDebounced]
|
2022-03-25 14:59:40 +01:00
|
|
|
)
|
2021-12-23 13:49:24 +01:00
|
|
|
|
2022-01-25 18:19:37 +01:00
|
|
|
const handleValueChange = (value: string) => {
|
2023-02-19 09:53:57 +01:00
|
|
|
setValue(value)
|
2021-12-23 13:49:24 +01:00
|
|
|
if (value.endsWith('.') || value.endsWith(',')) return
|
2023-02-19 09:53:57 +01:00
|
|
|
if (value === '') return onValueChangeDebounced(undefined)
|
|
|
|
if (
|
|
|
|
value.startsWith('{{') &&
|
|
|
|
value.endsWith('}}') &&
|
|
|
|
value.length > 4 &&
|
|
|
|
(withVariableButton ?? true)
|
|
|
|
) {
|
|
|
|
onValueChangeDebounced(value as Value<HasVariable>)
|
|
|
|
return
|
|
|
|
}
|
2021-12-23 13:49:24 +01:00
|
|
|
const newValue = parseFloat(value)
|
|
|
|
if (isNaN(newValue)) return
|
2023-02-19 09:53:57 +01:00
|
|
|
onValueChangeDebounced(newValue)
|
2022-01-25 18:19:37 +01:00
|
|
|
}
|
2021-12-23 13:49:24 +01:00
|
|
|
|
2023-02-19 09:53:57 +01:00
|
|
|
const handleVariableSelected = (variable?: Variable) => {
|
|
|
|
if (!variable) return
|
|
|
|
const newValue = `{{${variable.name}}}`
|
|
|
|
handleValueChange(newValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
const Input = (
|
|
|
|
<NumberInput onChange={handleValueChange} value={value} {...props}>
|
2022-05-24 14:25:15 -07:00
|
|
|
<NumberInputField placeholder={props.placeholder} />
|
2021-12-23 13:49:24 +01:00
|
|
|
<NumberInputStepper>
|
|
|
|
<NumberIncrementStepper />
|
|
|
|
<NumberDecrementStepper />
|
|
|
|
</NumberInputStepper>
|
|
|
|
</NumberInput>
|
|
|
|
)
|
2023-02-19 09:53:57 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl
|
|
|
|
as={HStack}
|
|
|
|
isRequired={isRequired}
|
|
|
|
justifyContent="space-between"
|
|
|
|
>
|
|
|
|
{label && (
|
|
|
|
<FormLabel mb="0" flexShrink={0}>
|
|
|
|
{label}{' '}
|
|
|
|
{moreInfoTooltip && (
|
|
|
|
<MoreInfoTooltip>{moreInfoTooltip}</MoreInfoTooltip>
|
|
|
|
)}
|
|
|
|
</FormLabel>
|
|
|
|
)}
|
|
|
|
{withVariableButton ?? true ? (
|
|
|
|
<HStack spacing={0}>
|
|
|
|
{Input}
|
|
|
|
<VariablesButton onSelectVariable={handleVariableSelected} />
|
|
|
|
</HStack>
|
|
|
|
) : (
|
|
|
|
Input
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
)
|
2021-12-23 13:49:24 +01:00
|
|
|
}
|