Skip to content

Commit d877eb7

Browse files
committed
docs(Select): add receipt for reading the selectedOption
1 parent 270110d commit d877eb7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Story, Props, Title, Subtitle, Description, Primary, Stories } from '@storybook/addon-docs/blocks';
2+
import { Select } from '@ui5/webcomponents-react/lib/Select'
3+
import { Option } from '@ui5/webcomponents-react/lib/Option'
4+
5+
<Title />
6+
<Subtitle />
7+
<Description />
8+
<Primary />
9+
<Props />
10+
<Stories />
11+
12+
## Recipes
13+
14+
### Get the ID of the selected item with `onChange`
15+
16+
With the help of the HTML5 `data-` API you can define any primitive value (like numbers, strings)
17+
as an attribute on an element.
18+
This data will be available on the `onChange` event as part of the `dataset` object of the `selectedOption`:
19+
```jsx
20+
21+
const data = [
22+
{ id: 1, text: 'Option 1'},
23+
{ id: 2, text: 'Option 2'}
24+
];
25+
26+
const onChange = (event) => {
27+
// event.detail.selectedOption is a reference to the selected HTML Element
28+
// dataset contains all attributes that were passed with the data- prefix.
29+
console.log(event.detail.selectedOption.dataset.id)
30+
}
31+
32+
<Select onChange={onChange}>
33+
{data.map(item => (
34+
<Option key={item.id} data-id={item.id}>{item.text}</Option>
35+
))}
36+
</Select>
37+
```

packages/main/src/webComponents/Select/Select.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { Option } from '@ui5/webcomponents-react/lib/Option';
44
import { Select } from '@ui5/webcomponents-react/lib/Select';
55
import { ValueState } from '@ui5/webcomponents-react/lib/ValueState';
66
import React from 'react';
7+
import mdx from './Select.mdx';
78

89
export default {
910
title: 'UI5 Web Components / Select',
1011
component: Select,
1112
parameters: {
12-
subcomponents: { Option }
13+
subcomponents: { Option },
14+
docs: {
15+
page: mdx
16+
}
1317
}
1418
};
1519

0 commit comments

Comments
 (0)