Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value which is not in a list #179

Closed
barsukov opened this issue May 10, 2015 · 7 comments
Closed

Add value which is not in a list #179

barsukov opened this issue May 10, 2015 · 7 comments

Comments

@barsukov
Copy link

How it possible to add value when it is not in a list of options? I recceived every time empty value:(
like create = true? is it support?

@dcousens
Copy link
Collaborator

@barsukov can you elaborate on what it is you're trying to do? Maybe a [pseudo] code example?

@barsukov
Copy link
Author

@dcousens Ok I will try to explain it here.
Like in Selectize lib I want to have ability to add Item when nothing is found like that
screenshot 2015-05-12 12 42 19
Instead of
screenshot 2015-05-12 12 43 32
I found #101 here in development which provide functionality like <Select ... creatable={true} />

This functionality Is very very very important, without it this component should not be usable...

@anthonator
Copy link

I've been able to accomplish something like this when using asyncOptions.

Here's an example.

import React from 'react';
import Select from 'react-select';

class TagSelect extends React.Component {
  render() {
    return (
      <Select {...this.props} asyncOptions={ this._handleAsync } delimiter=',' multi={ true } />
    );
  }

  // private

  _handleAsync(input, callback) {
    setTimeout(function() {
      callback(null, {
        options: [
          { value: input, label: input }
        ]
      });
    }, 500);
  }
}

export default TagSelect;

@barsukov
Copy link
Author

This solution is good , but it looks like

@barsukov
Copy link
Author

@anthonator and your example works only in one direction, What if I want to have the search for existance options(like I received from server, and if I did not found anything I want to add this value) So useful case I am really did not understand why it is not out of the box ;(

@anthonator
Copy link

You should be able to do that.

Here's a real world example of me fetching tag values from the server and also adding the user's input at the top so the user can select what they're currently typing as an option.

If you wanted to only show the user's input if nothing was returned from the server you should be able to tweak something like this to make that work.

_handleAsync(input, callback) {
    let options = [];

    if (!_.isEmpty(input)) {
      options.push({ value: input, label: input });

      TagQueries.autocomplete(this.state.data.get('id'), input)
        .then(() => {
          TagStore.get(this.state.data.get('id')).forEach((tag) => {
            options.push({ value: tag.get('id'), label: tag.get('name') });
          });

          callback(null, { options: options });
        });
    } else {
      callback(null, { options: options });
    }
  }

@dcousens
Copy link
Collaborator

Added in #151

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants