-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (153 loc) · 3.27 KB
/
Copy pathindex.js
File metadata and controls
180 lines (153 loc) · 3.27 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* External dependencies.
*/
import { Component } from '@wordpress/element';
import { get } from 'lodash';
/**
* Internal dependencies.
*/
import './style.scss';
import MediaLibrary from '../../components/media-library';
import apiFetch from '../../utils/api-fetch';
class FileField extends Component {
/**
* Local state.
*
* @type {Object}
*/
state = {
data: {}
};
/**
* Lifecycle Hook.
*
* @return {void}
*/
componentDidMount() {
const { value, field } = this.props;
if ( value ) {
// TODO: Refactor this to use `@wordpress/api-fetch` package.
apiFetch(
`${ window.wpApiSettings.root }carbon-fields/v1/attachment/?type=${ field.value_type }&value=${ value }`,
'get'
).then( ( data ) => {
this.handleFileDataChange( data[ 0 ] );
} );
}
}
/**
* Returns an URL to the attachment's thumbnail.
*
* @return {string}
*/
getThumb() {
const { data } = this.state;
if ( data.sizes ) {
const size = data.sizes.thumbnail || data.sizes.full;
if ( size ) {
return size.url;
}
}
if ( data.thumb_url ) {
return data.thumb_url;
}
return data.icon;
}
/**
* Returns the filename to the attachment thumbnail.
*
* @return {string}
*/
getFileName() {
const { data } = this.state;
return data.filename || data.file_name;
}
/**
* Handles the file meta set.
*
* @param {Object} data
* @return {void}
*/
handleFileDataChange = ( data ) => {
this.setState( { data } );
}
/**
* Handles the clear action of the file field.
*
* @return {void}
*/
handleClear = () => {
const { id, onChange } = this.props;
onChange( id, '' );
this.handleFileDataChange( {} );
}
/**
* Handles the file selection.
*
* @param {Object} files
* @return {void}
*/
handleSelect = ( files ) => {
const {
id,
field,
onChange
} = this.props;
const [ file ] = files;
onChange( id, get( file, field.value_type, file.id ) );
this.handleFileDataChange( file );
}
/**
* Render the component.
*
* @return {Object}
*/
render() {
const { data } = this.state;
const {
value,
name,
field,
buttonLabel,
mediaLibraryButtonLabel,
mediaLibraryTitle
} = this.props;
return (
<MediaLibrary
onSelect={ this.handleSelect }
multiple={ false }
title={ mediaLibraryTitle }
buttonLabel={ mediaLibraryButtonLabel }
typeFilter={ field.type_filter }
>
{
( { openMediaBrowser } ) => {
return <div className="cf-file__inner">
<input
type="hidden"
name={ name }
value={ value }
readOnly
/>
{ ( value && !! data.id ) && (
<div className="cf-file__content">
<div className="cf-file__preview">
<img src={ this.getThumb() } className="cf-file__image" />
<button type="button" className="cf-file__remove dashicons-before dashicons-no-alt" onClick={ this.handleClear }></button>
</div>
<span className="cf-file__name" title={ this.getFileName() }>
{ this.getFileName() }
</span>
</div>
) }
<button type="button" className="button cf-file__browse" onClick={ openMediaBrowser }>
{ buttonLabel }
</button>
</div>;
}
}
</MediaLibrary>
);
}
}
export default FileField;