|
| 1 | +--- |
| 2 | +title: FileTrigger |
| 3 | +description: Åtkomst till filsystemet |
| 4 | +--- |
| 5 | + |
| 6 | +import { Button } from '@midas-ds/components' |
| 7 | +import { FileTrigger } from 'react-aria-components' |
| 8 | +import { ComponentHeader } from '@site/src/components/getComponentMetaData' |
| 9 | +import { Example, DropZoneExample } from '@site/src/components/examples/file-upload/FileUploadExamples' |
| 10 | +import { PropTable } from '@site/src/components/PropsTable' |
| 11 | + |
| 12 | +<ComponentHeader |
| 13 | + name='FileTrigger' |
| 14 | + friendlyName='Filuppladdning, filväljare' |
| 15 | + overrideHeadlessLink='https://react-spectrum.adobe.com/react-aria/FileTrigger.html' |
| 16 | +/> |
| 17 | + |
| 18 | +[FileTrigger](https://react-spectrum.adobe.com/react-aria/FileTrigger.html) från React Aria gör det möjligt för en användare |
| 19 | +att komma åt filsystemet via valfri tryckbar React Aria-komponent, som till exempel [Button](/components/button). |
| 20 | +Se [API](#api) eller [officiell dokumentation](https://react-spectrum.adobe.com/react-aria/FileTrigger.html) för hur den kan användas. |
| 21 | +FileTrigger är en komponent utan utseende och går att använda direkt från React Aria eller importeras från Midas. DropZone |
| 22 | +har en enkelt stylad yta men med samma beteende som React Aria. |
| 23 | + |
| 24 | +```tsx |
| 25 | + |
| 26 | +import { Button } from '@midas-ds/components' |
| 27 | +import { FileTrigger } from 'react-aria-components' |
| 28 | +import React from 'react' |
| 29 | + |
| 30 | +export const Example = () => { |
| 31 | + let [file, setFile] = React.useState(null); |
| 32 | + |
| 33 | + return ( |
| 34 | + <> |
| 35 | + <FileTrigger |
| 36 | + onSelect={(e) => { |
| 37 | + let files = e ? Array.from(e) : []; |
| 38 | + let filenames = files.map((file) => file.name); |
| 39 | + setFile(filenames); |
| 40 | + }}> |
| 41 | + <Button>Välj fil</Button> |
| 42 | + </FileTrigger> |
| 43 | + {file && file} |
| 44 | + </> |
| 45 | + ) |
| 46 | +} |
| 47 | +``` |
| 48 | +<div className={'card'}> |
| 49 | + <Example/> |
| 50 | +</div> |
| 51 | + |
| 52 | +## Varianter |
| 53 | + |
| 54 | +Se [API](#api) samt [React Aria](https://react-spectrum.adobe.com/react-aria/FileTrigger.html) för samtliga |
| 55 | +möjligheter. |
| 56 | + |
| 57 | +### Drag & drop |
| 58 | + |
| 59 | +Använd FileTrigger tillsammans med [DropZone](https://react-spectrum.adobe.com/react-aria/DropZone.html) |
| 60 | +för att tillåta val av filer via drag & drop. |
| 61 | + |
| 62 | +```tsx |
| 63 | +export const DropZoneExample = () => { |
| 64 | + let [files, setFiles] = React.useState(null); |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <DropZone |
| 68 | + onDrop={(e) => { |
| 69 | + let files = e.items.filter((file) => |
| 70 | + file.kind === 'file' |
| 71 | + ) as FileDropItem[]; |
| 72 | + let filenames = files.map((file) => file.name); |
| 73 | + setFiles(filenames.join(', ')); |
| 74 | + }} |
| 75 | + > |
| 76 | + <FileTrigger |
| 77 | + allowsMultiple |
| 78 | + onSelect={(e) => { |
| 79 | + let files = Array.from(e); |
| 80 | + let filenames = files.map((file) => file.name); |
| 81 | + setFiles(filenames.join(', ')); |
| 82 | + }} |
| 83 | + > |
| 84 | + <Button>Select files</Button> |
| 85 | + </FileTrigger> |
| 86 | + <Text slot="label" style={{ display: 'block' }}> |
| 87 | + {files || 'Drop files here'} |
| 88 | + </Text> |
| 89 | + </DropZone> |
| 90 | + </> |
| 91 | + ) |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +<DropZoneExample/> |
| 96 | + |
| 97 | +### Alternativ |
| 98 | + |
| 99 | +För att tillåta användaren att ladda upp flera filer på samma gång, använd attributet `allowsMultiple`, för att |
| 100 | +begränsa valbara filtyper, använd `acceptedFileTypes`. |
| 101 | + |
| 102 | +### Media Capture |
| 103 | + |
| 104 | +På mobila enheter kan användaren använda enhetens kamera för att välja media. `defaultCamera` väljer vilken kamera som |
| 105 | +används. |
| 106 | + |
| 107 | +```tsx |
| 108 | +<FileTrigger defaultCamera={'environment'}> |
| 109 | + <Button>Öppna kameran</Button> |
| 110 | +</FileTrigger> |
| 111 | +``` |
| 112 | + |
| 113 | +<div className='card'> |
| 114 | + <FileTrigger defaultCamera={'environment'}> |
| 115 | + <Button>Öppna kameran</Button> |
| 116 | + </FileTrigger> |
| 117 | +</div> |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +## API |
| 122 | + |
| 123 | +### FileTrigger |
| 124 | +<PropTable name='FileTrigger' /> |
| 125 | + |
| 126 | +### DropZone |
| 127 | +<PropTable name='DropZone' /> |
| 128 | + |
| 129 | + |
0 commit comments