@letar/forms

Утилиты

Хелперы схем, мета-функции и провайдеры связей

UI Metadata хелперы

withUIMeta / withUIMetaDeep

Добавление UI метаданных к полям схемы:

import { withUIMeta, withUIMetaDeep } from '@letar/forms'

const nameField = withUIMeta(z.string(), { title: 'Имя', placeholder: 'Введите имя' })

const Schema = withUIMetaDeep(
  z.object({
    name: z.string(),
    email: z.string().email(),
  }),
  {
    name: { title: 'Имя' },
    email: { title: 'Email' },
  }
)

Типо-специфичные хелперы

import { textMeta, numberMeta, booleanMeta, dateMeta, enumMeta, relationMeta } from '@letar/forms'

z.string().meta(textMeta('Заголовок', { placeholder: 'Введите...' }))
z.number().meta(numberMeta('Цена', { min: 0 }))
z.boolean().meta(booleanMeta('Активный'))

Обход схемы

traverseSchema

Обход всех полей схемы:

traverseSchema(Schema, (path, fieldSchema) => {
  console.log(path, fieldSchema._def.typeName)
})

getFieldPaths

Получить все пути полей:

const paths = getFieldPaths(Schema)
// ['name', 'email', 'address.street', 'address.city']

Извлечение constraints

getZodConstraints

const constraints = getZodConstraints(z.string().min(2).max(100).email())
// { minLength: 2, maxLength: 100, format: 'email' }

generateConstraintHint

Генерация подсказки из constraints с поддержкой i18n:

generateConstraintHint(constraints) // → "Minimum 2 characters"
generateConstraintHint(constraints, 'ru') // → "Минимум 2 символа"

createForm

Создание расширенной формы. См. createForm guide.

const AppForm = createForm({
  extraFields: { PlateNumber: FieldPlateNumber },
  lazySelects: {
    Category: () => import('./selects/select-category').then((m) => m.SelectCategory),
  },
})

RelationFieldProvider

Предоставляет данные связей для полей формы:

<RelationFieldProvider
  relations={{
    category: { useQuery: useFindManyCategory, labelField: 'name' },
  }}
>
  <Form schema={Schema} onSubmit={save}>
    <Form.Field.Select name="categoryId" />
  </Form>
</RelationFieldProvider>

withRelations

HOC-альтернатива RelationFieldProvider:

const EnhancedForm = withRelations(BaseForm, {
  category: { useQuery: useFindManyCategory, labelField: 'name' },
})

См. Связанные поля.

On this page