Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

reactjs - Material-UI Autocomplete rendered values

I have a ReactJs app that use a Material-UI Autocomplete component with multiple values and checkboxes totally like their code snippet :

<Autocomplete
      multiple
      id="checkboxes-tags-demo"
      options={top100Films}
      disableCloseOnSelect
      getOptionLabel={(option) => option.title}
      renderOption={(option, { selected }) => (
        <React.Fragment>
          <Checkbox
            icon={icon}
            checkedIcon={checkedIcon}
            style={{ marginRight: 8 }}
            checked={selected}
          />
          {option.title}
        </React.Fragment>
      )}
      style={{ width: 500 }}
      renderInput={(params) => (
        <TextField {...params} variant="outlined" label="Checkboxes" placeholder="Favorites" />
      )}
    />

After checking a values, that value will be rendered in a chip with a "X" button like this: enter image description here

How can I change the props so the rendered values will be shown like "Value A, Value B, "Value C" with comma between them and no other styling?

Thank you for your time


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

provide autocomplete RenderTags prop:

Signature:

function(value: T[], getTagProps: function) => ReactNode
value: The value provided to the component.
getTagProps: A tag props getter.

in your case:

renderTags={(values) => 
  values
  .map(value => value.title)
  .join(', ')
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...