Home > Mobile >  Capitalize the first word in input -React js typescript
Capitalize the first word in input -React js typescript

Time:01-07

I don't know why my auto capitalize html thing doesn't work. I need help to resolve the issue.

type InputProp ={
autoCapitalize?: 'none' | 'sentences' | 'words' | 'characters';} 

then I'm passing this prop to the <Input> , next I'm going to the input in the app e.g

<Input
                    inputType={'text'}
                    inputName={'forename'}
                    placeholder="Forename"
                    onChange={something something}
                    autoCapitalize ="sentences"
                  />

Everything is working except autoCapitalize .

CodePudding user response:

Thank you all for your answers. They were very helpful. Actually the answer was easier than expected. I just added style={{textTransform:"capitalize"}} to my input and that's it.

CodePudding user response:

//  if you need to capitalize first and lowercase rest
const capitalizeFirstLowercaseRest = str => {
  return str.charAt(0).toUpperCase()   str.slice(1).toLowerCase();
};

export default function App() {
  //  if you only need to capitalize first letter
  const capitalizeFirst = str => {
    return str.charAt(0).toUpperCase()   str.slice(1);
  };

  const str = 'alice';

  console.log(capitalizeFirst(str)); //  "Alice"
  return (
    <div>
      <h2>{capitalizeFirst(str)}</h2>
    </div>
  );
}
  • Related