2023-03-15 11:51:30 +01:00
|
|
|
import { VariablesButton } from '@/features/variables/components/VariablesButton'
|
2021-12-23 13:49:24 +01:00
|
|
|
import {
|
|
|
|
NumberInputProps,
|
2023-03-03 09:01:11 +01:00
|
|
|
NumberInput as ChakraNumberInput,
|
2021-12-23 13:49:24 +01:00
|
|
|
NumberInputField,
|
|
|
|
NumberInputStepper,
|
|
|
|
NumberIncrementStepper,
|
|
|
|
NumberDecrementStepper,
|
2023-02-19 09:53:57 +01:00
|
|
|
HStack,
|
|
|
|
FormControl,
|
|
|
|
FormLabel,
|
2023-04-17 16:47:17 +02:00
|
|
|
Stack,
|
2023-09-22 17:12:15 +02:00
|
|
|
Text,
|
2023-12-13 10:22:02 +01:00
|
|
|
FormHelperText,
|
2021-12-23 13:49:24 +01:00
|
|
|
} from '@chakra-ui/react'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { Variable, VariableString } from '@typebot.io/schemas'
|
2023-12-13 10:22:02 +01:00
|
|
|
import { ReactNode, useEffect, useState } from 'react'
|
2022-03-25 11:28:49 +01:00
|
|
|
import { useDebouncedCallback } from 'use-debounce'
|
2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2023-02-19 09:53:57 +01:00
|
|
|
import { MoreInfoTooltip } from '../MoreInfoTooltip'
|
2021-12-23 13:49:24 +01:00
|
|
|
|
2023-03-09 08:46:36 +01:00
|
|
|
type Value<HasVariable> = HasVariable extends true | undefined
|
2023-02-19 09:53:57 +01:00
|
|
|
? number | VariableString
|
|
|
|
: number
|
|
|
|
|
|
|
|
type Props<HasVariable extends boolean> = {
|
2023-03-09 08:46:36 +01:00
|
|
|
defaultValue: Value<HasVariable> | undefined
|
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
|
2023-04-17 16:47:17 +02:00
|
|
|
direction?: 'row' | 'column'
|
2023-09-22 17:12:15 +02:00
|
|
|
suffix?: string
|
2023-12-13 10:22:02 +01:00
|
|
|
helperText?: ReactNode
|
2023-02-19 09:53:57 +01:00
|
|
|
onValueChange: (value?: Value<HasVariable>) => void
|
|
|
|
} & Omit<NumberInputProps, 'defaultValue' | 'value' | 'onChange' | 'isRequired'>
|
2023-01-26 18:23:09 +01:00
|
|
|
|
2023-03-03 09:01:11 +01:00
|
|
|
export const NumberInput = <HasVariable extends boolean>({
|
2023-02-19 09:53:57 +01:00
|
|
|
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,
|
2023-12-13 10:22:02 +01:00
|
|
|
direction = 'column',
|
2023-09-22 17:12:15 +02:00
|
|
|
suffix,
|
2023-12-13 10:22:02 +01:00
|
|
|
helperText,
|
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,
|
2023-08-28 09:13:53 +02:00
|
|
|
env.NEXT_PUBLIC_E2E_TEST ? 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
|
|
|
|
2023-05-03 18:09:25 -04:00
|
|
|
const handleValueChange = (newValue: string) => {
|
|
|
|
if (value.startsWith('{{') && value.endsWith('}}') && newValue !== '')
|
|
|
|
return
|
|
|
|
setValue(newValue)
|
|
|
|
if (newValue.endsWith('.') || newValue.endsWith(',')) return
|
|
|
|
if (newValue === '') return onValueChangeDebounced(undefined)
|
2023-02-19 09:53:57 +01:00
|
|
|
if (
|
2023-05-03 18:09:25 -04:00
|
|
|
newValue.startsWith('{{') &&
|
|
|
|
newValue.endsWith('}}') &&
|
|
|
|
newValue.length > 4 &&
|
2023-02-19 09:53:57 +01:00
|
|
|
(withVariableButton ?? true)
|
|
|
|
) {
|
2023-05-03 18:09:25 -04:00
|
|
|
onValueChangeDebounced(newValue as Value<HasVariable>)
|
2023-02-19 09:53:57 +01:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 18:09:25 -04:00
|
|
|
const numberedValue = parseFloat(newValue)
|
|
|
|
if (isNaN(numberedValue)) return
|
|
|
|
onValueChangeDebounced(numberedValue)
|
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 = (
|
2023-12-13 10:22:02 +01:00
|
|
|
<ChakraNumberInput
|
|
|
|
onChange={handleValueChange}
|
|
|
|
value={value}
|
|
|
|
w="full"
|
|
|
|
{...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>
|
2023-03-03 09:01:11 +01:00
|
|
|
</ChakraNumberInput>
|
2021-12-23 13:49:24 +01:00
|
|
|
)
|
2023-02-19 09:53:57 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl
|
2023-04-17 16:47:17 +02:00
|
|
|
as={direction === 'column' ? Stack : HStack}
|
2023-02-19 09:53:57 +01:00
|
|
|
isRequired={isRequired}
|
|
|
|
justifyContent="space-between"
|
2024-01-03 16:29:41 +01:00
|
|
|
width={label || props.width === 'full' ? 'full' : 'auto'}
|
2023-09-22 17:12:15 +02:00
|
|
|
spacing={direction === 'column' ? 2 : 3}
|
2023-02-19 09:53:57 +01:00
|
|
|
>
|
|
|
|
{label && (
|
2023-12-13 10:22:02 +01:00
|
|
|
<FormLabel display="flex" flexShrink={0} gap="1" mb="0" mr="0">
|
2023-02-19 09:53:57 +01:00
|
|
|
{label}{' '}
|
|
|
|
{moreInfoTooltip && (
|
|
|
|
<MoreInfoTooltip>{moreInfoTooltip}</MoreInfoTooltip>
|
|
|
|
)}
|
|
|
|
</FormLabel>
|
|
|
|
)}
|
2023-12-13 10:22:02 +01:00
|
|
|
<HStack w={direction === 'row' ? undefined : 'full'}>
|
2023-09-22 17:12:15 +02:00
|
|
|
{withVariableButton ?? true ? (
|
2023-12-13 10:22:02 +01:00
|
|
|
<HStack spacing="0" w="full">
|
2023-09-22 17:12:15 +02:00
|
|
|
{Input}
|
|
|
|
<VariablesButton onSelectVariable={handleVariableSelected} />
|
|
|
|
</HStack>
|
|
|
|
) : (
|
|
|
|
Input
|
|
|
|
)}
|
|
|
|
{suffix ? <Text>{suffix}</Text> : null}
|
|
|
|
</HStack>
|
2023-12-13 10:22:02 +01:00
|
|
|
{helperText ? <FormHelperText mt="0">{helperText}</FormHelperText> : null}
|
2023-02-19 09:53:57 +01:00
|
|
|
</FormControl>
|
|
|
|
)
|
2021-12-23 13:49:24 +01:00
|
|
|
}
|