fieldNameToLabel
function fieldNameToLabel(fieldName: string): string;
This function generates a user-friendly label for the form field based on its fieldName
prop, eliminating the need for the developer to explicitly provide the label
prop, unless required.
Usage
import { fieldNameToLabel } from '@nish1896/rhf-mui-components/form-helpers';
This function can be extremely useful for generating human-readable validation messages for your fields, saving you the effort of manually crafting a custom sentence for each field validation. It:
- Converts camelCase and snake_case into words.
- Capitalizes the first letter of each word.
- Handles abbreviations when generating the field label.
Parameters
fieldName
: The input string for generating a meaningful label.
Example
const label1 = fieldNameToLabel('firstName'); // Output: 'First Name'
const label2 = fieldNameToLabel('email_address'); // Output: 'Email Address'
const label3 = fieldNameToLabel('IMDBRating'); // Output: 'IMDB Rating'