Categories
React Answers

How to select part of text in a Textfield on onFocus event with Material UI in React?

Sometimes, we want to select part of text in a Textfield on onFocus event with Material UI in React.

In this article, we’ll look at how to select part of text in a Textfield on onFocus event with Material UI in React.

How to select part of text in a Textfield on onFocus event with Material UI in React?

To select part of text in a Textfield on onFocus event with Material UI in React, we can set the onFocus prop to a function that does the text selection.

For instance, we write:

import React from "react";
import TextField from "@material-ui/core/TextField";

export default function App() {
  const handleFocus = (event) => {
    event.preventDefault();
    const { target } = event;
    const extensionStarts = target.value.lastIndexOf(".");
    target.focus();
    target.setSelectionRange(0, extensionStarts);
  };
  return (
    <div>
      <TextField value="myfile.doc" onFocus={handleFocus} />
    </div>
  );
}

We set the onFocus prop to handleFocus.

The function calls preventDefault to stop the default focus behavior.

Then we call target.focus to focus on the text input.

Finally, we call setSelectionRange with the start and end index of the input value to highlight.

The character at the end index is excluded from the selection.

Conclusion

To select part of text in a Textfield on onFocus event with Material UI in React, we can set the onFocus prop to a function that does the text selection.

Categories
React Answers

How to position React Material UI Popper in the bottom right corner of the browser with JavaScript?

Sometimes, we want to position React Material UI Popper in the bottom right corner of the browser with JavaScript.

In this article, we’ll look at how to position React Material UI Popper in the bottom right corner of the browser with JavaScript.

How to position React Material UI Popper in the bottom right corner of the browser with JavaScript?

To position React Material UI Popper in the bottom right corner of the browser with JavaScript, we can set the popper’s modifiers prop.

For instance, we write:

import React from "react";
import Popper from "@material-ui/core/Popper";
import Button from "@material-ui/core/Button";

export default function App() {
  const [anchorEl, setAnchorEl] = React.useState();

  return (
    <div>
      <Button onClick={(e) => setAnchorEl(e.currentTarget)}>open</Button>
      <Popper
        className="popper-root"
        open={!!anchorEl}
        anchorEl={anchorEl}
        placement="bottom-end"
        modifiers={{
          offset: {
            enabled: true,
            offset: "0, 30"
          }
        }}
      >
        <div>hello</div>
      </Popper>
    </div>
  );
}

We set the modifiers prop to an object that has the offset property.

The property is set to an object that has enabled set to true to enable offset of the position.

Then we set offset to '0, 30' to move the popper down by 30px.

Conclusion

To position React Material UI Popper in the bottom right corner of the browser with JavaScript, we can set the popper’s modifiers prop.

Categories
React Answers

How to change the outline for OutlinedInput with React Material UI?

Sometimes, we want to change the outline for OutlinedInput with React Material UI.

In this article, we’ll look at how to change the outline for OutlinedInput with React Material UI.

How to change the outline for OutlinedInput with React Material UI?

To change the outline for OutlinedInput with React Material UI, we can use the makeStyles function.

For instance, we write:

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

const useStyles = makeStyles(() => ({
  root: {
    "& $notchedOutline": {
      borderWidth: 0
    },
    "&:hover $notchedOutline": {
      borderWidth: 0
    },
    "&$focused $notchedOutline": {
      borderWidth: 0
    }
  },
  focused: {},
  notchedOutline: {}
}));

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

  return (
    <div>
      <OutlinedInput
        disableUnderline={true}
        notched={true}
        classes={classes}
        value={1}
      />
    </div>
  );
}

We call makeStyles with a callback to return the border styles.

We set all the borders to have width 0.

Then we call useStyles in App to return the classes.

Next, we set disableUnderline to true to remove the underline.

And set the classes prop to classes to remove the remaining borders.

Conclusion

To change the outline for OutlinedInput with React Material UI, we can use the makeStyles function.

Categories
React Answers

How to hide browser autocomplete with React Material UI Autocomplete and TextField?

Sometimes, we want to hide browser autocomplete with React Material UI Autocomplete and TextField.

In this article, we’ll look at how to hide browser autocomplete with React Material UI Autocomplete and TextField.

How to hide browser autocomplete with React Material UI Autocomplete and TextField?

To hide browser autocomplete with React Material UI Autocomplete and TextField, we can set inputProps.autocomplete to 'off'.

For instance, we write:

import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";

const fruits = [
  { name: "Apple", value: "apple" },
  { name: "Orange", value: "orange" },
  { name: "Grape", value: "grape" }
];

export default function App() {
  return (
    <div>
      <Autocomplete
        options={fruits}
        getOptionLabel={(option) => option.name}
        getOptionValue={(option) => option.value}
        renderInput={(params) => {
          const inputProps = params.inputProps;
          inputProps.autoComplete = "off";
          return (
            <TextField
              {...params}
              inputProps={inputProps}
              label="Combo box"
              variant="outlined"
              onBlur={(event) => console.log(event.target.value)}
              fullWidth
            />
          );
        }}
      />
    </div>
  );
}

We set inputProps.autoComplete to 'off' in the renderInput function to disable browser autocomplete in the text field.

Conclusion

To hide browser autocomplete with React Material UI Autocomplete and TextField, we can set inputProps.autocomplete to 'off'.

Categories
React Answers

How to change color of CircularProgress with React Material UI?

Sometimes, we want to change color and position of CircularProgress with React Material UI.

In this article, we’ll look at how to change color and position of CircularProgress with React Material UI.

How to change color and position of CircularProgress with React Material UI?

To change color and position of CircularProgress with React Material UI, we can set the color CSS property.

For instance, we write:

import React from "react";
import CircularProgress from "@material-ui/core/CircularProgress";

export default function App() {
  return (
    <div>
      <CircularProgress style={{ color: "yellow" }} />
    </div>
  );
}

We add the CircularProgress component with the style prop set to an object with the color property set to 'yellow'.

Therefore, the circular progress ring should be yellow.

Conclusion

To change color and position of CircularProgress with React Material UI, we can set the color CSS property.