react-bootstrap-typeahead icon indicating copy to clipboard operation
react-bootstrap-typeahead copied to clipboard

Donot select first hint on tab but make the entered text as selection

Open sukhminderx opened this issue 6 years ago • 6 comments

Version

Steps to reproduce

  • enter text as abc in async typeahead

  • lets say abc is not present in options

  • on tab, the first item that has abc in it gets selected

Expected Behavior

On tab in such scenario, only abc should get selected as a selection. Which prop will help us in achieving this?

Actual Behavior

sukhminderx avatar Dec 12 '19 11:12 sukhminderx

You can use allowNew to create a selection from user input that isn't part of the predefined options. The hint won't work in this case, though, so you can't use tab to select.

ericgio avatar Dec 12 '19 19:12 ericgio

Thanks for the suggestion @ericgio I used this code for putting the customOption on top.

renderMenu = (results: any[], menuProps: any) => {
        //let customOption = results.splice(results.length-1,1);
        let customOption = results.find(r=> r.customOption);
        let indexOfCustomOption = results.findIndex(r=> r.customOption);
        if (customOption) {
            results.splice(indexOfCustomOption,1);
            results.unshift(customOption);
        }
        return <Menu {...menuProps}>
            {
                results.map((result, index) => {
                return <MenuItem key={result.label} option={result} position={index}>
                    {result.label}
                    </MenuItem>
            })}
        </Menu>
    }

As you rightly said the tab won't auto select the first(customOption). Is there a way to somehow mark the first option for selection programatically (just like we do with the arrow key) and then tab would work for selection...? Kindly suggest.. Thanks.

sukhminderx avatar Dec 13 '19 07:12 sukhminderx

Other observation: with allowNew, labelKey should be a string => this warning is encountered even though the typeahead works. In my logic, labelkey should be a callback.

sukhminderx avatar Dec 13 '19 09:12 sukhminderx

So, I played around a little and made explicit selection of first item(customOption) without using arrow key to highlight it first.


onBlur = (e: any) => {
        if (e.target.value) {
            let transformedInputValue = {
                label: e.target.value,
                customOption: true
            };
            this.setState({
                selectedValue: [...[transformedInputValue], ...//earlier selections]
            }, () => {
                 (this.typeahead.current as any)._instance.getInstance().setState({ text: ''});
            });
        }
    }

I am still confused about the cause of the warning with allowNew, labelKey should be a string => this warning is encountered even though the typeahead works. Though everything works fine.

sukhminderx avatar Dec 13 '19 13:12 sukhminderx

Is there a way to somehow mark the first option for selection programatically (just like we do with the arrow key) and then tab would work for selection...?

I'm not sure, you'll have to try it and see. You probably need to check for the tab key in onKeyDown and then select the input value or something like that.

I am still confused about the cause of the warning

When using allowNew, the component adds a custom option. It tries to use the developer-defined labelKey to attach the input value as a property of the option. If labelKey is something other than a string, the component warns you and falls back to using "label" as the labelkey. If it still works for you that's great, though you may run into issues, so be aware.

ericgio avatar Dec 13 '19 21:12 ericgio

Though this has been closed, I want to mention the resolution to the above labelKey issue. It seems like the library expects a simple JSON object and I was passing a custom class with label as a property. To resolve, I have to do JSON.parse(JSON.stringify(obj))

sukhminderx avatar Dec 17 '19 05:12 sukhminderx