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
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.
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.
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.
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', }), };
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} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With