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

Default / Initial value for downshift #665

Open
rjuszkiewicz opened this issue Feb 11, 2019 · 12 comments
Open

Default / Initial value for downshift #665

rjuszkiewicz opened this issue Feb 11, 2019 · 12 comments

Comments

@rjuszkiewicz
Copy link

rjuszkiewicz commented Feb 11, 2019

I'm using downshift 3.0.0

How to set the initial default value ?

My code:

            <Downshift
                itemToString={item => (item ? item.value : '')}
                onSelect={this.props.onChange}
            >
                {({
                      getInputProps,
                      getItemProps,
                      getMenuProps,
                      isOpen,
                      inputValue,
                      highlightedIndex,
                      selectedItem
                  }) => (
                    <div className="max-width-252">
                        <CustomInput getInputProps={getInputProps} labelText={labelText}/>
                        <div>
                            {isOpen
                                ? items
                                    .filter(item => !inputValue || item.value.includes(inputValue))
                                    .map((item, index) => (
                                        <option
                                            {...getItemProps({
                                                key: item.value,
                                                index,
                                                item,
                                                style: {
                                                    backgroundColor:
                                                        highlightedIndex === index ? 'lightgray' : 'white',
                                                    fontWeight: selectedItem === item ? 'bold' : 'normal',
                                                },
                                            })}
                                        >
                                            {item.value}
                                        </option>
                                    ))
                                : null}
                        </div>
                    </div>
                )}
            </Downshift>
@silviuaavram
Copy link
Collaborator

Hello @rjuszkiewicz have you tried using the initialSelectedItem prop?

@rjuszkiewicz
Copy link
Author

@silviuavram , I've tried defaultSelectedItem as Downshift prop.
<Downshift itemToString={item => (item ? item.value : '')} onSelect={this.props.onChange} defaultSelectedItem={} >
But it's not working.
I've seen initialSelectedItemprop in documentation, but I don't know how to use it.
DownshiftProps doesn't contain initialSelectedItem.

Could you add an example how to use iinitialSelectedItem?

@silviuaavram
Copy link
Collaborator

can you please read our documentation? it's the reason why it is there. and it has all the information you needed to fix your problem in less than 5 minutes.

there is no defaultSelectedItem so no point in using that.
you can use that as <Downshift onChange={this.handleChange} initialSelectedItem='Blue'> . I copied this line from our example in combobox.js. It will add Blue as the default selected item in the dropdown, and it will also add the value to the edit text.

Is it clear now? :)

@mglaman
Copy link

mglaman commented May 22, 2021

I know this is closed, but defaultSelectedItem is in your types.

https://github.com/downshift-js/downshift/blob/master/flow-typed/npm/downshift_v2.x.x.js.flow#L51

@silviuaavram
Copy link
Collaborator

Both defaultSelectedItem and initialSelectedItem are. They provide slightly different behaviours.

@angusd3v
Copy link

angusd3v commented May 6, 2022

Hi @silviuaavram, I think you might have been mistaken here. defaultSelectedItem is in your typings and is set in some of your tests, but from the looks of it isn't actually made use of in the code anywhere. Might be confusing for consumers to see that the prop is available in the types but doesn't do anything

@silviuaavram
Copy link
Collaborator

Then it should probably be removed from the types. However, it is used in useCombobox and if you want that behaviour, then you can switch to using the hook, instead of Downshift.

Thanks for pointing this out!

@ks-himanshub
Copy link

In case if someone is looking to set initial value for input field you get use

initialInputValue regardless of available option in items

heres a code example

const {
    isOpen,
    getMenuProps,
    getInputProps,
    highlightedIndex,
    getItemProps,
    setInputValue,
  } = useCombobox({
    onInputValueChange({ inputValue }) {
      setSearchInputText(inputValue as string);
    },
    items
    itemToString(item) {
      return item ? item.title : "";
    },
    initialInputValue: "Hello World", // here it is
  });

@Shelagh-Lewins
Copy link

I can't see any mention of initialSelectedItem or defaultSelectedItem in the docs? It'd be very useful to know what is the difference between them and how to use them in useSelect, which has both.
https://www.downshift-js.com/

@silviuaavram
Copy link
Collaborator

https://github.com/downshift-js/downshift/tree/master/src/hooks/useSelect#advanced-props

Initial means the value is used at initial render. Default means the value is used at initial render + after each selection / reset.

@Shelagh-Lewins

@Shelagh-Lewins
Copy link

Ah thanks! BTW I was looking at the docs that come up in a web search for Downshift: https://www.downshift-js.com/use-select/
Perhaps they should be removed or at least contain a link to the real docs? Or if they already contain a link, I didn't see it? When there is a special docs website I tend not to think of hunting up the READMEs in the source code.

@silviuaavram
Copy link
Collaborator

We can add links to the github page labelled as full documentation. Will keep this issue open. It needs to land in https://github.com/downshift-js

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

6 participants