JedWatson / react-select

The Select Component for React.js
https://react-select.com/
MIT License
27.61k stars 4.12k forks source link

[1.0.0-beta13] Async options with Promises displays [object Promise] as input value #940

Closed matejlauko closed 8 years ago

matejlauko commented 8 years ago

When loading options async with Promises, like in the example [object Promise] string is set as input value of Select

const getOptions = (input) => {
  return fetch(`/users/${input}.json`)
    .then((response) => {
      return response.json();
    }).then((json) => {
      return { options: json };
    });
}

<Select.Async
    loadOptions={getOptions}
/>

It is because of #907.

loadOptions from Async component attaches to onInputChange on Select component. so this

if (this.state.inputValue !== event.target.value && this.props.onInputChange) {
    let nextState = this.props.onInputChange(newInputValue);
    if (nextState != null) {
        newInputValue = '' + nextState;
    }
 }

sets newInputValue to return value of loadOptions, which is [object Promise]

VladimirPal commented 8 years ago

I've got the same error. https://github.com/VladimirPal/react-select/tree/async_promise_bug - here i've added example with fetch.js + bug fixing

matejlauko commented 8 years ago

@VladimirPal thx for the fix

JedWatson commented 8 years ago

Fixed by #941, thanks @VladimirPal!

And thanks @matejlauko for the comprehensive report too :)

dvreed77 commented 8 years ago

I'm running 1.0.0-beta13 and am still experiencing this bug.

VladimirPal commented 8 years ago

@dvreed77 Currently this fix only in master branch

dvreed77 commented 8 years ago

maybe dumb question, but can I force use this branch from npm?

VladimirPal commented 8 years ago

Try this: "react-select": "git+ssh://git@github.com:JedWatson/react-select.git"

VladimirPal commented 8 years ago

@dvreed77 But it will not help you:( You have to build lib folder manually. cd node_modules/react-select npm install && npm build

dvreed77 commented 8 years ago

@VladimirPal Was battling some corporate proxy stuff there for a second. I followed all that, but am still getting the [object Promise] in Select.

Only difference is I used http protocal vs ssh: "react-select": "git+https://github.com/JedWatson/react-select.git"

VladimirPal commented 8 years ago

@dvreed77 i've created builded version, try use it: "git+https://github.com/VladimirPal/react-select.git"

dvreed77 commented 8 years ago

I get this:

 gulp-babel@5.3.0 node_modules/react-select/node_modules/gulp-babel
- babelify@6.4.0 node_modules/react-select/node_modules/babelify
- react-component-gulp-tasks@0.7.7 node_modules/react-select/node_modules/react-component-gulp-tasks
- babel-eslint@4.1.8 node_modules/react-select/node_modules/babel-eslint
- babel@5.8.38 node_modules/react-select/node_modules/babel
- react-select@1.0.0-beta13 node_modules/react-select/node_modules/react-select
orgchart@1.0.0 /Users/dreed/Projects/orgchart
├── react@15.0.2  extraneous
├── react-dom@15.0.2  extraneous
├── react-select@1.0.0-beta13  invalid (git+https://github.com/VladimirPal/react-select.git#acb3a69464dfe66edf00faef2b7fbebac2f9ddb5)
└── reactify@1.1.1  extraneous

npm WARN orgchart@1.0.0 No description
npm WARN orgchart@1.0.0 No repository field.

Note the invalid error

dvreed77 commented 8 years ago

Also am trying it without browserify and including the .js and .css from /dist directory and am still getting same issue

dvreed77 commented 8 years ago

@VladimirPal I'm closer... I was using this example code, which doesn't seem to work:

const getOptions = (input) => {
  return fetch(`/users/${input}.json`)
    .then((response) => {
      return response.json();
    }).then((json) => {
      return { options: json };
    });
}

<Select.Async
    name="form-field-name"
    value="one"
    loadOptions={getOptions}
/>

and now am using something like this:

getUsers: function (input, callback) {
            console.log(input);
            fetch(`/users/${input}.json`)
                .then(function(response) {
                    return response.json()
                })
                .then(function(json) {
                    var data = {
                        options: json,
                        complete: false
                    };
                    callback(null, data);
                });

        },

This still isn't quite working, but am not getting the [object Promise] in select window.

The optionRenderer is only firing on first character, even though I can see elements coming back

dvreed77 commented 8 years ago

ok, even closer: had to set filterOption={function(){ return true; }} to just pass through

mattparrilla commented 8 years ago

@JedWatson any time frame on when this might get released? I'm not familiar with the release cadence but would love to use fetch + react-select for async options!