react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Is there a way to add floating label to react-select ?

Open himanshuarora05 opened this issue 9 years ago • 9 comments

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 ?

himanshuarora05 avatar Nov 13 '17 12:11 himanshuarora05

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}
        />

tech-khushboo avatar Mar 25 '19 11:03 tech-khushboo

@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 avatar Mar 25 '19 14:03 FlavyP

@FlavyP Were you able to find a solution to this? And if not, @khushboo2502 : could you provide a codesandbox like @FlavyP requested? Thanks!

philstar4 avatar May 30 '19 22:05 philstar4

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.

NoahDavidATL avatar Jun 10 '19 20:06 NoahDavidATL

This comment has a functioning example for a floating label.

Rall3n avatar Jun 10 '19 22:06 Rall3n

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!

bladey avatar May 28 '20 03:05 bladey

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:

image

spokvulcan avatar Mar 31 '21 11:03 spokvulcan

@ModPhoenix didn't understand what you did, can you please elaborate too?

preetu391 avatar Mar 14 '23 16:03 preetu391

@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 Снимок экрана 2023-03-15 в 20 09 24

RuchkoVlad avatar Mar 15 '23 18:03 RuchkoVlad