Skip to main content

Forms Hooks

Package Reference: This documentation covers the Forms hooks built on top of the @mapples/form package. For complete API documentation, see the @mapples/form package reference.

This guide covers all the available hooks in the Forms system and how to use them effectively in your React Native applications.

Core Hooks

useFormField

Hook for managing individual form fields with validation and state.

Function Reference: useFormField - Complete API documentation for the useFormField hook.

🔍 Field Management

useFormField provides field-specific state, validation, and event handlers:

import { Form, useFormField } from '@mapples/form';

const EmailForm = () => {
return (
<Form onSubmit={(data) => console.log('Email form:', data)}>
<EmailField />
</Form>
);
};

const EmailField = () => {
const {
value,
error,
errors,
isTouched,
setValue,
touch,
validate
} = useFormField('email', '');

return (
<View>
<TextInput
value={value || ''}
onChangeText={setValue}
onBlur={() => touch('email')}
placeholder="Email"
keyboardType="email-address"
autoCapitalize="none"
/>
{isTouched && error && <Text style={{ color: 'red' }}>{error}</Text>}
</View>
);
};

useFormField Parameters

  • fieldPath: string - Dot notation path to the field (e.g., 'user.name')
  • fallbackValue?: T - Default value to use if field is undefined
  • subscribe?: boolean - Whether to subscribe to real-time value changes (default: true)

useFormField Returns

interface UseFormFieldReturns {
value: T | undefined;
error: string | undefined;
errors: ValidationError[] | undefined;
isTouched: boolean;
validate: () => void;
touch: (fieldName: string) => void;
setValue: (newValue: T) => void;
}

Additional Hooks

useFormArrayField

Hook for managing array fields with specialized array manipulation methods.

Function Reference: useFormArrayField - Complete API documentation for the useFormArrayField hook.

📝 Dynamic Fields

useFormArrayField provides functionality for dynamic form fields and array management:

import { useFormArrayField } from '@mapples/form';

const TagsForm = () => {
const { value, api } = useFormArrayField('tags', []);

const addTag = () => {
api.push('New Tag');
};

const removeTag = (index: number) => {
api.remove(index);
};

return (
<View>
{value?.map((tag, index) => (
<View key={index}>
<Text>{tag}</Text>
<Button
title="Remove"
onPress={() => removeTag(index)}
/>
</View>
))}
<Button title="Add Tag" onPress={addTag} />
<Button title="Remove First" onPress={() => api.dropLeft(1)} />
<Button title="Remove Last" onPress={() => api.dropRight(1)} />
</View>
);
};

useFormFieldToggle

Hook for managing toggle/boolean form fields.

Function Reference: useFormFieldToggle - Complete API documentation for the useFormFieldToggle hook.

🔘 Toggle Fields

useFormFieldToggle provides functionality for boolean/toggle form fields:

import { useFormFieldToggle } from '@mapples/form';

const ToggleField = () => {
const { value, toggle } = useFormFieldToggle('acceptTerms', false);

return (
<View>
<Switch value={value} onValueChange={toggle} />
<Text>Accept Terms: {value ? 'Yes' : 'No'}</Text>
</View>
);
};

useBindFormField

Hook for binding form fields with external components.

Function Reference: useBindFormField - Complete API documentation for the useBindFormField hook.

🔗 Field Binding

useBindFormField provides field binding functionality:

import { useBindFormField } from '@mapples/form';

const BoundField = () => {
const fieldProps = useBindFormField('username');

return (
<TextInput
{...fieldProps}
placeholder="Username"
/>
);
};

Context Hooks

useFormContext

Hook for accessing form context containing form state and methods.

Function Reference: useFormContext - Complete API documentation for the useFormContext hook.

🔗 Context Access

useFormContext provides access to form context in deeply nested components:

import { useFormContext } from '@mapples/form';

const NestedComponent = () => {
const { form, submitting, submitForm, validationErrors } = useFormContext();

return (
<View>
<Button
title={submitting ? 'Submitting...' : 'Submit'}
onPress={submitForm}
disabled={submitting}
/>
{validationErrors && (
<Text style={{ color: 'red' }}>Validation errors occurred</Text>
)}
</View>
);
};

State Management Hooks

useFormState

Hook for accessing form state without re-rendering.

Function Reference: useFormState - Complete API documentation for the useFormState hook.

⚡ Performance

useFormState provides access to form state without causing re-renders:

import { useFormState } from '@mapples/form';

const FormStateDisplay = () => {
const { isValid, isDirty, isSubmitting, errorCount } = useFormState();

return (
<View>
<Text>Form is valid: {isValid ? 'Yes' : 'No'}</Text>
<Text>Form is dirty: {isDirty ? 'Yes' : 'No'}</Text>
<Text>Submitting: {isSubmitting ? 'Yes' : 'No'}</Text>
<Text>Error count: {errorCount}</Text>
</View>
);
};

useFormValue

Hook for watching specific form field values and changes.

Function Reference: useFormValue - Complete API documentation for the useFormValue hook.

👀 Value Watching

useFormValue allows you to watch specific form field values:

import { useFormValue } from '@mapples/form';

const FormWatcher = () => {
const email = useFormValue('email');
const password = useFormValue('password');

return (
<View>
<Text>Current email: {email}</Text>
<Text>Current password length: {password?.length || 0}</Text>
</View>
);
};

Advanced Hooks

useFormSubmit

Hook for form submission handling.

Function Reference: useFormSubmit - Complete API documentation for the useFormSubmit hook.

📤 Form Submission

useFormSubmit provides form submission functionality:

import { useFormSubmit } from '@mapples/form';

const FormWithSubmit = () => {
const submitForm = useFormSubmit();

const handleSubmit = () => {
submitForm();
};

return (
<View>
<Button title="Submit" onPress={handleSubmit} />
</View>
);
};

useFormHistory

Hook for managing form history and undo/redo functionality.

Function Reference: useFormHistory - Complete API documentation for the useFormHistory hook.

📜 Form History

useFormHistory provides form history management:

import { useFormHistory } from '@mapples/form';

const FormWithHistory = () => {
const { canUndo, canRedo, undo, redo } = useFormHistory();

return (
<View>
<Button
title="Undo"
onPress={undo}
disabled={!canUndo}
/>
<Button
title="Redo"
onPress={redo}
disabled={!canRedo}
/>
</View>
);
};

Best Practices

Hook Composition

🎯 Composition

Combine multiple hooks for complex form functionality:

import { Form, useFormField, useFormArrayField, useFormContext } from '@mapples/form';

const ComplexForm = () => {
return (
<Form onSubmit={(data) => console.log('Form submitted:', data)}>
<UserInfoSection />
<TagsSection />
<SubmitSection />
</Form>
);
};

const UserInfoSection = () => {
const { value: name, setValue: setName } = useFormField('name', '');
const { value: email, setValue: setEmail } = useFormField('email', '');

return (
<View>
<TextInput value={name} onChangeText={setName} placeholder="Name" />
<TextInput value={email} onChangeText={setEmail} placeholder="Email" />
</View>
);
};

const TagsSection = () => {
const { value: tags, api } = useFormArrayField('tags', []);

return (
<View>
{tags?.map((tag, index) => (
<View key={index}>
<Text>{tag}</Text>
<Button title="Remove" onPress={() => api.remove(index)} />
</View>
))}
<Button title="Add Tag" onPress={() => api.push('New Tag')} />
</View>
);
};

const SubmitSection = () => {
const { form, submitting, submitForm } = useFormContext();

return (
<Button
title={submitting ? 'Submitting...' : 'Submit'}
onPress={submitForm}
disabled={submitting}
/>
);
};

Performance Optimization

⚡ Performance

Optimize your forms for better performance:

import { memo, useCallback } from 'react';
import { Form, useFormField } from '@mapples/form';

const OptimizedForm = memo(() => {
const onSubmit = useCallback((data) => {
console.log('Form submitted:', data);
}, []);

return (
<Form onSubmit={onSubmit}>
<OptimizedField />
</Form>
);
});

const OptimizedField = memo(() => {
const { value, setValue } = useFormField('username', '');

return (
<TextInput
value={value}
onChangeText={setValue}
placeholder="Username"
/>
);
});

What's Next?

📦 Installation

Installation and configuration guide for using Forms in your projects.

Get Started →

📚 API Reference

Complete technical documentation for all Forms functions and components.

Full API →

🏗️ Forms Overview

Learn about the Forms system architecture and core concepts.

System Overview →