Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit b65aa79

Browse files
Factor out FileSelector field
1 parent fc3ccff commit b65aa79

File tree

1 file changed

+58
-0
lines changed
  • packages/builder/src/components/FileSelector

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React, { createRef } from 'react'
2+
3+
import { useField, Field } from 'formik'
4+
import { InputGroup, InputGroupAddon, InputGroupText, Button } from 'reactstrap'
5+
6+
import FileSelector from './index'
7+
import Icon from '../Icon'
8+
import { Input } from '../Form'
9+
10+
export default ({
11+
name, component, icon=undefined, placeholder='source',
12+
accept='*/*', tab
13+
}) => {
14+
const fileSelector = createRef()
15+
const [, , helpers] = useField({ name })
16+
17+
return <>
18+
<FileSelector
19+
accept={ accept }
20+
component={ component }
21+
tab={ tab }
22+
ref={ fileSelector }
23+
/>
24+
<InputGroup>
25+
{
26+
icon !== undefined
27+
? <InputGroupAddon addonType="prepend">
28+
<InputGroupText>
29+
<Icon fixedWidth icon={ icon } />
30+
</InputGroupText>
31+
</InputGroupAddon>
32+
: null
33+
}
34+
<Field
35+
name={ name }
36+
placeholder={ placeholder }
37+
component={ Input }
38+
className="text-monospace"
39+
/>
40+
<InputGroupAddon addonType="append">
41+
<Button
42+
outline color="secondary"
43+
style={{ minWidth: '3rem' }}
44+
onClick={ async () => {
45+
try {
46+
const files = await fileSelector.current.select()
47+
helpers.setValue(`\${ this.files["${ files[0].localPath }"] }`)
48+
} catch (error) {
49+
console.log('Error while selecting image', error)
50+
}
51+
} }
52+
>
53+
<Icon fixedWidth icon="folder" />
54+
</Button>
55+
</InputGroupAddon>
56+
</InputGroup>
57+
</>
58+
}

0 commit comments

Comments
 (0)