Skip to content

Commit 8b48b51

Browse files
committed
refactor: use consistent pattern to write stories
1 parent 05cae48 commit 8b48b51

25 files changed

+203
-227
lines changed

packages/go-ui-storybook/src/stories/Progressbar.stories.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const meta = {
1818
},
1919
},
2020
tags: ['autodocs'],
21-
argTypes: {},
2221
} satisfies Meta<typeof ProgressBar>;
2322

2423
export default meta;

packages/go-ui-storybook/src/stories/RadioInput.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ function Template(args:Args) {
5858

5959
// NOTE: We are casting args as props because of discriminated union
6060
// used in RadionInputProps
61-
6261
const handleChange = (val: string | undefined, name: string) => {
6362
setArgs({ value: val });
6463
// eslint-disable-next-line react/destructuring-assignment

packages/go-ui-storybook/src/stories/RadioInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
RadioInput as PureRadioInput,
3-
RadioInputProps as PureRadioInputProps,
3+
RadioInputProps,
44
RadioProps,
55
} from '@ifrc-go/ui';
66

77
// eslint-disable-next-line max-len
8-
function RadioInput<const N, O extends object, V extends string | number | boolean, RRP extends RadioProps<V, N>>(props: PureRadioInputProps<N, O, V, RRP, never>) {
8+
function RadioInput<const N, O extends object, V extends string | number | boolean, RRP extends RadioProps<V, N>>(props: RadioInputProps<N, O, V, RRP, never>) {
99
return (
1010
<PureRadioInput {...props} /> // eslint-disable-line react/jsx-props-no-spreading
1111
);

packages/go-ui-storybook/src/stories/RawFileInput.stories.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type RawFileInputSpecificProps = RawFileInputProps<string>;
1111

1212
type Story = StoryObj<RawFileInputSpecificProps>;
1313

14-
const meta: Meta<RawFileInputSpecificProps> = {
14+
const meta: Meta<typeof RawFileInput> = {
1515
title: 'Components/RawFileInput',
1616
component: RawFileInput,
1717
parameters: {
@@ -55,22 +55,22 @@ export const WithAccept: Story = {
5555
},
5656
};
5757

58-
export const ReadOnly: Story = {
58+
export const Disabled: Story = {
5959
args: {
6060
name: 'RawFileInput',
6161
children: 'Export',
62-
readOnly: true,
6362
multiple: false,
6463
onChange: fn(),
64+
disabled: true,
6565
},
6666
};
6767

68-
export const Disabled: Story = {
68+
export const ReadOnly: Story = {
6969
args: {
7070
name: 'RawFileInput',
7171
children: 'Export',
72+
readOnly: true,
7273
multiple: false,
7374
onChange: fn(),
74-
disabled: true,
7575
},
7676
};

packages/go-ui-storybook/src/stories/RawFileInput.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
RawFileInput as PureRawFileInput,
3-
RawFileInputProps as PureRawFileInputProps,
3+
RawFileInputProps,
44
} from '@ifrc-go/ui';
55

6-
type RawFileInputProps<N> = PureRawFileInputProps<N>;
7-
86
function RawFileInput< const N>(props: RawFileInputProps<N>) {
97
// eslint-disable-next-line react/jsx-props-no-spreading
108
return (

packages/go-ui-storybook/src/stories/RawList.stories.tsx

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
ListKey,
33
RawListProps,
44
} from '@ifrc-go/ui';
5-
import { useArgs } from '@storybook/preview-api';
65
import type {
76
Meta,
87
StoryObj,
@@ -16,29 +15,30 @@ type Story = StoryObj<RawListSpecificProps>;
1615

1716
interface Option {
1817
id: string;
19-
key: string;
18+
label: string;
2019
}
2120

2221
const options: Option[] = [
23-
{ id: '1', key: 'Nepal' },
24-
{ id: '4', key: 'France' },
25-
{ id: '2', key: 'India' },
26-
{ id: '5', key: 'Spain' },
22+
{ id: '1', label: 'Nepal' },
23+
{ id: '4', label: 'France' },
24+
{ id: '2', label: 'India' },
25+
{ id: '5', label: 'Spain' },
2726
];
2827

2928
type RendererProps = {
30-
children: React.ReactNode
29+
label: React.ReactNode
3130
};
32-
function Option({ children }: RendererProps) {
31+
32+
function Option({ label }: RendererProps) {
3333
return (
3434
<div>
35-
{ children }
35+
{ label }
3636
</div>
3737
);
3838
}
3939

40-
const keySelector = (d: Option) => d.key;
41-
const rendererParams = (_key: string, datum: Option) => ({ children: datum.key });
40+
const keySelector = (d: Option) => d.label;
41+
const rendererParams = (_: string, datum: Option) => ({ label: datum.label });
4242

4343
const meta: Meta<typeof RawList> = {
4444
title: 'Components/RawList',
@@ -52,22 +52,6 @@ const meta: Meta<typeof RawList> = {
5252
},
5353
},
5454
tags: ['autodocs'],
55-
decorators: [
56-
function Component(_, ctx) {
57-
const componentArgs = ctx.args as RawListSpecificProps;
58-
const [args] = useArgs();
59-
const { data } = args;
60-
return (
61-
<RawList
62-
// eslint-disable-next-line react/jsx-props-no-spreading
63-
{...componentArgs}
64-
data={data}
65-
keySelector={keySelector}
66-
67-
/>
68-
);
69-
},
70-
],
7155
};
7256

7357
export default meta;
@@ -81,13 +65,3 @@ export const Default: Story = {
8165

8266
},
8367
};
84-
85-
export const NoValue: Story = {
86-
args: {
87-
data: undefined,
88-
keySelector,
89-
renderer: Option,
90-
rendererParams,
91-
92-
},
93-
};

packages/go-ui-storybook/src/stories/RawList.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import {
22
ListKey,
33
RawList as PureRawList,
4-
RawListProps as PureRawListProps,
4+
RawListProps,
55
} from '@ifrc-go/ui';
66

7-
interface RawListProps< DATUM, KEY extends ListKey, RENDERER_PROPS>
8-
extends PureRawListProps< DATUM, KEY, RENDERER_PROPS> {}
9-
107
function RawList
118
// eslint-disable-next-line max-len
129
<DATUM, KEY extends ListKey, RENDERER_PROPS>(props: RawListProps<DATUM, KEY, RENDERER_PROPS>) {

packages/go-ui-storybook/src/stories/ReducedListDisplay.stories.tsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const options: Option[] = [
1919
];
2020

2121
type RendererProps = {
22-
children: React.ReactNode
22+
value: React.ReactNode
2323
};
2424

2525
type ReducedListDisplaySpecificProps = ReducedListDisplayProps<Option, RendererProps>;
@@ -41,21 +41,21 @@ const meta: Meta<typeof ReducedListDisplay> = {
4141

4242
export default meta;
4343

44-
function Option({ children }: RendererProps) {
44+
function Option({ value }: RendererProps) {
4545
return (
4646
<div>
47-
{ children }
47+
{ value }
4848
</div>
4949
);
5050
}
5151

5252
const keySelector = (d: Option) => d.label;
5353

54-
const rendererParams = (option: Option) => ({ children: option.label });
54+
const rendererParams = (option: Option) => ({ value: option.label });
5555

5656
export const Default: Story = {
5757
args: {
58-
title: 'Test',
58+
title: 'Colors',
5959
list: options,
6060
keySelector,
6161
renderer: Option,
@@ -64,26 +64,16 @@ export const Default: Story = {
6464
},
6565
};
6666

67-
export const MaxItems: Story = {
67+
export const WithMaxItems: Story = {
6868
args: {
69-
title: 'Test',
70-
list: options,
71-
keySelector,
72-
renderer: Option,
73-
rendererParams,
74-
maxItems: 2,
75-
separator: ' ',
69+
...Default.args,
70+
maxItems: 3,
7671
},
7772
};
7873

79-
export const MinItems: Story = {
74+
export const WithMinItems: Story = {
8075
args: {
81-
title: 'Test',
82-
list: options,
83-
keySelector,
84-
renderer: Option,
85-
rendererParams,
86-
minItems: 1,
87-
separator: ' ',
76+
...Default.args,
77+
minItems: 3,
8878
},
8979
};

packages/go-ui-storybook/src/stories/SearchMultiSelectInput.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export const Default: Story = {
119119
},
120120
};
121121

122+
export const WithPlaceholder: Story = {
123+
render: Template,
124+
args: {
125+
placeholder: 'Select an appeal type',
126+
},
127+
};
122128
export const Disabled: Story = {
123129
render: Template,
124130
args: {

packages/go-ui-storybook/src/stories/SearchSelectInput.stories.tsx

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
useCallback,
3+
useState,
4+
} from 'react';
15
import {
26
OptionKey,
37
SearchSelectInputProps,
@@ -8,9 +12,10 @@ import type {
812
Meta,
913
StoryObj,
1014
} from '@storybook/react';
15+
import { fn } from '@storybook/test';
1116
import { isDefined } from '@togglecorp/fujs';
1217

13-
import SearchSelectInput from './ SearchSelectInput';
18+
import SearchSelectInput from './SearchSelectInput';
1419

1520
interface Option {
1621
key: string;
@@ -58,75 +63,84 @@ const meta: Meta<typeof SearchSelectInput> = {
5863
allowFullscreen: true,
5964
},
6065
},
66+
args: {
67+
onChange: fn(),
68+
},
69+
tags: ['autodocs'],
6170
};
6271

6372
export default meta;
6473

6574
function Template(args:Args) {
6675
const [
67-
{ value },
76+
{
77+
value,
78+
onChange,
79+
},
6880
setArgs,
69-
] = useArgs<{ value: OptionKey | null | undefined}>();
70-
71-
// NOTE: We are casting args as props because of discriminated union
72-
// used in SearchSelectInputProps
81+
] = useArgs();
82+
const [filteredOptions, setFilteredOptions] = useState(options);
7383

74-
const onChange = (
84+
const handleChange = useCallback((
7585
newValue: OptionKey | undefined,
7686
name: string,
7787
val: Option | undefined,
7888
) => {
89+
onChange(newValue, name, val);
7990
setArgs({ value: newValue });
80-
// eslint-disable-next-line react/destructuring-assignment
81-
if (args.nonClearable && isDefined(newValue) && isDefined(val)) {
82-
// eslint-disable-next-line react/destructuring-assignment
83-
args.onChange(newValue, name, val);
84-
} else if (isDefined(newValue) && isDefined(val)) {
85-
// eslint-disable-next-line react/destructuring-assignment
86-
args.onChange(newValue, name, val);
91+
}, [onChange, setArgs]);
92+
93+
const handleSearchValueChange = useCallback((searchText: string | undefined) => {
94+
if (isDefined(searchText)) {
95+
const newOptions = options.filter(
96+
(v) => v.label.toLowerCase().includes(searchText.toLowerCase()),
97+
);
98+
setFilteredOptions(newOptions);
99+
} else {
100+
setFilteredOptions(options);
87101
}
88-
};
102+
}, []);
89103

90104
return (
91105
<SearchSelectInput
92106
// eslint-disable-next-line react/jsx-props-no-spreading
93107
{...args}
94108
keySelector={keySelector}
95109
labelSelector={labelSelector}
96-
onChange={onChange}
110+
onChange={handleChange}
97111
value={value}
98-
name="SearchSelectInput"
112+
name="searchselectinput"
99113
options={options}
114+
onSearchValueChange={handleSearchValueChange}
115+
searchOptions={filteredOptions}
100116
selectedOnTop
101117
/>
102118
);
103119
}
120+
104121
export const Default: Story = {
105122
render: Template,
106123
};
107124

108125
export const WithPlaceholder: Story = {
109126
render: Template,
110127
args: {
111-
placeholder: 'Search',
128+
placeholder: 'Select a fruit',
112129
},
113130
};
131+
114132
export const Disabled: Story = {
115133
render: Template,
116134
args: {
135+
value: 1,
117136
disabled: true,
118137
},
119138
};
120139

121-
export const Error: Story = {
122-
render: Template,
123-
args: {
124-
error: 'This is required ',
125-
},
126-
};
127140
export const ReadOnly: Story = {
128141
render: Template,
129142
args: {
143+
value: 2,
130144
readOnly: true,
131145
},
132146
};

0 commit comments

Comments
 (0)