最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Material-ui Autocomplete: Add a value to startAdornment - Stack Overflow

programmeradmin5浏览0评论

I have autoplete with multiple selection permission.

=/src/App.js

In the example I have 3 options for cities. How can I manually add automatic added value in TextField when something is selected?

In other words here:

 renderInput={(params) => {
        console.log(params);
        return (
          <TextField
            {...params}
            variant="outlined"
            label="Cities"
            placeholder="Enter cities"
            autoComplete="off"
            InputProps={{
              ...params.InputProps,
              endAdornment: (
                <React.Fragment>
                  {params.InputProps.endAdornment}
                </React.Fragment>
              )
            }}
          />
        );
  }}

I want to be able to add to params.InputProps.startAdornment a value before rendering the textfield.

as every selected object seems to be very plex object, how I can do this manually(It is too plicated to push())? Any ideas how I can add object like this:

manually?

I have autoplete with multiple selection permission.

https://codesandbox.io/s/bold-jackson-dkjmb?file=/src/App.js

In the example I have 3 options for cities. How can I manually add automatic added value in TextField when something is selected?

In other words here:

 renderInput={(params) => {
        console.log(params);
        return (
          <TextField
            {...params}
            variant="outlined"
            label="Cities"
            placeholder="Enter cities"
            autoComplete="off"
            InputProps={{
              ...params.InputProps,
              endAdornment: (
                <React.Fragment>
                  {params.InputProps.endAdornment}
                </React.Fragment>
              )
            }}
          />
        );
  }}

I want to be able to add to params.InputProps.startAdornment a value before rendering the textfield.

as every selected object seems to be very plex object, how I can do this manually(It is too plicated to push())? Any ideas how I can add object like this:

manually?

Share Improve this question edited Aug 25, 2021 at 9:55 dee 2,4243 gold badges16 silver badges35 bronze badges asked Aug 19, 2021 at 11:51 Borislav StefanovBorislav Stefanov 7313 gold badges19 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13 +100

the value of startAdornment is undefined until a value is chosen from the dropdown/checkbox. So, you could add startAdornment property to the InputProps like below,

import { Chip } from '@material-ui/core';
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles((theme) => ({
  chip: {
    margin: theme.spacing(0.5, 0.25)
  }
}));

const classes = useStyles();

const handleDelete = (item) => () => {...};

 renderInput={(params) => {
    console.log(params);
    return (
      <TextField
        {...params}
        variant="outlined"
        label="Cities"
        placeholder="Enter cities"
        autoComplete="off"
        InputProps={{
          ...params.InputProps,
          startAdornment: (
            <Chip
              key={"manual"}
              tabIndex={-1}
              label={"manually added"}
              className={classes.chip}
              onDelete={handleDelete("blah")}
              deleteIcon // requires onDelete function to work
            />
          ),
          endAdornment: (
            <React.Fragment>
              {params.InputProps.endAdornment}
            </React.Fragment>
          )
        }}
      />
    );
  }}

The other solution didn't work 100% from myside,
As it adds the automatic field,
But new selected options -> are selected but chips not showing next to the automatic added option!!

So to fix this problem I made a few changes:

<TextField
    {...params}
    variant="outlined"
    label="Cities"
    placeholder="Enter cities"
    autoComplete="off"
    InputProps={{
      ...params.InputProps,
    startAdornment: (
              <>
                <Chip
                 key={"manual"}
                 tabIndex={-1}
                 label={"manually added"}
                 className={classes.chip}
                 onDelete={handleDelete("blah")}
                 deleteIcon // requires onDelete function to work
                />
                <React.Fragment> //the change
                  {" "}
                  {params.InputProps.startAdornment}{" "}. //the change
                </React.Fragment>
              </>
            ),
          }}
      endAdornment: (
        <React.Fragment>
          {params.InputProps.endAdornment}
        </React.Fragment>
      )
    }}
  />
发布评论

评论列表(0)

  1. 暂无评论