Docyrus

Form

Form-library-agnostic form wrapper that connects DocyField components to any state management library.

iOSAndroid
Preview Form on your device

Scan with Expo Go

Download Expo Go, then scan the QR code to preview native components.

Installation

pnpm dlx @docyrus/cli add @docyrus/rn-form

Overview

DocyForm is a form-library-agnostic wrapper that connects DocyField* components to any form state management library via the adapter pattern. You implement a FormAdapter interface for your chosen library (react-hook-form, TanStack Form, etc.), and DocyForm handles the wiring.

Usage

With react-hook-form

import { DocyForm, DocyFormField } from '@/components/docyrus-native/form';
import { DocyFieldText } from '@/components/docyrus-native/form-fields/docy-field-text';
import { DocyFieldEmail } from '@/components/docyrus-native/form-fields/docy-field-email';

// Create adapter from your form library
const adapter = createReactHookFormAdapter({
  defaultValues: { name: '', email: '' },
  onSubmit: (data) => console.log(data),
});

<DocyForm adapter={adapter}>
  {({ handleSubmit }) => (
    <>
      <DocyFormField name="name">
        <DocyFieldText name="name" label="Full Name" />
      </DocyFormField>

      <DocyFormField name="email">
        <DocyFieldEmail name="email" label="Email" />
      </DocyFormField>

      <Button onPress={handleSubmit}>Submit</Button>
    </>
  )}
</DocyForm>

Compound Fields

For fields that manage multiple form values (e.g. DocyFieldMoney needs both amount and currency):

<DocyFormField name="price" compoundFields={{ currency: 'price_currency' }}>
  <DocyFieldMoney name="price" label="Price" />
</DocyFormField>

API Reference

DocyForm

PropTypeDefaultDescription
adapterFormAdapter<T>—Required. Form library adapter
childrenReactNode | (methods) => ReactNode—Form content. Render-prop provides handleSubmit and reset
classNamestring—Container style

DocyFormField

PropTypeDefaultDescription
namestring—Required. Field name in the form
childrenReactElement—Required. DocyField component to connect
validationsDocyFieldValidationRule[]—Additional validation rules
defaultValueunknown—Default value for this field
compoundFieldsRecord<string, string>—Maps prop names to additional form field names

FormAdapter Interface

Implement this interface to plug in your form library:

MethodSignatureDescription
FormWrapper(props: { children: ReactNode }) => ReactElementWraps children with library-specific provider
getValue(name: string) => unknownGet current field value
setValue(name: string, value: unknown) => voidSet field value programmatically
getError(name: string) => string | undefinedGet field error message
getFieldProps(name: string) => FormAdapterFieldPropsGet props for a field (value, onChange, onBlur, error)
handleSubmit() => void | Promise<void>Run validation and submit
reset(values?: Partial<T>) => voidReset form to defaults
getValues() => TGet all current form values

Hooks

HookReturnsDescription
useFormAdapter<T>()FormAdapter<T>Access the form adapter from within DocyForm
useSimpleFormAdapter<T>(options)FormAdapter<T>Local-state adapter (no form library)

useSimpleFormAdapter options

OptionTypeDefaultDescription
defaultValuesTrequiredInitial values
onSubmit(values: T) => void | Promise<void>—Called by handleSubmit once validation passes
validationsPartial<Record<keyof T, string[]>>—Validation tokens per field (required, minLength:N, maxLength:N, pattern:RE, min:N, max:N), enforced on submit with the shared token parser. Errors surface through getError / getFieldProps().error and clear when the field changes
labelsPartial<Record<keyof T, string>>field nameLabels used in validation messages

For schema-driven forms (layout sections, computed fields, actions) use useDynamicFormView instead.

Components

ComponentDescription
DocyFormForm wrapper with adapter context
DocyFormFieldConnects a DocyField component to the form adapter

Type Exports

TypeDescription
DocyFormPropsProps for DocyForm component
DocyFormFieldPropsProps for DocyFormField component
FormAdapterForm library adapter interface
FormAdapterFieldPropsField props returned by adapter ({ name, value, onChange, onBlur, error })

On this page