-
Provide Clear Instructions:
Be explicit about the format and type of input expected. For instance:
<mat-hint>Enter values separated by commas, e.g., 10, 20, 30</mat-hint>
-
Specify the Data Type:
If the input should be numbers, mention it to prevent users from entering unintended data types.
<mat-hint>Enter numeric values only, separated by commas (e.g., 10, 20, 30)</mat-hint>
-
Use Placeholders:
In addition to mat-hint, you can use the placeholder attribute to show an example inside the input field.
<input matInput formControlName="values" placeholder="10, 20, 30" />
-
Implement Input Validation:
Enhance user experience by adding validation that checks the input format and values. You can provide real-time feedback if the input doesn't match the expected pattern.
this.fieldForm = this.formBuilder.group({
values: ['', [Validators.pattern(/^(\s*\d+\s*(,\s*\d+\s*)*)?$/)]],
});
<mat-error *ngIf="fieldForm.get('values').hasError('pattern')">
Please enter a comma-separated list of numbers.
</mat-error>
-
Consider Using Chips for Multiple Values:
For improved usability, especially when dealing with multiple values, you might use mat-chip-list and mat-chip-input components.
<mat-chip-list #chipList>
<mat-chip *ngFor="let value of values" [selectable]="selectable" [removable]="removable" (removed)="remove(value)">
{{value}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="Add a value"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
(matChipInputTokenEnd)="add($event)"
/>
</mat-chip-list>
<mat-hint>Add values by typing and pressing enter; e.g., 10</mat-hint>
This approach enhances user interaction and ensures that the values are captured accurately.
-
Include Tooltip for Additional Help:
If more explanation is needed, a tooltip can provide extra guidance without cluttering the interface.
<mat-form-field class="w-100">
<mat-label>Values</mat-label>
<input matInput formControlName="values" />
<button mat-icon-button matTooltip="Enter comma-separated numbers like 10, 20, 30">
<mat-icon>help_outline</mat-icon>
</button>
<mat-hint>e.g., 10, 20, 30</mat-hint>
</mat-form-field>