Having issues with RTL on MuiTextField when using special characters

Currently, I am working on a project where Mui is being used. I have successfully applied RTL according to the Mui guide. However, I am encountering a frustrating problem where special characters in MuiTextField are aligning to the left instead of the right.

For example, when entering secretpassword123! into a TextField, the '!' character shifts to the left.

See example here

      <TextField
        error={Boolean(formik.touched.password && formik.errors.password)}
        fullWidth
        helperText={formik.touched.password && formik.errors.password}
        label={t("password")}
        margin="dense"
        name="password"
        onBlur={formik.handleBlur && handlePasswordFieldBlur}
        onFocus={handlePasswordFieldFocus}
        onChange={formik.handleChange}
        type={isPasswordVisible ? "text" : "password"}
        value={formik.values.password}
        InputProps={{
          endAdornment: (
            <InputAdornment position="end">
              <IconButton onClick={handleVisibilityIconClick}>
                {isPasswordVisible ? <Visibility /> : <VisibilityOff />}
              </IconButton>
            </InputAdornment>
          ),
        }}
      />

I have come across many others facing the same issue, but their solutions did not work for my specific situation.

Answer №1

When working with right-to-left (RTL) text, this behavior is expected. A similar example can be seen when typing a password on Google's IL website https://i.stack.imgur.com/VU3pb.png. It's not a flaw but rather a feature that RTL users are accustomed to when dealing with Latin text input in various apps and websites.

To customize the direction of just one TextField according to the official MUI documentation, you can use the following code snippet:

import { styled } from '@mui/material';

const LtrInput = styled("input")`
  /* @noflip */
  direction: ltr;
`;

<TextField
  InputProps={{ inputComponent: LtrInput }}
  label={"Test"}
  sx={{ "& .MuiInputBase-input": { textAlign: "end" } }}
/>

Answer №2

after following the advice from @Sam A, I have made changes to the TextField component

    dir: "ltr",
    sx: { "& .MuiInputBase-input": { textAlign: "end" } }

this adjustment successfully resolved the issue :)

FullCode

      <TextField
        error={Boolean(formik.touched.password && formik.errors.password)}
        fullWidth
        helperText={formik.touched.password && formik.errors.password}
        label={t("password")}
        margin="dense"
        name="password"
        onBlur={formik.handleBlur && handlePasswordFieldBlur}
        onFocus={handlePasswordFieldFocus}
        onChange={formik.handleChange}
        type={isPasswordVisible ? "text" : "password"}
        value={formik.values.password}
        InputProps={{
          startAdornment: (
            <InputAdornment position="start">
              <IconButton onClick={handleVisibilityIconClick}>
                {isPasswordVisible ? <Visibility /> : <VisibilityOff />}
              </IconButton>
            </InputAdornment>
          ),
          dir: "ltr",
          sx: { "& .MuiInputBase-input": { textAlign: "end" } },
        }}
      />

result

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

Creating .html pages for dynamic routes in a Next.js application

Having a Next.js app (version 13.4.5) with a dynamic route /blog/[slug], I am looking to export the app as a static website. The directory structure is as follows: /app /blog /[slug] page.jsx page.jsx layout.jsx globals.css ... The n ...

What is the reason behind React deciding to skip the final question, even though it has been accurately saved to the

A simple web application was developed to calculate risk scores by asking five questions, with additional points awarded if a checkbox is checked. Despite extensive hours spent debugging, a persistent bug remains: the final question does not appear on the ...

Error encountered in Docker: npm could not be found due to an ENOENT error, indicating that the file or directory '/package.json' does not exist. This issue occurred in the docker-entrypoint.sh script at line 38

I am currently working with Docker for the first time and have a Dockerfile set up for a React app. After building the container, I am running tests but encountering some issues. I'm following a course to learn more about Docker, so I'm still get ...

Encountering an application error - despite working perfectly during development, I am now receiving an error message post deployment on Heroku

I encountered an issue while attempting to deploy my application to Heroku. The error message I received was: An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do ...

Is it possible to utilize Mui Components independently of the Mui theme?

I am currently working on a project where I have created my own Theme using @emotion/styled. I was curious if it is possible to use Mui components such as Data-Grid or Material-React-Table without having to define the Mui Theme using createTheme. However ...

Issue with :visited pseudo-class not functioning as intended

My code includes all four basic pseudo-classes, and they all seem to be working fine except for the :visited pseudo-class. The color property doesn't function properly, and the background property appears to be inactive. I'm not sure what the iss ...

Enclose an <img> tag within a <span> element and use jQuery to assign a class to the <span>

I am trying to wrap a specific <img> tag with a <span> element and assign a class to the <span>. Below is the relevant code snippet: <img id="wrapped-image" alt="" src=""> Despite my efforts, the following code does not seem to w ...

Fix for fixed scrolling in the navigation bar

Having a website that receives traffic from different countries, including Portugal and other non-English speaking places, I decided to add a translation option. However, I encountered an issue with Google's translate feature which displays a banner a ...

I'm experiencing difficulty in scrolling on my Nextjs web application

Currently, I am facing an issue with my portfolio webpage which is divided into 3 main components - Hero, About, and Portfolio. The layout structure is as follows: export default function RootLayout({ children, }: { children: React.ReactNode }) { ret ...

Adding an element to an array using useState is successful, but the map method fails to render the newly added elements

I've encountered an issue with updating my array of objects using useState. Although I can see that the array is being successfully updated in the console with the new elements added, these new elements are not displaying when I try to map through the ...

Encountering errors while trying to execute "npm run build" in React Hook

Currently, I am using NextJS 14 for my project. The issue arises in the python-course/page.tsx file when I run 'npm run build' command. I encounter an error on this page. Here is a snippet of my package.json: { "name": "frontend", "version": ...

Using custom hooks in JSX triggers a TypeScript error when the returned component is accessed

i just created a custom hook // useDropdown.ts function useDropdown(defaultState: number, options: number[]) { const [state, setState] = useState(defaultState); function Dropdown({ name }: { name: string }) { return ( <> <sel ...

Encountering error while loading NextJS in development mode (_devPagesManifest.json fetch and 'includes' of undefined)

Lately, I've been encountering some issues while working on my NextJS app. Initially, when I run npm run dev, everything seems to work fine. However, after a few hot reloads or manual page refreshes, the app starts loading indefinitely and becomes un ...

Saving table sorting in Redux with Ant Design Table

I am currently working with Antd Version 4.2.2 in my ReactJS project. Specifically, I am utilizing the Ant Design < Table /> component. My goal is to save the sorting order that is applied to the columns into Redux state. Here is my current approa ...

Guide on integrating Snipcart with React?

I am trying to implement Snipcart on a single product page using React, similar to the example here. However, with the code provided below, clicking on the button (class="snipcart-add-item") does not trigger any action. What am I missing in terms of impor ...

`Unable to activate label function in HTML`

As I dabble around with HTML and CSS, I've been experimenting with creating a custom file selection button. I came across a method on the internet that involves hiding the original button and superimposing a new one over it using the following code: ...

How to customize the appearance of radio buttons in React when they are selected

Two custom radio button components are hidden, and when the label is clicked, the radio button is checked. I've added a feature where a styling effect appears on the label when the radio button is checked (border with padding and background). The prob ...

Customizing font size in React with Material UI: A comprehensive guide on adjusting the font size of the Select component

I'm currently working on a web application that utilizes React along with Material UI. My goal is to adjust the font size of the Select component. I've attempted to achieve this by utilizing the MenuProps property, as shown in the following code ...

React TSX file not recognizing JSON data stored in an HTML data attribute

I am having some trouble with implementing the password toggle component from the Preline UI. Here is how the component looks: "use client" import React, { ChangeEvent, MouseEventHandler, useEffect } from "react"; export default functi ...

The componentDidMount function is not functioning properly when using a custom _document.js in NextJS

Currently, I am in the process of developing a web app prototype and decided to utilize NextJS to enhance my skills. However, I have realized that my approach differs from the traditional method. I initially began with the Next + Material-UI example availa ...