````markdown # Form Component Documentation ## Overview A highly flexible and configurable dynamic form component built with **PrimeVue**. This component generates forms based on field configuration objects and supports various input types including **AutoComplete with custom values**, validation, responsive layouts, and both controlled and uncontrolled form state management. ## ✨ New Features (PrimeVue Migration) - **AutoComplete component** - Users can select from suggestions OR enter completely custom values - **Enhanced Date/Time Pickers** - Comprehensive date handling with multiple formats, time selection, constraints, and smart defaults - **Better accessibility** with ARIA support - **More flexible styling** with CSS custom properties - **Enhanced mobile responsiveness** with CSS Grid ## Basic Usage ```vue ``` ## Props ### `fields` (Array) - Required - **Description:** Array of field configuration objects that define the form structure - **Type:** `Array` - **Required:** `true` ### `formData` (Object) - **Description:** External form data object for controlled form state - **Type:** `Object` - **Default:** `null` - **Note:** When provided, the form operates in controlled mode. When null, uses internal state. ### `onChange` (Function) - **Description:** Global change handler function called when any field changes - **Type:** `Function` - **Signature:** `(fieldName: string, value: any, formData: Object) => void` ### `onSubmit` (Function) - **Description:** Submit handler function called when form is submitted - **Type:** `Function` - **Signature:** `(formData: Object) => Promise | void` ### `showSubmitButton` (Boolean) - **Description:** Controls visibility of the submit button - **Type:** `Boolean` - **Default:** `true` ### `showCancelButton` (Boolean) - **Description:** Controls visibility of the cancel button - **Type:** `Boolean` - **Default:** `false` ### `submitButtonText` (String) - **Description:** Text displayed on the submit button - **Type:** `String` - **Default:** `'Submit'` ### `cancelButtonText` (String) - **Description:** Text displayed on the cancel button - **Type:** `String` - **Default:** `'Cancel'` ### `validateOnChange` (Boolean) - **Description:** Enables real-time validation as fields change - **Type:** `Boolean` - **Default:** `true` ## Field Configuration Each field object in the `fields` array supports the following properties: ### Basic Properties - **`name`** (String, required) - Unique identifier for the field - **`label`** (String, required) - Display label for the field - **`type`** (String, required) - Field input type - **`required`** (Boolean, default: `false`) - Makes the field mandatory - **`disabled`** (Boolean, default: `false`) - Disables the field - **`readonly`** (Boolean, default: `false`) - Makes the field read-only - **`placeholder`** (String) - Placeholder text for input fields - **`helpText`** (String) - Help text displayed below the field - **`defaultValue`** (Any) - Initial value for the field ### Layout Properties - **`cols`** (Number, default: `12`) - Column width on extra small screens - **`sm`** (Number, default: `12`) - Column width on small screens - **`md`** (Number, default: `6`) - Column width on medium screens - **`lg`** (Number, default: `6`) - Column width on large screens ### Validation Properties - **`validate`** (Function) - Custom validation function - **Signature:** `(value: any) => string | null` - **Returns:** Error message string or null if valid ### Field-Specific Properties - **`onChangeOverride`** (Function) - Field-specific change handler that overrides global onChange - **Signature:** `(value: any, fieldName: string, formData: Object) => void` ## Field Types ### Text Input (`type: 'text'`) Standard text input field with optional format validation. ```javascript { name: 'username', label: 'Username', type: 'text', required: true, placeholder: 'Enter your username' } ``` **Additional Properties:** - **`format`** (String) - Input format validation (`'email'` for email validation) ### Number Input (`type: 'number'`) Numeric input field with optional min/max constraints. ```javascript { name: 'age', label: 'Age', type: 'number', min: 0, max: 120, step: 1 } ``` **Additional Properties:** - **`min`** (Number) - Minimum allowed value - **`max`** (Number) - Maximum allowed value - **`step`** (Number) - Step increment for the input ### Textarea (`type: 'textarea'`) Multi-line text input for longer content. ```javascript { name: 'description', label: 'Description', type: 'textarea', rows: 4, placeholder: 'Enter description...' } ``` **Additional Properties:** - **`rows`** (Number, default: `3`) - Number of visible text lines ### Select Dropdown (`type: 'select'`) Dropdown selection field with predefined options and built-in filtering. ```javascript { name: 'country', label: 'Country', type: 'select', required: true, options: [ { label: 'United States', value: 'us' }, { label: 'Canada', value: 'ca' }, { label: 'Mexico', value: 'mx' } ], filter: true, // Enable search filtering showClear: true // Allow clearing selection } ``` **Additional Properties:** - **`options`** (Array, required) - Array of option objects with `label` and `value` properties - **`optionLabel`** (String, default: `'label'`) - Property name for display text - **`optionValue`** (String, default: `'value'`) - Property name for value - **`filter`** (Boolean, default: `true`) - Enable filtering/search - **`showClear`** (Boolean, default: `true`) - Show clear button ### ⭐ AutoComplete (`type: 'autocomplete'`) - NEW! **The game-changer!** AutoComplete field that allows users to select from suggestions OR enter completely custom values not in the predefined list. ```javascript // Basic AutoComplete - allows custom entries { name: 'skills', label: 'Skills', type: 'autocomplete', placeholder: 'Add your skills', options: [ { label: 'JavaScript', value: 'js' }, { label: 'Python', value: 'python' }, { label: 'Vue.js', value: 'vue' } ], optionLabel: 'label', optionValue: 'value', forceSelection: false, // KEY: Allows custom values! multiple: true, // Multiple selection helpText: 'Select existing skills or add your own custom skills' } // Strict selection (only predefined options) { name: 'department', label: 'Department', type: 'autocomplete', options: [ { label: 'Engineering', value: 'eng' }, { label: 'Marketing', value: 'mkt' }, { label: 'Sales', value: 'sales' } ], forceSelection: true, // Only allows predefined options dropdown: true, // Show dropdown button required: true } // Dynamic/Remote data loading { name: 'users', label: 'Select Users', type: 'autocomplete', options: [], // Initially empty onSearch: async (query, callback) => { if (query.length > 2) { const response = await fetch(`/api/users?search=${query}`); const users = await response.json(); callback(users); // Update options dynamically } } } ``` **Additional Properties:** - **`options`** (Array) - Array of suggestion objects - **`optionLabel`** (String, default: `'label'`) - Property for display text - **`optionValue`** (String, default: `'value'`) - Property for value - **`forceSelection`** (Boolean, default: `false`) - If true, only allows predefined options. If false, allows custom values! - **`multiple`** (Boolean, default: `false`) - Allow multiple selections - **`dropdown`** (Boolean, default: `true`) - Show dropdown button - **`onSearch`** (Function) - Custom search function for dynamic data loading - **Signature:** `(query: string, callback: Function) => void` ### Checkbox (`type: 'checkbox'`) Boolean checkbox input. ```javascript { name: 'subscribe', label: 'Subscribe to newsletter', type: 'checkbox', defaultValue: false } ``` ### Radio Group (`type: 'radio'`) Radio button group for single selection from multiple options. ```javascript { name: 'gender', label: 'Gender', type: 'radio', required: true, options: [ { label: 'Male', value: 'male' }, { label: 'Female', value: 'female' }, { label: 'Other', value: 'other' } ] } ``` **Additional Properties:** - **`options`** (Array, required) - Array of option objects with `label` and `value` properties ### Date Input (`type: 'date'`) Enhanced date picker input field with comprehensive formatting and configuration options. ```javascript // Basic date input { name: 'birthDate', label: 'Birth Date', type: 'date', required: true, } // Date with custom format { name: 'eventDate', label: 'Event Date', type: 'date', format: 'YYYY-MM-DD', // or 'mm/dd/yyyy', 'dd/mm/yyyy', etc. required: true, placeholder: 'Select event date' } // Date with time picker { name: 'appointmentDateTime', label: 'Appointment Date & Time', type: 'date', showTime: true, hourFormat: '12', // '12' or '24' required: true, defaultToNow: true, // Set to current date/time by default } // Time-only picker { name: 'preferredTime', label: 'Preferred Time', type: 'date', timeOnly: true, hourFormat: '12', stepMinute: 15, // 15-minute intervals defaultValue: 'now' } // Advanced date configuration { name: 'projectDeadline', label: 'Project Deadline', type: 'date', format: 'dd/mm/yyyy', minDate: 'today', // Can't select past dates maxDate: '2025-12-31', // Maximum date defaultToToday: true, showButtonBar: true, yearNavigator: true, monthNavigator: true, yearRange: '2024:2030', helpText: 'Select a deadline for the project completion' } // Inline date picker (always visible) { name: 'calendarDate', label: 'Calendar', type: 'date', inline: true, view: 'date', // 'date', 'month', 'year' showWeek: true, defaultValue: 'today' } ``` **Additional Properties:** - **`format`** (String) - Date format: `'YYYY-MM-DD'`, `'mm/dd/yyyy'`, `'dd/mm/yyyy'`, `'dd-mm-yyyy'`, `'mm-dd-yyyy'` - **`dateFormat`** (String) - PrimeVue-specific format string (overrides `format`) - **`showTime`** (Boolean, default: `false`) - Include time picker - **`timeOnly`** (Boolean, default: `false`) - Show only time picker (no date) - **`hourFormat`** (String, default: `'24'`) - Hour format: `'12'` or `'24'` - **`stepHour`** (Number, default: `1`) - Hour step increment - **`stepMinute`** (Number, default: `1`) - Minute step increment - **`showSeconds`** (Boolean, default: `false`) - Show seconds in time picker - **`stepSecond`** (Number, default: `1`) - Second step increment - **`minDate`** (String|Date) - Minimum selectable date - **`maxDate`** (String|Date) - Maximum selectable date - **`defaultToToday`** (Boolean, default: `false`) - Set default to today's date - **`defaultToNow`** (Boolean, default: `false`) - Set default to current date/time - **`showButtonBar`** (Boolean, default: `true`) - Show today/clear buttons - **`todayButtonLabel`** (String, default: `'Today'`) - Today button text - **`clearButtonLabel`** (String, default: `'Clear'`) - Clear button text - **`showWeek`** (Boolean, default: `false`) - Show week numbers - **`manualInput`** (Boolean, default: `true`) - Allow manual date entry - **`yearNavigator`** (Boolean, default: `false`) - Show year dropdown - **`monthNavigator`** (Boolean, default: `false`) - Show month dropdown - **`yearRange`** (String, default: `'1900:2100'`) - Available year range - **`inline`** (Boolean, default: `false`) - Display picker inline (always visible) - **`view`** (String, default: `'date'`) - Default view: `'date'`, `'month'`, `'year'` - **`touchUI`** (Boolean, default: `false`) - Optimize for touch devices - **`onDateChange`** (Function) - Custom date change handler - **Signature:** `(dateValue: Date) => any` **Default Value Options:** ```javascript // String values defaultValue: "today"; // Set to today's date defaultValue: "now"; // Set to current date/time defaultValue: "2024-12-25"; // Specific date string // Boolean flags defaultToToday: true; // Set to today (date only) defaultToNow: true; // Set to current date/time // Date object defaultValue: new Date(); // Specific Date object ``` ### DateTime Input (`type: 'datetime'`) - LEGACY **⚠️ DEPRECATED:** Use `type: 'date'` with `showTime: true` instead. Legacy date and time picker input field. This is maintained for backward compatibility. ```javascript // LEGACY - Use date with showTime instead { name: 'appointmentTime', label: 'Appointment Time', type: 'datetime', required: true } // RECOMMENDED - Use this instead { name: 'appointmentTime', label: 'Appointment Time', type: 'date', showTime: true, required: true } ``` **Additional Properties:** - **`min`** (String) - Minimum allowed datetime - **`max`** (String) - Maximum allowed datetime - **`hourFormat`** (String, default: `'24'`) - Hour format: `'12'` or `'24'` ### File Input (`type: 'file'`) File upload input field. ```javascript { name: 'resume', label: 'Resume', type: 'file', accept: '.pdf,.doc,.docx', multiple: false } ``` **Additional Properties:** - **`accept`** (String) - File types to accept (MIME types or file extensions) - **`multiple`** (Boolean, default: `false`) - Allow multiple file selection ## Events ### `update:formData` - **Description:** Emitted when form data changes (controlled mode only) - **Payload:** Updated form data object - **Usage:** `@update:formData="handleFormDataUpdate"` ### `submit` - **Description:** Emitted when form is successfully submitted - **Payload:** Form data object - **Usage:** `@submit="handleSubmit"` ### `cancel` - **Description:** Emitted when cancel button is clicked - **Usage:** `@cancel="handleCancel"` ### `change` - **Description:** Emitted when any field value changes - **Payload:** Object with `fieldName`, `value`, and `formData` properties - **Usage:** `@change="handleFieldChange"` ## Exposed Methods The component exposes several methods that can be accessed via template refs: ```vue ``` ### `validateForm()` - **Description:** Validates the entire form and returns validation status - **Returns:** `Boolean` - `true` if valid, `false` if invalid - **Side Effect:** Updates form error state ### `getCurrentFormData()` - **Description:** Gets the current form data object - **Returns:** `Object` - Current form data ### `resetForm()` - **Description:** Resets form to initial state and clears all errors - **Returns:** `void` ### `setFieldError(fieldName, error)` - **Description:** Sets an error message for a specific field - **Parameters:** - `fieldName` (String) - The field name - `error` (String) - Error message to display ### `clearFieldError(fieldName)` - **Description:** Clears the error for a specific field - **Parameters:** - `fieldName` (String) - The field name to clear ## Usage Examples ### Basic Contact Form ```vue ``` ### AutoComplete Examples ```vue ``` ### Enhanced Date Picker Examples ```vue ``` ### Real-World Date Picker Scenarios ```vue ``` ```vue ``` ### User Registration Form ```vue ``` ### Survey Form with Custom Validation ```vue ``` ### File Upload Form ```vue ``` ## Form State Management ### Controlled Mode (External Form Data) When you provide a `formData` prop, the component operates in controlled mode: ```vue ``` ### Uncontrolled Mode (Internal Form Data) When no `formData` is provided, the component manages its own internal state: ```vue ``` ## Validation ### Built-in Validation - **Required fields** - Validates that required fields are not empty - **Email format** - Validates email format when `format: 'email'` is used - **Number ranges** - Validates min/max values for number fields ### Custom Validation Each field can have a custom validation function: ```javascript { name: 'password', label: 'Password', type: 'text', required: true, validate: (value) => { if (value && value.length < 8) { return 'Password must be at least 8 characters long' } if (value && !/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/.test(value)) { return 'Password must contain at least one lowercase letter, one uppercase letter, and one number' } return null // Valid } } ``` ### Validation Timing - **On change** - When `validateOnChange` is `true` (default) - **On submit** - Always validates on form submission - **Manual** - Using the exposed `validateForm()` method ## Responsive Layout The component uses Vuetify's grid system for responsive layouts: ```javascript { name: 'field', label: 'Field', type: 'text', cols: 12, // Full width on extra small screens sm: 12, // Full width on small screens md: 6, // Half width on medium screens lg: 4 // One-third width on large screens } ``` ## Styling The component uses Vuetify's design system with: - **Outlined variants** for consistent appearance - **Comfortable density** for optimal spacing - **Error state styling** for validation feedback - **Required field indicators** with red asterisks - **Responsive design** that adapts to screen size ## Best Practices 1. **Use meaningful field names** that reflect the data structure 2. **Provide clear labels and help text** for better user experience 3. **Implement proper validation** for data integrity 4. **Consider responsive layout** for different screen sizes 5. **Handle form submission errors** gracefully 6. **Use controlled mode** when form data needs to be managed externally 7. **Leverage custom validation** for business-specific rules 8. **Test with various field combinations** to ensure proper behavior 9. **Use appropriate field types** for better user experience 10. **Provide meaningful default values** when appropriate ## Accessibility The component includes: - **Proper form semantics** with native HTML form elements - **Label associations** for screen readers - **Error message announcements** for validation feedback - **Keyboard navigation** support throughout the form - **Focus management** for better usability - **Required field indicators** for clarity ## Browser Support Compatible with all modern browsers that support: - Vue 3 Composition API - Vuetify 3 components - ES6+ features - CSS Grid and Flexbox ## Migration from Vuetify The component has been completely migrated from Vuetify to PrimeVue. Here's what you need to know: ### ✨ What's New - **AutoComplete component** - The star feature! Users can select from suggestions OR enter completely custom values - **Enhanced Date/Time Pickers** - Multiple format support (`YYYY-MM-DD`, `mm/dd/yyyy`, etc.), smart defaults (`today`, `now`), time-only mode, inline calendars, business hour constraints, and comprehensive validation - **Better file uploads** - Drag & drop, better validation, file preview - **Improved accessibility** - Full ARIA support, better keyboard navigation - **Flexible styling** - CSS custom properties, easier theming - **Mobile-first responsive** - Better grid system, improved mobile UX ### 🔄 Migration Changes #### Layout System ```javascript // OLD (Vuetify): { cols: 12, sm: 12, md: 6, lg: 6 } // NEW (CSS Grid): Same properties, better responsive behavior { cols: 12, md: 6, lg: 4 } ``` #### Component Mapping | Vuetify | PrimeVue | Notes | | --------------- | -------------- | ------------------------ | | `v-text-field` | `InputText` | Same functionality | | `v-textarea` | `Textarea` | Added autoResize | | `v-select` | `Select` | Added filtering | | `v-checkbox` | `Checkbox` | Improved styling | | `v-radio-group` | `RadioButton` | Better layout | | `v-file-input` | `FileUpload` | Enhanced features | | `v-btn` | `Button` | Same functionality | | **New!** | `AutoComplete` | 🚀 Custom values support | | **New!** | `DatePicker` | Calendar popup | #### Event Changes ```javascript // OLD (Vuetify): @update:model-value="handleChange" // NEW (PrimeVue): @update:model-value="handleChange" // Same! // But also enhanced with better event data ``` #### Styling Changes ```css /* OLD: Vuetify classes */ .v-form { ... } .v-text-field { ... } /* NEW: Custom CSS with variables */ .dynamic-form { ... } .field-wrapper { ... } /* Theming with CSS custom properties */ :root { --text-color: #374151; --red-500: #ef4444; } ``` ### 🚀 Upgrade Benefits 1. **AutoComplete with Custom Values** - Users are no longer limited to predefined options 2. **Better Performance** - PrimeVue components are more lightweight 3. **Enhanced Accessibility** - Full WCAG compliance out of the box 4. **Improved Developer Experience** - Better TypeScript support, clearer APIs 5. **Modern Styling** - CSS Grid, custom properties, better mobile support 6. **Rich Components** - Better date pickers, file uploads, and form controls ### 📝 Quick Migration Checklist - [ ] Update imports from Vuetify to PrimeVue components - [ ] Replace `v-select` with `AutoComplete` where custom values are needed - [ ] Update CSS classes from `v-*` to custom classes - [ ] Test responsive layout (should work better!) - [ ] Enjoy the new AutoComplete functionality! 🎉 ## Dependencies - **Vue 3** with Composition API - **PrimeVue 4.4+** components (InputText, InputNumber, Textarea, Select, AutoComplete, Checkbox, RadioButton, DatePicker, FileUpload, Button, Message) - **Modern JavaScript** features (ES6+) - **CSS Grid** and **Flexbox** support ````