Custom Material-UI TextField with mandatory input

Within the Component, you have the option to specify whether a field should be mandatory or not. However, I am interested in replacing the symbol * with * mandatory field.

Is it possible to customize this in any way?

Answer №1

I couldn't locate any information about it in their API, but a workaround is to utilize the ::after pseudo-element on the asterisk's <span> element:

import React from "react";
import TextField from "@material-ui/core/TextField";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  root: {
    "& .MuiTextField-root": {
      margin: theme.spacing(1),
      width: 200,

      "& .MuiFormLabel-asterisk.MuiInputLabel-asterisk": { // these are the classes used from material-ui library for the asterisk element
        "&::after": {
          content: '"mandatory field"' // add your text here
        }
      }
    }
  }
}));

export default function FormPropsTextFields() {
  const classes = useStyles();

  return (
    <form className={classes.root} noValidate autoComplete="off">
      <div>
        <TextField
          defaultValue="My Name"
          required
          id="standard-required"
          label="Name"
        />
      </div>
    </form>
  );
}

Here is a link to a working example: example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

No declaration file was located for the module '@handsontable/react' despite the presence of a 'd.ts' file

Embarking on a fresh project using vite+react+ts+swc by executing the command below as per the vite documentation. npm create vite@latest -- --template react-swc-ts Additionally, I integrated the handsontable library into my setup with the following comm ...

Leveraging react-query with next-mdx-remote MDX Components?

I have been using next-mdx-remote in my next.js project. One of the components I've passed to it makes API calls using axios, which has been working well. However, I now want to switch to using react-query. When implementing this change, I encountered ...

Dealing with CORS issues in an Express Node.JS backend, a React frontend, and AWS infrastructure

I've developed a React frontend where users can add and view recipes, along with text and images. The backend is supported by an Express Node.JS application that interfaces with a DynamoDB database. This project is hosted on AWS via the Serverless Fra ...

Managing form submissions using Material UI and Next.js

Can someone provide insights on using Material UI with Next Js? I am experimenting with a Login template from Material UI, but I am facing an issue where query params are added to the URL upon Submit. For example: localhost:3000/auth/login changes to: ...

A guide on extracting content from a PDF file with JavaScript

Hey there, I'm experimenting with various methods to extract content from a PDF file but so far nothing seems to be working for me. Even when I try using pdf.js, it shows an error and I can't figure out why. This is the approach I've been tr ...

Selecting multiple options with a default value in Material UI Autocomplete

I am facing an issue with my Material UI Autocomplete component that allows for multiple selection and requires default values. The problem lies with the checkbox selection. Even though the names appear in the text field, they are not selected as values. ...

Sort columns in a MUI datatable

I am facing an issue with sorting in a column that represents an object. Although I can display the desired value, the sorting functionality does not seem to work for that particular column. Here is an example to provide better clarity: const [data, set ...

Customizing the appearance of checkboxes in React Material-Table filtering feature

Is there a way to modify the style of checkboxes in filtering? For instance, if I wish to adjust the dimensions of the checkbox that appears for the Birth Place field, what steps should I take? https://i.stack.imgur.com/pZLqQ.png ...

What is the correct method for retrieving a specific child element in React?

In my React project, I have created a component that consists of various subcomponents: import React, { FunctionComponent } from 'react'; import { FormControl, FormGroup, FormGroupProps, FormLabel, FormText, FormTextProps } from 'react-boots ...

Implementing Material UI styles within a React component

Discover a unique way of incorporating Material UI styles outside of a React component. import React from 'react'; import { makeStyles } from '@material-ui/styles'; import Button from '@material-ui/core/Button'; const useSty ...

Images in Svg format do not animate properly when placed within an <img> tag in a React project

I have an issue where I am trying to load an SVG in an img tag as a loader. The image is displaying but the animation is not working. Strangely, when I put it in index.html, it works as expected. Since I am using JSX, there might be something I am missin ...

I've noticed that Event.stopPropogation doesn't appear to function properly on Material UI containers

I am encountering an issue with a page that contains the following code snippet: <div> <Link key={row.id} onUpdateTask={this.props.onUpdateTask} to= {`/Supervisor/Task/${row.id}`}> <Paper className={classes.root} key={row.id}> ...

Having difficulty retrieving API data with getServerSideProps

I am attempting to retrieve a user's name from the JSON placeholder API and display it, but for some reason, it is returning as undefined. I am not sure what the issue could be. Any assistance would be greatly appreciated! function DisplayUser({ data ...

Keyboard control of Material UI Checkbox

As we work on developing a web application using react and material ui, accessibility for persons with disabilities is a key consideration. This means ensuring that the web application is operable through keyboard navigation. It's important that user ...

Is it necessary to write Higher Order Component in React as a function?

As I dive into learning React, the concept of Higher Order Components (HOCs) becomes clearer. Take for instance the following example extracted from React's official documentation: function withSubscription(WrappedComponent, selectData) { // ...a ...

In order to activate the VScode Tailwind CSS Intellisense plugin, I have discovered that I need to insert a space before

I recently followed the installation guide for using Tailwind with Next.js Here is a snippet from my tailwind.config.js file: /** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./app/**/*.{js,ts,jsx,tsx}", ...

Create a three-dimensional tree array in Typescript/Javascript by transforming a flat array

Received data is structured as follows: const worldMap = [ { "name": "Germany", "parentId": null, "type": "Country", "value": "country:unique:key:1234", "id&qu ...

Is it achievable to animate the offset with React Native Animated?

I am attempting to develop a dynamic drag and drop functionality, inspired by this example: My goal is to modify it so that when the user initiates the touch, the object moves upwards to prevent it from being obscured by their finger. I envision this move ...

In the React js and emotion framework, fonts are only loaded in production mode after the page has been re

I have set up emotion's Global component to globally apply a font to my app: export const GlobalStyle: FC<{}> = () => ( <Global styles={css` @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;50 ...

Comparing E-commerce: Laravel's Full MVC Approach vs Next.JS for Dynamic Views

Seeking guidance. Who here is knowledgeable about Next.js (or its rival Nuxt.JS)? In the near future, I have a challenging task of developing a fully customized E-commerce website. This platform needs to be efficient and particularly well optimized for ...