Is there a way to add floating label to react-select ?
I'm trying to accomplish the floating label in react-select for eg http://code-kotis.github.io/react-floating-label/
The only way I know to add placeholder is like <Select.... placeholder={} /> but there is nothing I've found on adding floating label to it. Can anyone please share some reference or any pointers on how to achieve this ?
Hi Himanshu,
I was having the same error but it is resolved now. See the below I have added the code.
textFieldProps={{ label: 'Label', InputLabelProps: { shrink: true, }, }}
In React-Select.
<Select
classes={classes}
styles={selectStyles}
textFieldProps={{
label: 'Label',
InputLabelProps: {
shrink: true,
},
}}
options={this.props.options}
components={components}
value={this.props.selectedOption}
onChange={this.handleChange('single')}
placeholder={this.props.placeholder}
isSearchable={this.props.isSearchable&&this.props.isSearchable}
isMulti={this.props.isMulti?this.props.isMulti:false}
isClearable={this.props.isClearable?this.props.isClearable:false}
/>
@khushboo2502 Hi, could you provide like a codesandbox or something, as I tried to add that code to my Select and it's not working:
const selectStyles = {
menu: base => ({
...base,
zIndex: 100
}),
control: (provided, state) =>
state.selectProps.value.length === 0
? {
...provided,
boxShadow: "0 0 0 .1px red !important",
borderColor: "red !important"
}
: provided
};
<Select
styles={selectStyles}
classNamePrefix="select"
textFieldProps={{
label: "Label",
InputLabelProps: {
shrink: true
}
}}
isSearchable={true}
name="Dep"
placeholder="Select..."
value={dep}
defaultValue={option[0]}
options={options}
onChange={e =>
this.props.input
}
/>
@FlavyP Were you able to find a solution to this? And if not, @khushboo2502 : could you provide a codesandbox like @FlavyP requested? Thanks!
I also want a shrunken label on the top of the select box like normal react textfields. I tried the code posted above, but it did not work.
This comment has a functioning example for a floating label.
Hello -
In an effort to sustain the react-select project going forward, we're closing old issues.
We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version.
If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.
However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you!
This is how I adding a label to the select.
"react-select": "4.3.0",
import Select, { Props as SelectProps, StylesConfig } from 'react-select';
export interface SelectFieldProps extends SelectProps {
error?: boolean;
helperText?: string;
label?: string;
}
function SelectField({
id,
label,
value,
onChange,
options,
...otherProps
}: SelectFieldProps): ReactElement {
return (
<FormControl>
{label && <Label htmlFor={id}>{label}</Label>}
<Select
inputId={id}
styles={customStyles}
value={value}
onChange={onChange}
options={options}
{...otherProps}
/>
</FormControl>
);
}
This is how it looks:

@ModPhoenix didn't understand what you did, can you please elaborate too?
@preetu391 He added an "id" for the Select and made the label by himself, just like usual in HTML.
<label for="firstname">First name:</label> <input type="text" id="firstname">
but in this case, he passed id in react-select and it's a good approach, you can see the same example in react-select documentation https://react-select.com/advanced#accessibility
