0

I work with linguistics software, and it's important for me to be able to normalize (usually with NFC or NFD) all text inputs from a user session. Is there a simple way to force all Material UI <TextField> components to automatically normalize the input text?

1 Answer 1

0

Here's a wrapper component I came up with, but I'd be interested to see other solutions:

import { TextField, TextFieldProps } from "@mui/material";
import { ReactElement } from "react";

export type NormalizedTextFieldProps = TextFieldProps & {
  form?: "NFC" | "NFD" | "NFKC" | "NFKD";
};

export function NormalizedTextField(props: NormalizedTextFieldProps): ReactElement {
  const { form, ...textFieldProps } = props;
  return (
    <TextField
      {...textFieldProps}
      onChange={(e) => {
        if (textFieldProps.onChange) {
          textFieldProps.onChange({
            ...e,
            target: {
              ...e.target,
              value: e.target.value.normalize(form || "NFC"),
            },
          });
        }
      }}
    />
  );
}

Note: This doesn't work with TextField prop type = "email" because the input is silently converted from Unicode to punycode on some web browsers, which seems to happen before onChange is called.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.