@ng-zi/extensions-form-field
v0.0.1
Published
Angular Material Extensions for form-field
Downloads
3
Readme
MtxFormField Component Overview
The MtxFormField
component is a versatile and highly configurable form field component built with Angular and Angular Material. It allows developers to create input fields with enhanced features such as icons, buttons, character counts, and validation error messages. This component is designed to simplify the creation of form fields while providing extensive customization options.
Features
| Feature | Description | | ---------------------- | --------------------------------------------------------------------------- | | Input Types | Supports different input types such as text, password, email, etc. | | TextArea Support | Can be configured as a textarea. | | Icons | Allows adding icons at the start or end of the input field. | | Buttons | Supports icon buttons within the form field. | | Character Count | Displays the current character count and the maximum allowed characters. | | Error Messages | Customizable error messages based on form validation. | | Hints and Placeholders | Provides hint and placeholder text for better user guidance. | | Clear Button | Includes a clear button to reset the input field. | | Disabled State | Supports enabling and disabling of the form field. | | Appearance Styles | Configurable appearance styles such as fill, outline, standard, and legacy. |
Installation
- Import the
MtxFormField
component and its dependencies in your module:
import { Component } from '@angular/core';
import { MtxFormFieldModule } from '@ng-zi/extensions/form-field';
@Component({
selector: 'from-field-example',
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
standalone: true,
imports: [MtxFormFieldModule],
})
export class AppComponent {}
Usage
To use the MtxFormField
component in your template, follow these steps:
Add the component to your template:
<mtx-form-field [formFieldConfig]="formFieldConfig" [control]="formControl" (iconBtnClick)="onIconBtnClick($event)" (clearClick)="onClearClick()" ></mtx-form-field>
Configure the component in your TypeScript file:
import { Component } from '@angular/core'; import { FormControl } from '@angular/forms'; import { MtxFormFieldConfig } from './path-to-your-component/form-field.config'; @Component({ selector: 'app-your-component', templateUrl: './your-component.html', styleUrls: ['./your-component.scss'], standalone: true, imports: [MtxFormFieldModule], }) export class YourComponent { formControl = new FormControl(''); formFieldConfig: MtxFormFieldConfig = { label: 'Your Label', placeholder: 'Enter text', type: 'text', icon: 'search', iconPosition: 'start', clearValue: true, maxlength: 100, showCharacterCount: true, hint: 'Hint text', errorMessages: { required: 'This field is required', }, }; onIconBtnClick(event: Event) { // Handle icon button click } onClearClick() { // Handle clear button click } }
Configuration Options
The MtxFormFieldConfig
interface provides the following configuration options:
label (
string
):- The label of the form field.
- Example:
'Name'
hintLabel (
string
):- The hint label displayed below the form field.
- Example:
'Enter your full name'
placeholder (
string
):- The placeholder text for the input.
- Example:
'John Doe'
control (
FormControl
):- The form control associated with the form field.
type (
string
):- The type of input.
- Possible values:
'text'
,'password'
,'email'
, etc. - Default:
'text'
isTextArea (
boolean
):- Determines if the form field is a textarea.
- Default:
false
hint (
string
):- The hint text displayed below the input.
- Example:
'This is a hint text'
hintAlign (
'start' | 'end'
):- The alignment of the hint text.
- Default:
'end'
errorMessages (
{ [key: string]: string }
):- An object containing error messages for validation errors.
- Example:
{ required: 'This field is required', minlength: 'Minimum length is 5' }
appearance (
MatFormFieldAppearance
):- The appearance style of the form field.
- Possible values:
'fill'
,'outline'
- Default:
'fill'
icon (
string
):- The icon to be displayed in the form field.
- Example:
'search'
iconPosition (
'start' | 'end'
):- The position of the icon.
- Default:
'start'
value (
string
):- The initial value of the form field.
iconBtn (
string
):- The icon for the button.
- Example:
'add'
iconBtnAriaLabel (
string
):- The aria-label for the button.
- Example:
'Add item'
iconBtnAriaPressed (
any
):- The aria-pressed state for the button.
- Example:
true
orfalse
disabled (
boolean
):- If true, the form field will be disabled.
- Default:
false
clearValue (
boolean
):- If true, a clear button will be displayed to reset the form field.
- Default:
false
maxlength (
number
):- The maximum length of the input.
- Example:
100
showCharacterCount (
boolean
):- If true, the character count will be displayed.
- Default:
false
hidePlaceholderOnFocus (
boolean
):- If true, Placeholder will get hide.
- Default:
false
hidePlaceholderWithDynamicWidth (
boolean
):- If true, Placeholder will hide with dynamic Input width.
- Default:
false
dynamicWidth (
boolean
):- If true, It Will provide dynamic Input width.
- Default:
false
Default Configuration
The DEFULT_MTX_FORM_FIELD_CONFIG
object provides the default configuration for the form field:
export const DEFULT_MTX_FORM_FIELD_CONFIG: MtxFormFieldConfig = {
label: '',
hintLabel: '',
placeholder: '',
type: 'text',
isTextArea: false,
hint: '',
hintAlign: 'end',
errorMessages: {},
appearance: 'fill',
icon: '',
iconPosition: 'start',
value: '',
iconBtn: '',
iconBtnAriaLabel: '',
iconBtnAriaPressed: '',
disabled: false,
clearValue: false,
maxlength: undefined,
showCharacterCount: false,
hidePlaceholderOnFocus: false,
hidePlaceholderWithDynamicWidth: false,
dynamicWidth: false,
};
Example
Here is an example of how to use the MtxFormField
component with a configuration object:
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MtxFormFieldConfig } from '@ng-zi/extensions/form-field';
@Component({
selector: 'app-example',
template: `
<mtx-form-field
[formFieldConfig]="formFieldConfig"
[control]="formControl"
(iconBtnClick)="onIconBtnClick($event)"
(clearClick)="onClearClick()"
></mtx-form-field>
`,
styleUrls: ['./example.component.scss'],
})
export class ExampleComponent {
formControl = new FormControl('');
formFieldConfig: MtxFormFieldConfig = {
label: 'Your Label',
placeholder: 'Enter text',
type: 'text',
icon: 'search',
iconPosition: 'start',
clearValue: true,
maxlength: 100,
showCharacterCount: true,
hint: 'Hint text',
errorMessages: {
required: 'This field is required',
},
};
onIconBtnClick(event: Event) {
// Handle icon button click
}
onClearClick() {
// Handle clear button click
}
}