downshift icon indicating copy to clipboard operation
downshift copied to clipboard

bind downshift props to component

Open scottjason opened this issue 9 years ago • 3 comments

  • downshift:1.4.0
  • node: 7.4.0
  • npm (or yarn) version: 0.19.1

Relevant code or config:

      <div style={styles.container}>
        <Downshift onChange={this.onChange}>
          {({
            getInputProps,
            getItemProps,
            isOpen,
            inputValue,
            selectedItem,
            highlightedIndex
          }) => (
            <div>
              <input 
                {...getInputProps({placeholder: this.props.placeholderText})}
                onFocus={this.onFocus}
                onBlur={this.onBlur}
                style={styles.searchInput}
                />
              {isOpen ? (
                <div style={styles.dropdown}>
                  {items
                    .filter(
                      i =>
                        !inputValue ||
                        i.toLowerCase().includes(inputValue.toLowerCase()),
                    )
                    .map((item, index) => (
                      <div
                        {...getItemProps({item})}
                        key={item}
                        style={{
                          backgroundColor:
                            highlightedIndex === index ? 'gray' : 'white',
                          fontWeight: selectedItem === item ? 'bold' : 'normal',
                        }}
                      >
                        {item}
                      </div>
                    ))}
                </div>
              ) : null}
            </div>
          )}
        </Downshift>
      </div>

Problem description:

Everything works great so far, but was wondering if you have any suggestions on exposing getInputProps, getItemProps, isOpen, etc so I can breakdown the above into smaller pieces without passing everything around.

For example, to breakdown the dropdown portion, right now I have a smaller function but need to pass a bunch of arguments like so:

  getDropdown(inputValue, selectedItem, highlightedIndex, getItemProps) {
     ...
  },

Is there a way to bind these to the component? Or any other suggestions to avoid passing these props around in an effort to break this down into smaller functions?

scottjason avatar Sep 08 '17 20:09 scottjason

Hi! :wave:

You could do this:

(
  <div style={styles.container}>
    <Downshift onChange={this.onChange}>
      {downshiftStateAndHelpers => {
        const {
          getInputProps,
          getItemProps,
          isOpen,
          inputValue,
          selectedItem,
          highlightedIndex
        } = downshiftStateAndHelpers
        return (
          <div>
            <input
              {...getInputProps({ placeholder: this.props.placeholderText })}
              onFocus={this.onFocus}
              onBlur={this.onBlur}
              style={styles.searchInput}
            />
            {isOpen ? (
              <div style={styles.dropdown}>
                {items
                  .filter(
                    i =>
                      !inputValue ||
                      i.toLowerCase().includes(inputValue.toLowerCase())
                  )
                  .map((item, index) => (
                    <div
                      {...getItemProps({ item })}
                      key={item}
                      style={{
                        backgroundColor:
                          highlightedIndex === index ? 'gray' : 'white',
                        fontWeight: selectedItem === item ? 'bold' : 'normal'
                      }}
                    >
                      {item}
                    </div>
                  ))}
              </div>
            ) : null}
          </div>
        )
      }}
    </Downshift>
  </div>
)

But doing things that way would allow you to pass downshiftStateAndHelpers anywhere you want as a single prop without much trouble.

I don't think that I want to expose any other way to do this as it would probably lead to indirection and more confusion than anything. As always, this is debatable, but I'm going to close this for now.

Good luck!!

kentcdodds avatar Sep 08 '17 20:09 kentcdodds

Ok, gotcha. Thank you @kentcdodds

scottjason avatar Sep 08 '17 20:09 scottjason

Sure thing! :)

kentcdodds avatar Sep 08 '17 21:09 kentcdodds