1
1
import { Markdown } from '@storybook/blocks' ;
2
2
import type { Meta , StoryObj } from '@storybook/react' ;
3
- import React from 'react' ;
3
+ import React , { useEffect , useState } from 'react' ;
4
4
import { FillVariants } from '../../../constants' ;
5
+ import { SegmentedControlProps } from '../../../types/segmentedControl' ;
5
6
import { Icon } from '../../Icon' ;
6
7
import ReadMe from '../README.md' ;
7
8
import SegmentedControl from '../SegmentedControl' ;
@@ -20,9 +21,9 @@ const meta: Meta<typeof SegmentedControl> = {
20
21
control : 'select' ,
21
22
options : [ 'label only' , 'label with icon' , 'icon only' ] ,
22
23
description : `This is the place for the content of the SegmentedControl.
23
- In the real code you can pass any children you want.
24
- In this demo we have predefined options:
25
- \`label only\`, \`label with icon\` and \`icon only\`.` ,
24
+ In the real code you can pass any children you want.
25
+ In this demo we have predefined options:
26
+ \`label only\`, \`label with icon\` and \`icon only\`.` ,
26
27
} ,
27
28
hasMultipleSelection : {
28
29
control : { type : 'boolean' } ,
@@ -40,6 +41,23 @@ const meta: Meta<typeof SegmentedControl> = {
40
41
control : 'select' ,
41
42
options : [ ...Object . values ( FillVariants ) ] ,
42
43
} ,
44
+ onSelectionChange : {
45
+ action : 'onSelectionChange' ,
46
+ description : 'Callback triggered when selected value changes.' ,
47
+ table : {
48
+ category : 'Events' ,
49
+ } ,
50
+ } ,
51
+ selectedValue : {
52
+ table : {
53
+ disable : true ,
54
+ } ,
55
+ } ,
56
+ setSelectedValue : {
57
+ table : {
58
+ disable : true ,
59
+ } ,
60
+ } ,
43
61
} ,
44
62
args : {
45
63
children : 'label only' ,
@@ -54,16 +72,33 @@ const meta: Meta<typeof SegmentedControl> = {
54
72
export default meta ;
55
73
type Story = StoryObj < typeof SegmentedControl > ;
56
74
75
+ const SegmentedControlWithHooks = ( { name, children, ...args } : SegmentedControlProps ) => {
76
+ const defaultSingleValue = 'value-1' ;
77
+ const defaultMultipleValue = [ 'value-1' ] ;
78
+
79
+ const [ selectedValue , setSelectedValue ] = useState < string | string [ ] > (
80
+ args . hasMultipleSelection ? defaultMultipleValue : defaultSingleValue ,
81
+ ) ;
82
+
83
+ useEffect ( ( ) => {
84
+ setSelectedValue ( args . hasMultipleSelection ? defaultMultipleValue : defaultSingleValue ) ;
85
+ } , [ args . hasMultipleSelection ] ) ;
86
+
87
+ return (
88
+ < SegmentedControl
89
+ { ...args }
90
+ name = { `${ name } ` }
91
+ UNSAFE_style = { { display : 'inline-grid' } }
92
+ selectedValue = { selectedValue }
93
+ setSelectedValue = { setSelectedValue }
94
+ >
95
+ { children }
96
+ </ SegmentedControl >
97
+ ) ;
98
+ } ;
99
+
57
100
export const Playground : Story = {
58
101
name : 'SegmentedControl' ,
59
- args : {
60
- children : 'labelOnly' ,
61
- hasMultipleSelection : false ,
62
- isFluid : false ,
63
- name : 'segmented-control-example' ,
64
- label : 'Segmented Control Example' ,
65
- variant : FillVariants . OUTLINE ,
66
- } ,
67
102
render : ( args ) => {
68
103
const getItemContent = ( isDisabled = false ) => {
69
104
const label = isDisabled ? 'Label disabled' : 'Label' ;
@@ -85,7 +120,7 @@ export const Playground: Story = {
85
120
} ;
86
121
87
122
return (
88
- < SegmentedControl { ...args } UNSAFE_style = { { display : 'inline-grid' } } >
123
+ < SegmentedControlWithHooks { ...args } >
89
124
< SegmentedControlItem value = "value-1" id = "segmented-control-1" >
90
125
{ getItemContent ( ) }
91
126
</ SegmentedControlItem >
@@ -98,7 +133,7 @@ export const Playground: Story = {
98
133
< SegmentedControlItem value = "value-4" id = "segmented-control-4" >
99
134
{ getItemContent ( ) }
100
135
</ SegmentedControlItem >
101
- </ SegmentedControl >
136
+ </ SegmentedControlWithHooks >
102
137
) ;
103
138
} ,
104
139
} ;
0 commit comments