When the textfield mui is set to shrink, an additional space appears in the label when using a font size of 12px

Struggling to customize the appearance of the textField component, particularly with the label when it is minimized:

import * as React from "react";
import TextField from "@mui/material/TextField";
import { createTheme } from "@mui/material/styles";
import createStyled from "@mui/system/createStyled";
import type { TextFieldProps } from "@mui/material/TextField";

const defaultTheme = createTheme({});
const styled = createStyled({ defaultTheme });

const CustomTextField = styled(TextField)<TextFieldProps>(
  ({ theme, size }) => ({
    "& .MuiOutlinedInput-root": {
      height: size === "small" ? theme.spacing(5) : theme.spacing(6)
    },
    "& .MuiInputLabel-root": {
      transform: "translate(14px, 8px) scale(1)"
    },
   "& .MuiInputLabel-shrink": {
      transform: "translate(12px, -9px) scale(0.75)",
      fontSize: "12px",
      fontWeight: 500,
      letterSpacing: 0.6,
      lineHeight: '20px',
      "&.MuiInputLabel-sizeSmall": {
        transform: "translate(12px, -9px) scale(0.75)"
      }
    }
  })
);

export default function BasicTextFields() {
  return (
    <CustomTextField
      id="outlined-basic"
      label="long"
      variant="outlined"
      size="medium"
      InputLabelProps={{ shrink: true }}
    />
  );
}

The issue causing concern is the additional space between the border and the text: https://i.stack.imgur.com/ALeKR.png

Answer №1

Here's a demonstration featuring the Mui filedSet and legend in TextFiled component.

import * as React from "react";
import TextField from "@mui/material/TextField";
import { createTheme } from "@mui/material/styles";
import createStyled from "@mui/system/createStyled";
import type { TextFieldProps } from "@mui/material/TextField";

const defaultTheme = createTheme({});
const styled = createStyled({ defaultTheme });

const CustomTextField = styled(TextField)<TextFieldProps>(
  ({ theme, size }) => ({
    "& .MuiOutlinedInput-root": {
      height: size === "small" ? theme.spacing(5) : theme.spacing(6),
      '& fieldset legend span':{
        opacity:1,
        backgroundColor:'blue',
        marginRight:10,
      }
    },
    "& .MuiInputLabel-root": {
      transform: "translate(14px, 8px) scale(1)",
      backgroundColor:'red'
    },
    "& .MuiInputLabel-shrink": {
      transform: "translate(12px, -9px) scale(0.75)",
      "&.MuiInputLabel-sizeSmall": {
        transform: "translate(12px, -9px) scale(0.75)"
      }
    }
  })
);

export default function BasicTextFields() {
  return (
    <CustomTextField
      id="outlined-basic"
      label="long"
      variant="outlined"
      size="medium"
      InputLabelProps={{ shrink: true }}
    />
  );
}

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

Make the current tab the active tab with the help of AngularJS

I have a set of five tabs that are functioning correctly. However, when I refresh the page, the active tab reverts back to its default state instead of staying in its current active state. Can anyone help me identify where I may be making a mistake? Here ...

Creating a React component that manages its own UI state without any external

I'm currently working on creating a select input component using React. The select itself should be a simple UI component without much functionality, but it does need to maintain its own state to determine whether or not to display the options list. ...

The reason behind firebase-realtime-db getting stuck in an endless loop when updating

Having a simple code to update my firebase realtime database, I encountered an issue with the update() function causing an infinite loop at the line: return update(listsRef, updates): const HandleEditFormSubmit = (event) => { event.preventDefault( ...

How can Array.map() be combined with D3 selection?

Is there a way to calculate the maximum length of text elements in an SVG selection similar to using Array.map()? Currently, I am retrieving the maximum length of a selection of SVG <text/> elements by utilizing .selectAll(...)[0].map(...), but it fe ...

Is it possible to programmatically include a getter method to a class in JavaScript or TypeScript?

My current focus is on TypeScript and I'm exploring the decorators functionality. I would greatly appreciate some guidance or expert knowledge on JavaScript capabilities in this area. I am curious about dynamically adding a getter method to a Prototy ...

What is the best way to convert exponential values to decimals when parsing JSON data?

var value = '{"total":2.47E-7}' var result = JSON.parse(value); Looking to convert an exponential value into decimal using JavaScript - any suggestions? ...

Is it possible to invoke a helper function in Node.js from within a callback?

I recently started programming with node.js and I am encountering an error that I can't quite figure out. The function appears to be correctly set up, and I don't believe there are any asynchronous issues because of the self variable I have in pl ...

Design your own custom up and down arrow icons or buttons using CSS styling techniques

Is there a way to create "up and down" control buttons using only CSS and no background image? I attempted to add CSS for arrows in "li.className:after or li.className:before", but it caused the main boxes to move. Check out the Fiddle link to see the is ...

Throttle individual users, exclude frontend server and getServerSideProps from throttling

Currently, I am working on an application using NestJs where I've implemented the throttle module to prevent abusive requests. One aspect that has left me puzzled is whether implementing a block for abusive requests (e.g., exceeding 20 requests per m ...

AngularJS: accessing siblings by using the ng-click directive

I am currently working on a list (<ul>), which is being used in multiple instances within different ng-repeat iterations on the same page. The initial list items are generated through ng-repeat, with the second to last item containing a span. When t ...

How can ListSubheader in Material-UI be customized to match the style of iOS UITableView?

Here is the current appearance of my ListSubheader. https://i.stack.imgur.com/HAzTA.png Currently, it is just aligned to the left. However, the default header view in UITableView on iOS looks like this: https://i.stack.imgur.com/F9Amv.png It features ...

Issues persist with $.getJSON

I apologize if this question has been addressed previously, but I couldn't find the answer I was seeking. My issue involves using $.getJSON to send variables to a PHP file. Although the file returns success as true, it always triggers the .fail functi ...

Refusing to cease downloading music on the local server through the embedded audio element

I was trying to setup background music on my website, but when I test it localhost, the music download instead of playing. However, when I tested with the full path, it played as expected. How can I prevent the download from happening in localhost? Here i ...

Generating dynamic forms using JSON Schema in Angular 8 can greatly streamline the form creation process

Struggling with generating dynamic forms from JSON Schema in Angular 8, I stumbled upon a couple of libraries. One seemed outdated with its last commit around 2 years ago, while the other appeared to be a more recent fork of it for Angular 8. The first li ...

Setting up your React Environment: A Step-by-Step Guide

I'm currently experiencing difficulties setting up my react environment. After executing the command npm create-react-app my-app, I encountered errors. The error messages are as follows: npm WARN npm npm does not support Node.js v20.12.2 npm WARN npm ...

Trigger action following a certain time frame

Currently, I am delving into the world of React and Redux for the very first time. My app involves utilizing a third-party API to retrieve data seamlessly, which is working out great. However, like many secure APIs, I must acquire a short-lived token to ...

The functionality of JSON.stringify involves transforming colons located within strings into their corresponding unicode characters

There is a javascript string object in my code that looks like this: time : "YYYY-MM-DDT00:00:00.000Z@YYYY-MM-DDT23:59:59.999Z" When I try to convert the object to a string using JSON.stringify, I end up with the following string: "time=YYY ...

What could be the reason for receiving the error "client.getVersion is not a function" while using Braintree (Braintree-Web)?

When attempting to render the hostedFields from Braintree, I encounter an issue. After creating a key and obtaining my token, I pass it to my function to create the hostedFields. However, I am faced with an error message stating client.getVersion is not ...

TypeScript encounters a problem when attempting to concatenate arrays with different signature overloads

I encountered the following error message: There is an issue with overloading. Overload 1 of 2, '(...items: ConcatArray<{ title: string; width: number; dataIndex: string; render: (level: string) => Element; }>[]): { title: string; width: nu ...

An introduction to using react-transition-group for creating exiting animations in React applications

I am currently utilizing react-transition-group to generate a modal that smoothly appears on the screen. const AnimatedModal: SFC<AnimatedModalProps> = (props: AnimatedModalProps) => ( <CSSTransition in={props.showWindow} unmountOnExit key={1 ...