93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { PlusIcon } from 'lucide-react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { z } from 'zod'
|
|
|
|
import { Button } from '@/components/ui/button'
|
|
import { Field, FieldError } from '@/components/ui/field'
|
|
import { Input } from '@/components/ui/input'
|
|
import { useCreateItem } from '@/hooks/use-items'
|
|
import { ApiError } from '@/lib/api-client'
|
|
|
|
const createItemSchema = z.object({
|
|
name: z.string().trim().min(1, 'Escribe un artículo').max(120, 'Máximo 120 caracteres'),
|
|
})
|
|
|
|
type CreateItemValues = z.infer<typeof createItemSchema>
|
|
|
|
type CreateItemFormProps = {
|
|
listId: string
|
|
/** Focus the input when the form mounts, so the user can type right away. */
|
|
autoFocus?: boolean
|
|
}
|
|
|
|
/** The input carries this id so it can be focused from outside the form (e.g. by the nav). */
|
|
const NEW_ITEM_INPUT_ID = 'new-item-input'
|
|
|
|
/**
|
|
* Puts the cursor in the "add an item" input when the home page is already the one on
|
|
* screen (the route does not remount, so nothing else would move the focus). When the
|
|
* user is navigating *to* the home page this is a no-op: the input takes the focus as
|
|
* soon as it mounts, through `autoFocus`. Either way, tapping "Inicio" always ends with
|
|
* the focus in the input — and never with the focus on another page's input.
|
|
*
|
|
* @param pathname the route the link was clicked from, so only the home input is focused.
|
|
*/
|
|
export function focusNewItemInput(pathname: string): void {
|
|
if (pathname !== '/') return
|
|
refocusNewItemInput()
|
|
}
|
|
|
|
/** Puts the cursor back in the "add an item" input of the page on screen, if it has one. */
|
|
export function refocusNewItemInput(): void {
|
|
document.getElementById(NEW_ITEM_INPUT_ID)?.focus()
|
|
}
|
|
|
|
/** Input to add an item to a list. Submits with Enter or the button. */
|
|
export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProps) {
|
|
const createItem = useCreateItem(listId)
|
|
|
|
const form = useForm<CreateItemValues>({
|
|
resolver: zodResolver(createItemSchema),
|
|
defaultValues: { name: '' },
|
|
})
|
|
|
|
// The input clears at once (the item is added optimistically) so several items
|
|
// can be typed in a row; if the request fails the text comes back with the error.
|
|
const onSubmit = form.handleSubmit((values) => {
|
|
form.reset()
|
|
createItem.mutate(values, {
|
|
onError: (error) => {
|
|
form.setValue('name', values.name)
|
|
form.setError('name', {
|
|
message:
|
|
error instanceof ApiError
|
|
? (error.problem.detail ?? error.problem.title)
|
|
: 'No pudimos añadir el artículo.',
|
|
})
|
|
},
|
|
})
|
|
})
|
|
|
|
return (
|
|
<form onSubmit={onSubmit} className="flex items-start gap-2">
|
|
<Field className="flex-1" data-invalid={!!form.formState.errors.name}>
|
|
<Input
|
|
id={NEW_ITEM_INPUT_ID}
|
|
aria-label="Nuevo artículo"
|
|
autoComplete="off"
|
|
placeholder="Añade un artículo…"
|
|
autoFocus={autoFocus}
|
|
{...form.register('name')}
|
|
/>
|
|
<FieldError errors={[form.formState.errors.name]} />
|
|
</Field>
|
|
|
|
<Button type="submit">
|
|
<PlusIcon />
|
|
Agregar
|
|
</Button>
|
|
</form>
|
|
)
|
|
}
|