Skip to content

Commit dbb642c

Browse files
committed
fixup! Feat(web-react): Introduce SegmentedControl #DS-1657
1 parent 2834d27 commit dbb642c

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

packages/web-react/src/components/SegmentedControl/stories/SegmentedControl.stories.tsx

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Markdown } from '@storybook/blocks';
22
import type { Meta, StoryObj } from '@storybook/react';
3-
import React from 'react';
3+
import React, { useEffect, useState } from 'react';
44
import { FillVariants } from '../../../constants';
5+
import { SegmentedControlProps } from '../../../types/segmentedControl';
56
import { Icon } from '../../Icon';
67
import ReadMe from '../README.md';
78
import SegmentedControl from '../SegmentedControl';
@@ -20,9 +21,9 @@ const meta: Meta<typeof SegmentedControl> = {
2021
control: 'select',
2122
options: ['label only', 'label with icon', 'icon only'],
2223
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\`.`,
2627
},
2728
hasMultipleSelection: {
2829
control: { type: 'boolean' },
@@ -40,6 +41,23 @@ const meta: Meta<typeof SegmentedControl> = {
4041
control: 'select',
4142
options: [...Object.values(FillVariants)],
4243
},
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+
},
4361
},
4462
args: {
4563
children: 'label only',
@@ -54,16 +72,33 @@ const meta: Meta<typeof SegmentedControl> = {
5472
export default meta;
5573
type Story = StoryObj<typeof SegmentedControl>;
5674

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+
57100
export const Playground: Story = {
58101
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-
},
67102
render: (args) => {
68103
const getItemContent = (isDisabled = false) => {
69104
const label = isDisabled ? 'Label disabled' : 'Label';
@@ -85,7 +120,7 @@ export const Playground: Story = {
85120
};
86121

87122
return (
88-
<SegmentedControl {...args} UNSAFE_style={{ display: 'inline-grid' }}>
123+
<SegmentedControlWithHooks {...args}>
89124
<SegmentedControlItem value="value-1" id="segmented-control-1">
90125
{getItemContent()}
91126
</SegmentedControlItem>
@@ -98,7 +133,7 @@ export const Playground: Story = {
98133
<SegmentedControlItem value="value-4" id="segmented-control-4">
99134
{getItemContent()}
100135
</SegmentedControlItem>
101-
</SegmentedControl>
136+
</SegmentedControlWithHooks>
102137
);
103138
},
104139
};

0 commit comments

Comments
 (0)