Skip to content

Commit eeec04e

Browse files
author
Nikolai Lopin
committed
docs: migrate FilePicker docs to csf3
1 parent 2a3d688 commit eeec04e

File tree

4 files changed

+129
-128
lines changed

4 files changed

+129
-128
lines changed

src/components/FilePicker/docs/ControlledErrorFilePicker.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,16 @@ import { HelperText } from '../../HelperText/HelperText';
55

66
const ControlledErrorFilePicker: FC = () => {
77
const [error, setError] = useState(false);
8-
const onError = file => {
9-
// eslint-disable-next-line no-console
10-
console.log('onError', file);
11-
setError(!!file);
12-
};
138

149
return (
1510
<Box maxWidth="100%" width="30rem">
1611
<FilePicker
17-
accept="image/png, image/jpeg"
1812
buttonText="Browse files"
1913
label="A picture of you"
2014
name="avatar"
2115
error={error}
22-
onChange={e => {
23-
// eslint-disable-next-line no-console
24-
console.log('onChange', e);
25-
}}
26-
onFileChange={(eventFile, e) => {
27-
// eslint-disable-next-line no-console
28-
console.log('onFileChange', eventFile, e);
29-
try {
30-
if (eventFile.size > 0) {
31-
throw new Error('Oops! I need a file with no size');
32-
}
33-
} catch {
34-
onError(eventFile);
35-
}
16+
onFileChange={() => {
17+
setError(true);
3618
}}
3719
/>
3820
{error && (

src/components/FilePicker/docs/FilePicker.stories.mdx

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
import { Meta, StoryObj } from '@storybook/react';
3+
import { FilePicker } from '../FilePicker';
4+
import { Box } from '../../Box/Box';
5+
6+
const meta: Meta = {
7+
title: 'Form Elements/FilePicker',
8+
component: FilePicker,
9+
argTypes: {
10+
capture: {
11+
options: ['user', 'environment'],
12+
control: 'select'
13+
}
14+
},
15+
args: {
16+
name: 'avatar',
17+
label: 'Picture of you',
18+
buttonText: 'Browse'
19+
}
20+
};
21+
22+
export default meta;
23+
24+
type Story = StoryObj<typeof FilePicker>;
25+
26+
export const Default: Story = {};
27+
28+
export const WithBrowserButtonAlwaysVisible: Story = {
29+
args: {
30+
alwaysShowActionButton: true
31+
}
32+
};
33+
34+
export const WithMultilineLabel: Story = {
35+
args: {
36+
label: 'A very long label which can stretch to more than one line without any issues'
37+
},
38+
decorators: [
39+
Story => (
40+
<Box width="20rem">
41+
<Story />
42+
</Box>
43+
)
44+
]
45+
};
46+
47+
export const AcceptingOnlyImages: Story = {
48+
args: {
49+
accept: 'image/*'
50+
}
51+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Primary, Stories, ArgTypes, Meta } from '@storybook/blocks';
2+
import { ControlledErrorFilePicker } from './ControlledErrorFilePicker';
3+
import { StyledSystemLinks } from '../../../docs/StyledSystemLinks';
4+
import * as FilePickerStories from './FilePicker.stories';
5+
6+
<Meta of={FilePickerStories} />
7+
8+
# FilePicker
9+
10+
This component delivers an optimal user experience by utilizing core web technologies. It uses the [HTML filepicker](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file) for a lightweight and efficient approach to file uploading.
11+
12+
<Primary />
13+
14+
## Properties
15+
16+
<ArgTypes of={FilePickerStories} />
17+
18+
<StyledSystemLinks component="FilePicker" supportedProps={['margin']} />
19+
20+
## Appearance
21+
22+
- **File title**: This provides a clear description of what the user should upload. If long, takes as many lines as needed extending the height of the component.
23+
- **Button label**: Describes the main action, it works better when short
24+
25+
## Placement
26+
27+
- They are mainly used in forms but can also appear in other places as a standalone component. In either case, prefer to have them inside `<form>` tag for better user experience.
28+
- Place `Filepicker` with a minimum of 8px _("Spaces" value 1 for our default spaces values)_ margin on each side.
29+
30+
## Behaviour
31+
32+
- The user may only select 1 file at a time.
33+
- By default, any file format is accepted, but you can add `accept` property to validate a specific file format.
34+
- Click on "Browse" button triggers a browser-specific upload window.
35+
- Once the user selects the file, the browser-specific select window closes and the files will appear below the File title label.
36+
37+
## Usage
38+
39+
### Showing errors
40+
41+
Below you have an example of how you can show an error using the [`HelperText` component](?path=/docs/components-helpertext--docs).
42+
Submit any file to trigger the error and clean the input by canceling the selection to remove the error state.
43+
44+
<ControlledErrorFilePicker />
45+
46+
```tsx
47+
const UploadAvatar = () => {
48+
const [error, setError] = useState<string>();
49+
50+
return (
51+
<Box>
52+
<FilePicker
53+
buttonText="Browse files"
54+
label="A picture of you"
55+
name="avatar"
56+
error={error}
57+
onFileChange={eventFile => {
58+
try {
59+
setError(undefined);
60+
// here we make something that can throw
61+
} catch (error) {
62+
setError(error.message);
63+
}
64+
}}
65+
/>
66+
{error && (
67+
<HelperText mt={1} variant="danger">
68+
{error}
69+
</HelperText>
70+
)}
71+
</Box>
72+
);
73+
};
74+
```
75+
76+
<Stories includePrimary={false} />

0 commit comments

Comments
 (0)