React Select auto size width

前端 未结 5 2114
闹比i
闹比i 2020-12-30 06:00

When using react-select it is not auto sizing by option value, but using width:100% as you can see in picture:

Options are short:<

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 06:37

    Update: for people who are using React-Select for a "tags autocomplete" feature but are having trouble where it sets a style width that is too narrow based on the previous tag you searched, this is what works for me:

    Set a style of:

    .myCustomPrefix__value-container > div {
      width: auto !important;
    }
    

    Set classNamePrefix="myCustomPrefix" in the component (docs).


    Old answer:

    See the official docs at https://react-select.com/styles#style-object

    I originally thought that setting width to "auto" for option worked for me:

    const customStyles = {
        option: (styles, { data, isDisabled, isFocused, isSelected }) => {
          return {
            ...styles,
            fontSize: '12px',
            textAlign: 'left',
            width: 'auto',
          }
        },
      }
    
    ...
    
    return (
        //https://react-select.com/props
        
      )
    

提交回复
热议问题