Skip to main content
Version: v2

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:

  1. Converts camelCase and snake_case into words.
  2. Capitalizes the first letter of each word.
  3. Handles abbreviations when generating the field label.

Parameters

  1. 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'