@chakra-ui/react#EditablePreview TypeScript Examples

The following examples show how to use @chakra-ui/react#EditablePreview. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Name.tsx    From calories-in with MIT License 5 votes vote down vote up
function Name() {
  const dietForm = useDietForm()
  const dietFormActions = useDietFormActions()
  const editablePreviewRef = useRef<HTMLDivElement>(null)
  const focusedForDietFieldIdMap = useRef<Record<string, boolean | undefined>>(
    {}
  )

  useEffect(() => {
    if (
      !canExportDietForm(dietForm) &&
      editablePreviewRef.current &&
      !focusedForDietFieldIdMap.current[dietForm.fieldId]
    ) {
      editablePreviewRef.current.focus()
      focusedForDietFieldIdMap.current[dietForm.fieldId] = true
    }
  }, [dietForm])

  function onNameChange(event: ChangeEvent<HTMLInputElement>) {
    const { value } = event.target
    dietFormActions.updateDietForm({ name: value })
  }

  const boxShadowColor = getComputedColorFromChakra('teal.400')

  return (
    <Editable
      fontSize="lg"
      fontWeight="medium"
      textAlign="center"
      value={dietForm.name}
      width="85%"
    >
      <EditablePreview ref={editablePreviewRef} width="100%" />
      <EditableInput
        _focus={{ boxShadow: `${boxShadowColor} 0px 0px 0px 3px` }}
        onChange={onNameChange}
      />
    </Editable>
  )
}