-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathCardSelectableIndicator.stories.tsx
More file actions
211 lines (189 loc) · 6.75 KB
/
Copy pathCardSelectableIndicator.stories.tsx
File metadata and controls
211 lines (189 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as React from 'react';
import type { JSXElement } from '@fluentui/react-components';
import { makeStyles, Button, Caption1, tokens, Checkbox, Text, useId } from '@fluentui/react-components';
import { MoreHorizontal20Regular } from '@fluentui/react-icons';
import { Card, CardHeader, CardPreview } from '@fluentui/react-components';
const resolveAsset = (asset: string) => {
const ASSET_URL =
'https://raw.githubusercontent.com/microsoft/fluentui/master/packages/react-components/react-card/stories/src/assets/';
return `${ASSET_URL}${asset}`;
};
const flex = {
gap: '16px',
display: 'flex',
};
const useStyles = makeStyles({
main: {
...flex,
flexDirection: 'column',
},
row: {
...flex,
flexWrap: 'wrap',
},
card: {
maxWidth: '400px',
width: '100%',
height: 'fit-content',
},
caption: {
color: tokens.colorNeutralForeground3,
},
smallRadius: { borderRadius: tokens.borderRadiusSmall },
grayBackground: {
backgroundColor: tokens.colorNeutralBackground3,
},
logoBadge: {
padding: '5px',
borderRadius: tokens.borderRadiusSmall,
backgroundColor: '#FFF',
boxShadow: '0px 1px 2px rgba(0, 0, 0, 0.14), 0px 0px 2px rgba(0, 0, 0, 0.12)',
},
actions: {
display: 'flex',
},
});
export const SelectableIndicator = (): JSXElement => {
const styles = useStyles();
const idPrefix = useId('card-title');
const [selected1, setSelected1] = React.useState(false);
const [selected2, setSelected2] = React.useState(false);
const [selected3, setSelected3] = React.useState(false);
const [selected4, setSelected4] = React.useState(false);
const setCheckboxState = React.useCallback(
(
{ selected, checked }: { selected?: boolean; checked?: boolean | 'mixed' },
setFn: React.Dispatch<React.SetStateAction<boolean>>,
) => setFn(!!(selected || checked)),
[],
);
const onSelected1Change = React.useCallback(
(
_: React.MouseEvent | React.KeyboardEvent | React.ChangeEvent,
state: { selected?: boolean; checked?: boolean | 'mixed' },
) => setCheckboxState(state, setSelected1),
[setCheckboxState],
);
const onSelected2Change = React.useCallback(
(
_: React.MouseEvent | React.KeyboardEvent | React.ChangeEvent,
state: { selected?: boolean; checked?: boolean | 'mixed' },
) => setCheckboxState(state, setSelected2),
[setCheckboxState],
);
const onSelected3Change = React.useCallback(
(
_: React.MouseEvent | React.KeyboardEvent | React.ChangeEvent,
state: { selected?: boolean; checked?: boolean | 'mixed' },
) => setCheckboxState(state, setSelected3),
[setCheckboxState],
);
const onSelected4Change = React.useCallback(
(
_: React.MouseEvent | React.KeyboardEvent | React.ChangeEvent,
state: { selected?: boolean; checked?: boolean | 'mixed' },
) => setCheckboxState(state, setSelected4),
[setCheckboxState],
);
return (
<div className={styles.main}>
<div className={styles.row}>
<Card
className={styles.card}
floatingAction={
<Checkbox aria-labelledby={`${idPrefix}-1`} onChange={onSelected1Change} checked={selected1} />
}
selected={selected1}
onSelectionChange={onSelected1Change}
>
<CardPreview
className={styles.grayBackground}
logo={<img className={styles.logoBadge} src={resolveAsset('logo3.svg')} alt="Figma app logo" />}
>
<img className={styles.smallRadius} src={resolveAsset('office1.png')} alt="Presentation Preview" />
</CardPreview>
<CardHeader
header={
<Text id={`${idPrefix}-1`} weight="semibold">
iOS App Prototype
</Text>
}
description={<Caption1 className={styles.caption}>You created 53m ago</Caption1>}
action={<Button appearance="transparent" icon={<MoreHorizontal20Regular />} aria-label="More actions" />}
/>
</Card>
<Card
className={styles.card}
floatingAction={
<Checkbox aria-labelledby={`${idPrefix}-2`} onChange={onSelected2Change} checked={selected2} />
}
selected={selected2}
onSelectionChange={onSelected2Change}
>
<CardPreview
className={styles.grayBackground}
logo={<img className={styles.logoBadge} src={resolveAsset('logo3.svg')} alt="Figma app logo" />}
>
<img className={styles.smallRadius} src={resolveAsset('office1.png')} alt="Presentation Preview" />
</CardPreview>
<CardHeader
header={
<Text id={`${idPrefix}-2`} weight="semibold">
iOS App Prototype
</Text>
}
description={<Caption1 className={styles.caption}>You created 53m ago</Caption1>}
action={<Button appearance="transparent" icon={<MoreHorizontal20Regular />} aria-label="More actions" />}
/>
</Card>
</div>
<div className={styles.row}>
<Card
className={styles.card}
selected={selected3}
onSelectionChange={onSelected3Change}
floatingAction={
<Checkbox aria-labelledby={`${idPrefix}-3`} onChange={onSelected3Change} checked={selected3} />
}
>
<CardHeader
image={<img src={resolveAsset('docx.png')} alt="Microsoft Word Logo" />}
header={
<Text id={`${idPrefix}-3`} weight="semibold">
Secret Project Briefing
</Text>
}
description={<Caption1 className={styles.caption}>OneDrive > Documents</Caption1>}
/>
</Card>
<Card
className={styles.card}
selected={selected4}
onSelectionChange={onSelected4Change}
floatingAction={
<Checkbox aria-labelledby={`${idPrefix}-4`} onChange={onSelected4Change} checked={selected4} />
}
>
<CardHeader
image={<img src={resolveAsset('xlsx.png')} alt="Microsoft Excel Logo" />}
header={
<Text id={`${idPrefix}-4`} weight="semibold">
Team Budget
</Text>
}
description={<Caption1 className={styles.caption}>OneDrive > Spreadsheets</Caption1>}
/>
</Card>
</div>
</div>
);
};
SelectableIndicator.parameters = {
docs: {
description: {
story: `By default, selectable cards do not include any element to represent its selection state. For example,
checkboxes can be composed together as an additional element by using the \`floatingAction\` property.
When doing so, ensure the checkbox's accessible name matches the card's title.`,
},
},
};