2022-02-04 19:00:08 +01:00
|
|
|
import {
|
|
|
|
EditablePreview,
|
|
|
|
EditableInput,
|
|
|
|
Editable,
|
|
|
|
Fade,
|
|
|
|
IconButton,
|
|
|
|
Flex,
|
|
|
|
} from '@chakra-ui/react'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { PlusIcon } from '@/components/icons'
|
|
|
|
import { useTypebot } from '@/features/editor'
|
2022-02-04 19:00:08 +01:00
|
|
|
import { ButtonItem, ItemIndices, ItemType } from 'models'
|
2022-11-17 17:46:24 +01:00
|
|
|
import React, { useRef, useState } from 'react'
|
2022-02-04 19:00:08 +01:00
|
|
|
import { isNotDefined } from 'utils'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
item: ButtonItem
|
|
|
|
indices: ItemIndices
|
|
|
|
isMouseOver: boolean
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:56:09 +01:00
|
|
|
export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
|
2022-02-04 19:00:08 +01:00
|
|
|
const { deleteItem, updateItem, createItem } = useTypebot()
|
|
|
|
const [itemValue, setItemValue] = useState(item.content ?? 'Click to edit')
|
|
|
|
const editableRef = useRef<HTMLDivElement | null>(null)
|
|
|
|
|
|
|
|
const handleInputSubmit = () => {
|
|
|
|
if (itemValue === '') deleteItem(indices)
|
|
|
|
else
|
|
|
|
updateItem(indices, { content: itemValue === '' ? undefined : itemValue })
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (e.key === 'Escape' && itemValue === 'Click to edit') deleteItem(indices)
|
2022-11-17 17:46:24 +01:00
|
|
|
if (e.key === 'Enter' && itemValue !== '' && itemValue !== 'Click to edit')
|
2022-02-04 19:00:08 +01:00
|
|
|
handlePlusClick()
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlePlusClick = () => {
|
|
|
|
const itemIndex = indices.itemIndex + 1
|
|
|
|
createItem(
|
2022-06-11 07:27:38 +02:00
|
|
|
{ blockId: item.blockId, type: ItemType.BUTTON },
|
2022-02-04 19:00:08 +01:00
|
|
|
{ ...indices, itemIndex }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-31 09:49:23 +02:00
|
|
|
<Flex px={4} py={2} justify="center" w="90%" pos="relative">
|
2022-02-04 19:00:08 +01:00
|
|
|
<Editable
|
|
|
|
ref={editableRef}
|
|
|
|
flex="1"
|
|
|
|
startWithEditView={isNotDefined(item.content)}
|
|
|
|
value={itemValue}
|
|
|
|
onChange={setItemValue}
|
|
|
|
onSubmit={handleInputSubmit}
|
|
|
|
onKeyDownCapture={handleKeyPress}
|
2022-02-25 14:36:28 +01:00
|
|
|
maxW="180px"
|
2022-02-04 19:00:08 +01:00
|
|
|
>
|
|
|
|
<EditablePreview
|
|
|
|
w="full"
|
|
|
|
color={item.content !== 'Click to edit' ? 'inherit' : 'gray.500'}
|
|
|
|
cursor="pointer"
|
|
|
|
/>
|
|
|
|
<EditableInput />
|
|
|
|
</Editable>
|
|
|
|
<Fade
|
|
|
|
in={isMouseOver}
|
2022-03-31 09:49:23 +02:00
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: '-15px',
|
|
|
|
zIndex: 3,
|
|
|
|
left: '90px',
|
|
|
|
}}
|
2022-02-04 19:00:08 +01:00
|
|
|
unmountOnExit
|
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
aria-label="Add item"
|
|
|
|
icon={<PlusIcon />}
|
|
|
|
size="xs"
|
|
|
|
shadow="md"
|
2022-03-31 09:49:23 +02:00
|
|
|
colorScheme="gray"
|
2022-02-04 19:00:08 +01:00
|
|
|
onClick={handlePlusClick}
|
|
|
|
/>
|
|
|
|
</Fade>
|
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
}
|