React-select: [v3] Autosize or alternative ?

Created on 3 Jun 2019  路  6Comments  路  Source: JedWatson/react-select

I am trying to make the width of my select box match the length of the selected item (at startup with its default value and afterwards with the selected item)
I found out about autosize that does not seem to work (autosize={false} or autosize={true}).
_Is it deprecated maybe in v3 ?_
The example here does not work as soon as I set the width below "90%"
https://github.com/JedWatson/react-select/issues/2636#issuecomment-448649800

I am using React 16.8.6 and react-select 3.0.3 on a POC.

categorquestion issuenhancement issureviewed

Most helpful comment

Managed to hack something together with custom styles in v3, see this CodeSandbox for a demo.

Kapture 2020-02-26 at 8 15 02


View code

```javascript
export default function App() {
// Unfortunately neither "input" or "singleValue" receive isFocused
// in their style state, so we have to capture this value in the container
// style function and store it in a ref so that other style functions can
// access it.
// This makes assumptions about the order of style rendering and is not
// guaranteed to work consistently. Use at your own risk.
const isFocusedRef = useRef(false);

const customStyles = useMemo(
() => ({
container: (base, state) => {
isFocusedRef.current = state.isFocused;
return {
...base,
display: "inline-block"
};
},
placeholder: (base, state) => {
return {
...base,
...(isFocusedRef.current && state.value
? {}
: {
position: "static",
top: "auto",
transform: "none"
})
};
},
input: (base, state) => {
return {
...base,
...(isFocusedRef.current && state.value
? {}
: {
position: "absolute",
top: "50%",
transform: "translateY(-50%)"
})
};
},
singleValue: (base, state) => {
return {
...base,
maxWidth: "none",
...(isFocusedRef.current && state.value
? {}
: {
position: "static",
top: "auto",
transform: "none"
})
};
}
}),
[]
);

return (


options={options}
styles={customStyles}
placeholder="Search items..."
/>

);
}
```

All 6 comments

yeah, autosize was a nice feature in v1. perhaps you can override it with

components={{
   Input: MyNormalInput
}}

const MyNormalInput = (props) => <input { ...props } />

yeah, autosize was a nice feature in v1. perhaps you can override it with

components={{
   Input: MyNormalInput
}}

const MyNormalInput = (props) => <input { ...props } />

It is only adding a border around the text but not shrinking the combobox itself.
I guess I need to specify a width but where and how ?

Screenshot-05-06-2019-23-55-39

const MyNormalInput = (props) => <input { ...props } style={{ width: '98%' }}/>?

^ Tried that to no avail :(

Here is the sample
Edit react-select single basic

@oudoulj Were you able to get this working? I also noticed it uses react-input-autosize internally. Maybe its suppose to autosize by default but so far nothing I've tried works.

Managed to hack something together with custom styles in v3, see this CodeSandbox for a demo.

Kapture 2020-02-26 at 8 15 02


View code

```javascript
export default function App() {
// Unfortunately neither "input" or "singleValue" receive isFocused
// in their style state, so we have to capture this value in the container
// style function and store it in a ref so that other style functions can
// access it.
// This makes assumptions about the order of style rendering and is not
// guaranteed to work consistently. Use at your own risk.
const isFocusedRef = useRef(false);

const customStyles = useMemo(
() => ({
container: (base, state) => {
isFocusedRef.current = state.isFocused;
return {
...base,
display: "inline-block"
};
},
placeholder: (base, state) => {
return {
...base,
...(isFocusedRef.current && state.value
? {}
: {
position: "static",
top: "auto",
transform: "none"
})
};
},
input: (base, state) => {
return {
...base,
...(isFocusedRef.current && state.value
? {}
: {
position: "absolute",
top: "50%",
transform: "translateY(-50%)"
})
};
},
singleValue: (base, state) => {
return {
...base,
maxWidth: "none",
...(isFocusedRef.current && state.value
? {}
: {
position: "static",
top: "auto",
transform: "none"
})
};
}
}),
[]
);

return (


options={options}
styles={customStyles}
placeholder="Search items..."
/>

);
}
```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

x-yuri picture x-yuri  路  3Comments

geraldfullam picture geraldfullam  路  3Comments

juliensnz picture juliensnz  路  3Comments

AchinthaReemal picture AchinthaReemal  路  3Comments

steida picture steida  路  3Comments