-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFile.jsx
More file actions
70 lines (65 loc) · 2.24 KB
/
Copy pathFile.jsx
File metadata and controls
70 lines (65 loc) · 2.24 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
import i18n from '@dhis2/d2-i18n'
import Button from '@material-ui/core/Button'
import FormControl from '@material-ui/core/FormControl'
import FormHelperText from '@material-ui/core/FormHelperText'
import PropTypes from 'prop-types'
import React from 'react'
import { Field } from 'react-final-form'
import { ErrorText } from './buildingBlocks/ErrorText.jsx'
import styles from './File.module.css'
const createChangeHandler = (input) => (event) => {
event.persist()
input.onChange({
file: event.target.files[0],
value: event.target.value,
})
}
const formatBlobToString = (data) => (data ? data.value : '')
export const File = (props) => (
<div>
<Field
name={props.name}
type="file"
placeholder={props.placeholder}
format={formatBlobToString}
{...props.fieldProps}
>
{({ input, meta, placeholder }) => (
<FormControl>
<ErrorText
error={meta.error || ''}
touched={meta.touched}
/>
<FormHelperText>{placeholder}</FormHelperText>
<FormHelperText>
<label htmlFor={input.name}>
<Button variant="contained" component="span">
{i18n.t('Select file')}
</Button>
</label>
</FormHelperText>
<input
className={styles.hiddenInput}
name={input.name}
value={input.value}
onChange={createChangeHandler(input)}
id={input.name}
type="file"
/>
<FormHelperText>
{input.value.replace('C:\\fakepath\\', '') ||
i18n.t('No file chosen')}
</FormHelperText>
</FormControl>
)}
</Field>
</div>
)
File.propTypes = {
name: PropTypes.string.isRequired,
placeholder: PropTypes.string.isRequired,
fieldProps: PropTypes.object,
}
File.defaultProps = {
fieldProps: {},
}