ant-design Custom Object Structure for TreeSelect Value State

gpfsuwkq  于 2022-12-31  发布在  其他
关注(0)|答案(7)|浏览(190)

What problem does this feature solve?

The current implementation only allows the value state for the TreeSelect to be of type string or string[]. In some cases you want to have more information for each selected value. For instance: When building a search UI you might want to add a different search type to each selected value like "and", "or", "not" in order to indicate how the values are going to be used. In that case it would be nice to have the option to store objects in that state like {value: "testValue", type: "and"}

This is different from the fieldNames property since fieldNames only tells the TreeSelect the name of the properties for the TreeNodes not for the state of the currently selected values

So I need the state to look line this:
{
value: [{value: "selectedValue1", type: "and"}, {value: "selectedValue2", type: "or"}, ...]
}

not like this:

{
value: ["selectedValue1", "selectedValue]
}

What does the proposed API look like?

Well there should by a possibility to tell the component which property of the value state object is going to be the label value within the value state where the selected values are stored NOT the fieldNames of the Treenodes. So it is similar to the fieldNames Property but not for the Nodes of the tree. It should work for the selected values instead

t1rydlwq

t1rydlwq2#

What's treeData look like?

eeq64g8w

eeq64g8w3#

@zombieJ
Each object for the tree looks either like this:
{title, value}

or if it has children in looks like this:
{title, value, children}

Displaying the tree in the dropdown isn't the problem. But under my Tree Select I have three radio buttons which indicate if the selected value will be a "search AND", "search OR", or "search NOT" value. but I can only store strings in the selected value state. As you can see in the documentation the "value" attribute for TreeSelect can only be String or String[]
https://ant.design/components/tree-select/

But I need a possibility to pass my custom object like
{value: "selectedValue1", type: "and"}
in there and a way to tell the component that the label for the Tag in the Inputfield comes from that object's value property.

mznpcxlj

mznpcxlj4#

Could you provide a screenshot for this?

t3irkdon

t3irkdon5#

Sure. This is the kind of search Filter I want to build. This was built using the normal Input Component with the Autocomplete component.
But I want the user to select an Option from the treeselect. The State for the tags contains three objects in that particular case:
{searchTerm: "twitter", searchType: "and"} {searchTerm: "heise", searchType: "not"} {searchTerm: "facebook", searchType: "or"}

But When selecting values from the treeselect it only returns string or an array of strings, so I can't add the searchType property right now. As I said before when passing the value containing the state for the selected options I can only pass string oder array of strings to the component.

ws51t4hk

ws51t4hk6#

Hi @RobinUndersigned

Looks like You're trying to combine 2 components - TreeSelect and Radio , which is not applicable for atomic components design, so you need to combine the state and link them together by using Native JavaScript/TypeScript (depends on your needs). Here's an example I made to get familiar with on Code Sandbox

CC: @zombieJ@afc163

ql3eal8s

ql3eal8s7#

@RuFi2k wow thank you so much! Learned a lot looking at that example :)

相关问题