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

docs(TreeSelect): getItemById prop docs #1748

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/components/TreeSelect/__stories__/TreeSelectDocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ Rewritten component [Select](https://preview.gravity-ui.com/uikit/?path=/docs/co

`Storybook` provides complex examples how to use this components from this documentation.

### Import:
## Import:

```tsx
import {unstable_TreeSelect as TreeSelect} from '@gravity-ui/uikit/unstable';
```

### Basic example:
## Example

### Basic:

```tsx
import {
Expand Down Expand Up @@ -59,3 +61,42 @@ const Component = () => {
);
};
```

## Props:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be it's better to rename section as a FAQ


### getItemById:

Take a look at this example:

```tsx
<TreeSelect
value={['two']}
items={['one', 'two', 'free']}
mapItemDataToContentProps={(title) => ({title})}
/>
```

In this case we will see select with `empty` value.

Why this happens?

Internal list representation make own `id` for every item. By default it bases on index of base and nested arrays.
Result `id` will be computed by formula: `{root-array-item-index}-{child-array-index-if-exists}-{...}`.
In example to select second item you need to set as a value item index:

```sh
value={['1']}
```

To fix our example we need to use `getItemById` prop and explicitly tell `TreeSelect` to use items values as uniq ids:

```diff
<TreeSelect
value={['two']}
+ getItemById={(value: id) => id}
items={['one', 'two', 'free']}
mapItemDataToContentProps={(title) => ({title})}
/>
```

Now we will se selected element with value `two`
Loading