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

Add maxlength prop to AsyncCreatable

Open stinoga opened this issue 10 years ago • 11 comments

It would be nice to be able to set a maxlength on the combobox input through a prop so we can limit the length on tag creation if needed.

stinoga avatar Oct 04 '16 17:10 stinoga

What happened to this issue? Any updates?

AlinCiocan avatar Sep 04 '17 12:09 AlinCiocan

This feature would be very useful.

cal0610 avatar May 01 '18 07:05 cal0610

Just some quick look into the source code, found that you can actually pass inputProps to the component...

e.g. <Select inputProps={{ maxLength: 5 }} {...this.props} />

cal0610 avatar May 01 '18 07:05 cal0610

@cal0610 doesn't work for me, also it is not documented in @types/react-select Es seen here #2571 the inputProps is removed in v2

krnlde avatar Dec 03 '18 15:12 krnlde

You can provide a custom Input to the Select's component props that has a maxLength set.

e.g. ` import Select, {components} from 'react-select'; const MaxLengthInput = props => <components.Input {...props} maxLength={5} />;

<Select ... components={{Input: MaxLengthInput}} `

tmeisenh avatar Jan 14 '19 20:01 tmeisenh

@tmeisenh what if maxLength needs to be parameterized as well? Surely we don't want to define MaxLengthInput on every render...

const MyFieldWithMaxLength = (props) => {
  const MaxLengthInput = (inputProps) => <components.Input maxLength={props.inputMaxLength} {...inputProps} />
  return <Select components={{ Input: MaxLengthInput }} />
}

For anyone else -- I settled on implementing manually via onInputChange:

const handleInputChange = inputValue => {
    if (props.inputMaxLength && inputValue.length > props.inputMaxLength) {
        return;
    }
    setInputValue(inputValue);
};

mponizil avatar Apr 16 '19 20:04 mponizil

Why did we close this one?

ruben0626 avatar Feb 04 '20 20:02 ruben0626

A working solution to this is writing a custom Input component and leveraging selectProps.

const Input = props => {
  const { maxLength } = props.selectProps;
  return <components.Input {{...props }} maxLength={maxLength} />
}

const MySelect = props => <Select components={{Input}} maxLength={props.maxLength} ... />

ebonow avatar Dec 03 '20 01:12 ebonow

how to use maxlength in AsyncPaginate ?

ajinbramantio avatar Nov 12 '21 14:11 ajinbramantio

Since we are already asking completely unrelated questions in closed issues: How much sugar do I need for pancakes?

krnlde avatar Nov 12 '21 16:11 krnlde

@ajinbramantio did you even try the solution posted?

ebonow avatar Nov 12 '21 18:11 ebonow