0
votes

I have two <CreatableSelect /> for min and max properties, Is there a way I can disable some options for second <CreatableSelect /> if first <CreatableSelect /> value is less than second <CreatableSelect /> option? I'm using react-select v2. I know isOptionDisabled prop is available but I need to disable options once one of the two has changed. I can also modify the options object that being passed to <CreatableSelect /> and set another property isDisabled but this seems not efficient.

import React from 'react';
import { FormSection, Field } from 'redux-form';
import CreatableSelect from 'react-select/lib/Creatable';

// components
import { LAND_SIZE_OPTIONS } from '../../../../../../constants/search';

const handleInputChange = (newValue, callBack) => {
    callBack(false);
};

const handleSelectChange = (newValue, input, callBack) => {
    callBack(true);
    input.onChange(newValue.value);
};

const RenderSelect = props => (
    <CreatableSelect
        simpleValue
        onChange={newValue => handleSelectChange(newValue, props.input, props.callBack)}
        onInputChange={newValue => handleInputChange(newValue, props.callBack)}
        options={LAND_SIZE_OPTIONS}
        placeholder={props.placeholder}
    />
);

const LandSizeSelect = props => (
    <div className="land-size-min-max-container">
        <p>Land Size</p>
        <div className="menu-select">
            <FormSection name="landArea">
                <Field
                    name="min"
                    component={RenderSelect}
                    {...{
                        callBack:
                        props.overrideDropdownSelectClickOutside,
                        placeholder: 'Min',
                        stateValue: props.max,
                    }}
                />
                <Field
                    name="max"
                    component={RenderSelect}
                    {...{
                        callBack:
                        props.overrideDropdownSelectClickOutside,
                        placeholder: 'Max',
                        stateValue: props.min,
                    }}
                />
            </FormSection>
        </div>
    </div>

);

export default LandSizeSelect;
1
Could you provide the code you have done so far ? - Laura
@Laura just pasted a snippet. What I'm trying to do when I select a min value, max options value that is less than to min value should be disabled and vice versa - Ron.Basco

1 Answers

0
votes

Form the documentation here I think your best chance is to pass our select values in props from one to the other.

Then in your RenderSelect either you create a new option set with isDisabled prop either you use isOptionDisabled function like this:

  const RenderSelect = props => (
    const { functionToCompare } = props
    const options = LAND_SIZE_OPTIONS.map(functionToCompare)
    return {
      <CreatableSelect
          simpleValue
          onChange={newValue => handleSelectChange(newValue, props.input, props.callBack)}
          onInputChange={newValue => handleInputChange(newValue, props.callBack)}
          options={options}
          placeholder={props.placeholder}
      />
    } 
);

Where functionToCompare could be something like:

const functionToCompare = option => {
  return {
    ...option,
    isDisabled: option.value <= formValueSelector("newPost")(state, "title").value
  }
}