Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Dropdown value is [object Object] when selecting an item that is an object using DropdownItemProps #2336

Open
KyleBastien opened this issue Feb 10, 2020 · 3 comments
Labels
vsts Paired with ticket in vsts

Comments

@KyleBastien
Copy link

KyleBastien commented Feb 10, 2020

Bug Report

If you provide an object for the items prop on Dropdown (fitting the DropdownItemProps interferace) and a defaultValue the Dropdown seems to break and not show content.

Steps

Create Dropdown items that might look like this:

const inputItems = [
  {
    key: "BruceW",
    content: "Bruce Wayne",
    active: false
  },
  {
    key: "Natasha",
    content: "Natasha Romanoff"
  },
  {
    key: "Steven",
    content: "Steven Strange"
  }
];

const DropdownExample = () => (
  <Dropdown
    items={inputItems}
    defaultValue={inputItems[1]}
    getA11ySelectionMessage={{
      onAdd: item => `${item} has been selected.`
    }}
  />
);

Here I need to create objects for these items, because I need to pass in the active property for some of my items.

I passed in a defaultValue which is the second item in my items array. But when I do this, I get this result:

image

So instead I changed the Dropdown to this:

const DropdownExample = () => (
  <Dropdown
    items={inputItems}
    defaultValue={inputItems[1].content}
    getA11ySelectionMessage={{
      onAdd: item => `${item} has been selected.`
    }}
  />
);

Which does give me a correct initial render:

image

But after selecting another item in this dropdown, I get back to a broken "[object Object]" state:

image

Expected Result

The expected result would be that defaultValue should render the content on the DropdownItemProps, as well as when selecting other values Dropdown should render the content property as well.

Actual Result

The defaultValue renders "[object Object]" and even if you explicitly pass the content prop, it breaks when selecting another value.

Version

0.44.0

Testcase

https://codesandbox.io/s/fluent-ui-example-qv4e8

@pkumarie2011 pkumarie2011 added the vsts Paired with ticket in vsts label Feb 10, 2020
@jugglingcats
Copy link

Have same issue. Would love to know if it's a bug or user error on my part.

@jugglingcats
Copy link

jugglingcats commented Feb 17, 2020

I managed to find a workaround (for my use case anyway) by supplying itemToString. My code:

export const FrameSelector = () => {
    const items = [
        <div key="item1" placeholder="Item 1" style={{marginLeft: "10px"}}>Item 1</div>,
        "Item 2"
    ];

    function itemToString(value) {
        const placeholder = value && value.props && value.props.placeholder;
        return placeholder || "PLACEHOLDER NOT PROVIDED";
    }

    return <Dropdown itemToString={itemToString} items={items}/>
};

My goal is simply to indent some items to make the dropdown look tree-like. I don't know if the above is best practice but it serves my purpose.

I realise re-reading the OP issue that mine is not quite the same, since my content is an element with style, but it may still be a solution to the original issue.

However, I notice that my solution does not correctly select the default value, if given:

    return <Dropdown itemToString={itemToString} items={items} defaultValue="Item 1"/>

This correctly displays "Item 1" in the trigger button but when dropdown is opened the second item is highlighted not the first.

@KyleBastien
Copy link
Author

@jugglingcats Thanks for the tip on itemToString, that got me closer (sort of..) and I ended up with this:

https://codesandbox.io/s/fluent-ui-example-wo34b

The issue I'm running into now is that the active property doesn't seem to be respected, and I'm able to select items even if it's set to active: false.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
vsts Paired with ticket in vsts
Projects
None yet
Development

No branches or pull requests

3 participants