Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing height of react-select component

Tags:

I am using the react-select component along with bootstrap v4

all of bootstraps stuff is based on 35px height it seems, the default height of the react-select component is 38px, which looks a little odd.

Any ideas how I can change the height of the component?

It is using some weird JS styling library I have never come across before. I have managed to get it to mimic the outline on focus using it, but the height escapes me, any help much appreceiated

You can play with it here

like image 540
DrogoNevets Avatar asked Jan 16 '19 13:01

DrogoNevets


People also ask

How do you reduce the size of React-select?

Read the react-select docs HERE. React-Select v2 uses emotion CSS-in-JS so the new way to control style over the select components and sub-components is by passing a style object to the styles prop. You can also set a className to style with an external stylesheet.

How do you set the height of a component in React?

In react native application, to set the dimensions of a component, we use width and height properties. Remember, all the values of dimensions are unitless like we can't use height: 20px to set the height of a component instead of we just use height: 20 and react-native automatically set the pixels.

How do I adjust image height in React?

To get the dimensions of image with React, we can get it from the load event handler of the image. We define the onImgLoad function that gets the image from the target property. Then we destructure the offsetHeight and offsetWidth properties from the image. Next, we log the height and width with console.


2 Answers

Spending hours, I end up with this to get exact 30px height of react select with border 1px:

  const customStyles = {     control: (provided, state) => ({       ...provided,       background: '#fff',       borderColor: '#9e9e9e',       minHeight: '30px',       height: '30px',       boxShadow: state.isFocused ? null : null,     }),      valueContainer: (provided, state) => ({       ...provided,       height: '30px',       padding: '0 6px'     }),      input: (provided, state) => ({       ...provided,       margin: '0px',     }),     indicatorSeparator: state => ({       display: 'none',     }),     indicatorsContainer: (provided, state) => ({       ...provided,       height: '30px',     }),   }; 
like image 135
Jivan Avatar answered Oct 12 '22 02:10

Jivan


You can add your styles to any part of the select components, take a look at the relevant docs

here is a working demo of what you ask for.

In your case the code that you need to add will look something like this:

const customStyles = {   control: base => ({     ...base,     height: 35,     minHeight: 35   }) }; 

and in the select component:

<Select           className="basic-single"           classNamePrefix="select"           defaultValue={colourOptions[0]}           isDisabled={isDisabled}           isLoading={isLoading}           isClearable={isClearable}           isRtl={isRtl}           isSearchable={isSearchable}           name="color"           options={colourOptions}           styles={customStyles}  /> 
like image 21
Erez Lieberman Avatar answered Oct 12 '22 02:10

Erez Lieberman