|
| 1 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { Button } from '../button'; |
| 4 | +import { Description, Label } from '../label'; |
| 5 | +import { UNSAFE_FileUpload as FileUpload } from './file-upload'; |
| 6 | + |
| 7 | +const meta: Meta<typeof FileUpload> = { |
| 8 | + title: 'FileUpload', |
| 9 | + component: FileUpload, |
| 10 | + parameters: { |
| 11 | + // disable built in padding in story, because we provide our own |
| 12 | + layout: 'fullscreen', |
| 13 | + }, |
| 14 | + render: () => { |
| 15 | + return ( |
| 16 | + <div className="p-4"> |
| 17 | + <FileUpload> |
| 18 | + <Label>Last opp fil</Label> |
| 19 | + <Description>Du kan laste opp én fil på opptil 10 mB.</Description> |
| 20 | + <Button className="w-fit">Velg fil</Button> |
| 21 | + </FileUpload> |
| 22 | + </div> |
| 23 | + ); |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +export default meta; |
| 28 | + |
| 29 | +type Story = StoryObj<typeof FileUpload>; |
| 30 | + |
| 31 | +export const FileUploadStory: Story = { |
| 32 | + args: {}, |
| 33 | +}; |
| 34 | + |
| 35 | +export const AllowsMultiple: Story = { |
| 36 | + render: () => { |
| 37 | + return ( |
| 38 | + <div className="p-4"> |
| 39 | + <FileUpload allowsMultiple> |
| 40 | + <Label>Last opp filer</Label> |
| 41 | + <Description> |
| 42 | + Du kan laste opp flere filer. Du kan laste de opp samtidig. |
| 43 | + </Description> |
| 44 | + <Button className="w-fit">Velg filer</Button> |
| 45 | + </FileUpload> |
| 46 | + </div> |
| 47 | + ); |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +export const LimitFileTypes: Story = { |
| 52 | + render: () => { |
| 53 | + return ( |
| 54 | + <div className="p-4"> |
| 55 | + <FileUpload acceptedFileTypes={['.pdf']}> |
| 56 | + <Label>Last opp PDF</Label> |
| 57 | + <Description>Du kan kun laste opp PDF-er.</Description> |
| 58 | + <Button className="w-fit">Velg PDF</Button> |
| 59 | + </FileUpload> |
| 60 | + </div> |
| 61 | + ); |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +export const AcceptDirectory: Story = { |
| 66 | + render: () => { |
| 67 | + return ( |
| 68 | + <div className="p-4"> |
| 69 | + <FileUpload acceptDirectory> |
| 70 | + <Label>Last opp mappe</Label> |
| 71 | + <Description>Du kan laste opp en mappe.</Description> |
| 72 | + <Button className="w-fit">Velg mappe</Button> |
| 73 | + </FileUpload> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + }, |
| 77 | +}; |
| 78 | + |
| 79 | +export const Controlled: Story = { |
| 80 | + render: () => { |
| 81 | + const [files, setFiles] = useState<File[]>([]); |
| 82 | + return ( |
| 83 | + <div className="p-4"> |
| 84 | + <FileUpload files={files} onChange={setFiles} allowsMultiple> |
| 85 | + <Label>Last opp filer</Label> |
| 86 | + <Description>Du kan laste opp flere filer.</Description> |
| 87 | + <Button className="w-fit">Velg filer</Button> |
| 88 | + </FileUpload> |
| 89 | + Filer: {files?.map((file) => file.name).join(', ')} |
| 90 | + </div> |
| 91 | + ); |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export const Required: Story = { |
| 96 | + render: () => { |
| 97 | + return ( |
| 98 | + <form |
| 99 | + encType="multipart/form-data" |
| 100 | + className="flex flex-col items-start gap-4 p-4" |
| 101 | + onSubmit={(e) => { |
| 102 | + e.preventDefault(); |
| 103 | + const formData = new FormData(e.target as HTMLFormElement); |
| 104 | + alert( |
| 105 | + `Lastet opp ${formData |
| 106 | + .getAll('files') |
| 107 | + .map((file) => (file as File).name) |
| 108 | + .join(', ')}`, |
| 109 | + ); |
| 110 | + }} |
| 111 | + > |
| 112 | + <FileUpload isRequired name="file"> |
| 113 | + <Label>Last opp medlemsbevis</Label> |
| 114 | + <Description>Du må laste opp medlemsbevis.</Description> |
| 115 | + <Button className="w-fit" variant="secondary"> |
| 116 | + Velg fil |
| 117 | + </Button> |
| 118 | + </FileUpload> |
| 119 | + <Button type="submit">Send inn</Button> |
| 120 | + </form> |
| 121 | + ); |
| 122 | + }, |
| 123 | +}; |
| 124 | + |
| 125 | +export const Validation: Story = { |
| 126 | + render: () => { |
| 127 | + return ( |
| 128 | + <div className="p-4"> |
| 129 | + <FileUpload |
| 130 | + validate={(file) => file.size < 1000000 || 'Filen er for stor'} |
| 131 | + > |
| 132 | + <Label>Last opp fil</Label> |
| 133 | + <Description>Du kan laste opp en fil på maksimalt 1 MB.</Description> |
| 134 | + <Button className="w-fit">Velg fil</Button> |
| 135 | + </FileUpload> |
| 136 | + </div> |
| 137 | + ); |
| 138 | + }, |
| 139 | +}; |
| 140 | + |
| 141 | +export const InForm = () => ( |
| 142 | + <form |
| 143 | + className="flex flex-col items-start gap-4 p-4" |
| 144 | + encType="multipart/form-data" |
| 145 | + onSubmit={(e) => { |
| 146 | + e.preventDefault(); |
| 147 | + const formData = new FormData(e.target as HTMLFormElement); |
| 148 | + alert( |
| 149 | + `Lastet opp ${formData |
| 150 | + .getAll('files') |
| 151 | + .map((file) => (file as File).name) |
| 152 | + .join(', ')}`, |
| 153 | + ); |
| 154 | + }} |
| 155 | + > |
| 156 | + <FileUpload |
| 157 | + validate={(file) => file.size < 1000000 || 'Filen er for stor'} |
| 158 | + isRequired |
| 159 | + allowsMultiple |
| 160 | + name="files" |
| 161 | + > |
| 162 | + <Label>Last opp filer</Label> |
| 163 | + <Description> |
| 164 | + Du må laste opp én fil. Filen kan ikke være større enn 1 MB |
| 165 | + </Description> |
| 166 | + <Button className="w-fit" variant="secondary"> |
| 167 | + Velg fil |
| 168 | + </Button> |
| 169 | + </FileUpload> |
| 170 | + <Button type="submit">Send inn</Button> |
| 171 | + </form> |
| 172 | +); |
0 commit comments