@chakra-ui/react#useMergeRefs TypeScript Examples
The following examples show how to use
@chakra-ui/react#useMergeRefs.
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: index.tsx From calories-in with MIT License | 5 votes |
function Form({
ownerName,
notes,
onClose,
initialRef,
onEditNotes,
fieldId,
textAreaHeight,
}: Props) {
const { register, handleSubmit } = useFormContext()
const notesRegister = register('notes')
const notesInputRef = useMergeRefs(notesRegister.ref, initialRef)
const oneTimeCheckActions = useOneTimeCheckActions()
const onSubmit = handleSubmit((form: NotesForm) => {
oneTimeCheckActions.set(`notes-${fieldId}`)
onEditNotes(form.notes || undefined)
onClose()
})
const { errorMessage, isInvalid } = useFormError('name')
return (
<form onSubmit={onSubmit}>
<ModalContent>
<Header ownerName={ownerName} notes={notes} />
<ModalCloseButton />
<ModalBody>
<FormControl isInvalid={isInvalid}>
<FormLabel>Notes</FormLabel>
<Textarea
autoComplete="off"
{...notesRegister}
ref={notesInputRef}
focusBorderColor={isInvalid ? 'red.500' : undefined}
placeholder="Enter notes"
height={textAreaHeight}
/>
<Collapse animateOpacity={true} in={Boolean(errorMessage)}>
<Box minHeight="21px">
<FormErrorMessage>{errorMessage}</FormErrorMessage>
</Box>
</Collapse>
</FormControl>
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={onClose}>
Close
</Button>
<Button
type="submit"
colorScheme="teal"
variant="solid"
onClick={onSubmit}
>
Save
</Button>
</ModalFooter>
</ModalContent>
</form>
)
}
Example #2
Source File: useGetInputElement.tsx From calories-in with MIT License | 5 votes |
function useGetInputElement({
inputType,
name,
isInvalid,
nutritionValueUnit,
isReadOnly,
textInputRef,
isBold = false,
}: Params) {
const { register } = useFormContext()
let result: ReactElement | null = null
const textInputRegister = register(name)
const finalTextInputRef = useMergeRefs(textInputRegister.ref, textInputRef)
if (isReadOnly) {
result = (
<ReadOnlyInput
name={name}
inputType={inputType}
nutritionValueUnit={nutritionValueUnit}
isBold={isBold}
/>
)
} else if (inputType === 'text') {
result = (
<Input
autoComplete="off"
{...textInputRegister}
ref={finalTextInputRef}
fontWeight={isBold ? 'semibold' : 'normal'}
width={{ base: '150px', md: '200px' }}
/>
)
} else if (inputType === 'foodCategory') {
result = (
<FoodCategoriesSelect
{...register(name, {
valueAsNumber: true,
})}
width={{ base: '150px', md: '200px' }}
>
<option disabled value={undefined}>
Select category
</option>
</FoodCategoriesSelect>
)
} else if (inputType === 'nutritionValue') {
result = (
<Controller
name={name}
render={({ field }) => (
<AmountInput value={field.value} onChange={field.onChange} />
)}
/>
)
}
if (!result) {
throw new Error()
}
if (isInvalid) {
return cloneElement(result, {
focusBorderColor: 'red.500',
})
}
return result
}
Example #3
Source File: Content.tsx From calories-in with MIT License | 5 votes |
function Content({ title, onClose, initialRef, variantFormIndex }: Props) {
const { register } = useFormContext()
const nameRegister = register('name')
const nameInputRef = useMergeRefs(nameRegister.ref, initialRef)
const onSubmit = useSubmitVariantNameForm({
variantFormIndex,
onComplete: onClose,
})
const { errorMessage, isInvalid } = useFormError('name')
useSelectInputText(initialRef)
return (
<form onSubmit={onSubmit}>
<ModalContent>
<ModalHeader>{title}</ModalHeader>
<ModalCloseButton />
<ModalBody>
<FormControl isInvalid={isInvalid}>
<FormLabel>Name</FormLabel>
<Input
autoComplete="off"
{...nameRegister}
ref={nameInputRef}
focusBorderColor={isInvalid ? 'red.500' : undefined}
placeholder="Enter name"
/>
<Collapse animateOpacity={true} in={Boolean(errorMessage)}>
<Box minHeight="21px">
<FormErrorMessage>{errorMessage}</FormErrorMessage>
</Box>
</Collapse>
</FormControl>
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={onClose}>
Close
</Button>
<Button
type="submit"
colorScheme="teal"
variant="solid"
onClick={onSubmit}
>
Rename
</Button>
</ModalFooter>
</ModalContent>
</form>
)
}