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

Autocomplete reimplementation #1169

Open
javivelasco opened this issue Jan 23, 2017 · 7 comments
Open

Autocomplete reimplementation #1169

javivelasco opened this issue Jan 23, 2017 · 7 comments

Comments

@javivelasco
Copy link
Member

One of the most problematic components so far is the Autocomplete. The most important problem is that the control logic and state is delegated to the component internals which makes very difficult to maintain the improve the component. Also, it's not fully customizable: the rendering of internal options cannot be easily changed, there cannot be separators, Chips are always rendered in the same place, you cannot control rendering inside the cursor... etc. Also you have to control how many options are passed because children rendering is delegated,

All of this point to a reimplementation more than fixes. I'm closing issues related to the Autocomplete, they are going to be fixed here. Fixes #174 #1065

@itamarro
Copy link

Hey, just curious, is this already being worked on somewhere? Or is it just at the ideation stage for now?

@victoragung
Copy link

Just to clarify that this change will add the scrollbars to the dropdown if there are options hidden from the view? Also when can we expect this to be released?

@joelzimmer
Copy link

Is there any update/progress on this? We have a use case for an ajax-based autocomplete and would love to use this. Happy to help out if needed.

@joelzimmer
Copy link

To get the ball rolling/randomly suggest a feature:

We have a need to:

  1. Allow users to type data into a field
  2. Perform an ajax request to provide a list of possible results based on the text typed in (after a delay in activity)
  3. Provide a list of options based on the result of that request
  4. Allow users to select data from the dropdown or submit their own data as it is.

We're currently performing steps 1 & 4 via an Input and an element like the Chip you have created, but an autocomplete would be incredibly useful for 2 & 3. I'm happy to work on this if it sounds good, or if there are other ideas, I'm happy to hear those as well.

@joezen777
Copy link

Looking for a way to sort by property value instead of property name.

@jasonmorita
Copy link

jasonmorita commented Oct 18, 2017

@joelzimmer For the record, you can use this component with AJAX. I did this today using this HOC (Full disclosure: that I made) https://www.npmjs.com/package/react-redux-ui-state

Here is the example I got working using Apollo, hopefully someone can use the idea:

import React from 'react';
import { withApollo, compose } from 'react-apollo';
import { pick } from 'lodash';
import Autocomplete from 'react-toolbox/lib/autocomplete';
import { uiState } from 'react-redux-ui-state';
import suggestionQuery from './queries/suggestionQuery.graphql';

export function MyComponent({ client, setUiState, uiSelections, uiSource }) {
    function slugify(str) {
        return `${str.split(' ').join('_')}`;
    }

    function loadSuggestions(query) {
        setUiState({
            uiSource: { [slugify(query)]: query, ...pick(uiSource, uiSelections) },
        });

        return client
            .query({
                query: suggestionQuery,
                variables: {
                    text: query,
                },
            })
            .then(({ data: { suggestions: { items } } }) => {
                // we want to update the source with the results from the server
                const results = items.reduce((acc, item) => {
                    return {
                        [slugify(item.label)]: item.label,
                        ...acc,
                    };
                }, {});

                const merged = { [slugify(query)]: query, ...results, ...uiSource };

                return setUiState({
                    uiSource: merged,
                });
            });
    }

    return (
        <div>
            <Autocomplete
                direction="down"
                selectedPosition="above"
                label="your label"
                multiple
                onChange={value => setUiState({ uiSelections: value })}
                onQueryChange={loadCategorySuggestions}
                onFocus={() =>
                    setUiState({
                        uiSource: pick(uiSource, uiSelections),
                    })}
                source={uiSource}
                suggestionMatch="anywhere"
                value={uiSelections}
            />
        </div>
    );
}

const uiStateConfig = {
    name: 'MyComponent',
    state: () => ({
        uiSelections: [],
        uiSource: {},
    }),
};

const wrapper = compose(withApollo, uiState(uiStateConfig));

export default wrapper(MyComponent);

@vitalif
Copy link

vitalif commented Dec 17, 2017

1 bug:

When Autocomplete is placed in the end of a Card, its options are clipped away by Card's 'overflow: hidden'.
The only real fix for it is to render options in document.body, not as a child of input element.

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

No branches or pull requests

7 participants