All files / src/features/milestones/components MilestoneCoreFields.tsx

100% Statements 9/9
100% Branches 12/12
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                      125x   625x             125x 125x 125x   125x                                                 125x                                                 125x                                                  
import { Controller, type Control, type FieldErrors, type UseFormRegister } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
 
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { DatePickerField } from '@/shared/components/DatePickerField'
import { SelectorCardGroup } from '@/shared/components/SelectorCardGroup'
import { fieldErrorKey } from '@/shared/utils/zod-error'
 
import { milestoneCategorySchema, type MilestoneFormInput } from '../api/milestones.schemas'
import { MILESTONE_CATEGORY_META } from './category-meta'
 
interface MilestoneCoreFieldsProps {
  control: Control<MilestoneFormInput>
  register: UseFormRegister<MilestoneFormInput>
  errors: FieldErrors<MilestoneFormInput>
}
 
export function MilestoneCoreFields({ control, register, errors }: MilestoneCoreFieldsProps) {
  const { t } = useTranslation()
 
  const categoryOptions = milestoneCategorySchema.options.map((category) => ({
    value: category,
    label: t(`milestones.category.${category.toLowerCase()}`),
    description: t(`milestones.editor.categoryHints.${category.toLowerCase()}`),
    media: <span className="text-xl">{MILESTONE_CATEGORY_META[category].emoji}</span>,
  }))
 
  const categoryErrorKey = fieldErrorKey(errors.category)
  const titleErrorKey = fieldErrorKey(errors.title)
  const achievedAtErrorKey = fieldErrorKey(errors.achievedAt)
 
  return (
    <div className="space-y-6">
      <div className="grid grid-cols-1 gap-6 md:grid-cols-3">
        <div className="md:col-span-2">
          <Label htmlFor="title">{t('milestones.form.titleLabel')}</Label>
          <Input
            id="title"
            placeholder={t('milestones.form.titlePlaceholder')}
            aria-invalid={!!errors.title}
            aria-describedby={titleErrorKey ? 'title-error' : undefined}
            className="mt-2"
            {...register('title')}
          />
          {titleErrorKey && (
            <p id="title-error" className="text-destructive mt-1 text-sm">
              {t(titleErrorKey)}
            </p>
          )}
        </div>
        <div>
          <Label htmlFor="achievedAt">{t('milestones.form.achievedAtLabel')}</Label>
          <Controller
            control={control}
            name="achievedAt"
            render={({ field }) => (
              <DatePickerField
                id="achievedAt"
                value={field.value}
                ref={field.ref}
                onBlur={field.onBlur}
                onValueChange={field.onChange}
                aria-invalid={!!errors.achievedAt}
                aria-describedby={achievedAtErrorKey ? 'achievedAt-error' : undefined}
                className="mt-2"
              />
            )}
          />
          {achievedAtErrorKey && (
            <p id="achievedAt-error" className="text-destructive mt-1 text-sm">
              {t(achievedAtErrorKey)}
            </p>
          )}
        </div>
      </div>
      <div>
        <Label id="category-label">{t('milestones.form.categoryLabel')}</Label>
        <Controller
          control={control}
          name="category"
          render={({ field }) => (
            <div
              className="mt-2"
              role="group"
              aria-labelledby="category-label"
              aria-describedby={categoryErrorKey ? 'category-error' : undefined}
            >
              <SelectorCardGroup
                value={field.value}
                onValueChange={field.onChange}
                options={categoryOptions}
                layout="vertical"
                className="grid grid-cols-1 gap-2 sm:grid-cols-2 [&>label]:p-3"
              />
            </div>
          )}
        />
        {categoryErrorKey && (
          <p id="category-error" className="text-destructive mt-1 text-sm">
            {t(categoryErrorKey)}
          </p>
        )}
      </div>
    </div>
  )
}