javascript - How to change fontsize of options in Material ui autocomplete? - Stack Overflow

Hi i am using material table in my project and i want to know how to change font size of options in mat

Hi i am using material table in my project and i want to know how to change font size of options in material ui autoplete. Thank you

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autoplete from '@material-ui/lab/Autoplete';

export default function ComboBox() {
  return (
    <Autoplete
      id="bo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
    />
  );
}

// Top 100 films as rated by IMDb users. 
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },

];

Hi i am using material table in my project and i want to know how to change font size of options in material ui autoplete. Thank you

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autoplete from '@material-ui/lab/Autoplete';

export default function ComboBox() {
  return (
    <Autoplete
      id="bo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb./chart/top
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },

];

Share Improve this question asked Nov 23, 2020 at 6:34 theWanderertheWanderer 6562 gold badges13 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You need to use renderOptions, together with the ponent you want.

 <Autoplete
        id="bo-box-demo"
        options={top100Films}
        renderOption={(option) => (
          <Typography style={{ fontSize: "1.5rem" }}>{option.title}</Typography>
        )}
        getOptionLabel={(option) => option.title}
        style={{ width: 300 }}
        renderInput={(params) => (
          <TextField
            {...params}
            label="Combo box"
            variant="outlined"
            inputProps={{ ...params.inputProps, style: { fontSize: "1rem" } }}
          />
        )}
      />

Codesandbox

Mui uses ul li html elements to render the autoplete options, by overriding renderOption you can customize lis and their inner content as well (ul is already put by Mui) and set them your desired styles, classes, etc.

Use renderOption like this :

<Autoplete
    id='bo-box-demo'
    options={top100Films}
    renderOption={(props, option) => (
    <li key={option} {...props}>
      <Typography style={{ fontSize: '1.1rem' }}>{option}</Typography>
      //<Typography variant='h6' >{option}</Typography>
      //<Typography className='font-large yourDesiredClass' >{option}</Typography>
    </li>
  )}
  />

Note that in above snippet, I emitted some of unmodified props like style, renderInput, etc to emphasize how to use renderOption prop, correctly and also for purpose of brevity.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745171170a4614933.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信