UNPKG

7.68 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11var _templateObject = _taggedTemplateLiteral(['\n color: ', ';\n font-family: ', '\n font-weight: 300;\n margin: 0.5em 0;\n'], ['\n color: ', ';\n font-family: ', '\n font-weight: 300;\n margin: 0.5em 0;\n']),
12 _templateObject2 = _taggedTemplateLiteral(['\n margin: 0.75em 0 1.25em 0;\n'], ['\n margin: 0.75em 0 1.25em 0;\n']);
13
14var _head = require('lodash/head');
15
16var _head2 = _interopRequireDefault(_head);
17
18var _isObject = require('lodash/isObject');
19
20var _isObject2 = _interopRequireDefault(_isObject);
21
22var _styledComponents = require('styled-components');
23
24var _styledComponents2 = _interopRequireDefault(_styledComponents);
25
26var _react = require('react');
27
28var _react2 = _interopRequireDefault(_react);
29
30var _dropzone = require('@scrnhq/eurovision-elements/dropzone');
31
32var _dropzone2 = _interopRequireDefault(_dropzone);
33
34var _fileList = require('./file-list');
35
36var _fileList2 = _interopRequireDefault(_fileList);
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
40function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
41
42function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
43
44function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
45
46function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
47
48function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
49
50function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
51
52var ErrorMessage = _styledComponents2.default.div(_templateObject, function (props) {
53 return props.theme.warning;
54}, function (props) {
55 return props.theme.fontBase;
56});
57
58var FormDropzone = function (_Component) {
59 _inherits(FormDropzone, _Component);
60
61 function FormDropzone() {
62 var _ref;
63
64 var _temp, _this, _ret;
65
66 _classCallCheck(this, FormDropzone);
67
68 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
69 args[_key] = arguments[_key];
70 }
71
72 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = FormDropzone.__proto__ || Object.getPrototypeOf(FormDropzone)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
73 fileList: _this.props.multiple ? [] : null
74 }, _this.onDrop = function (files) {
75 var fileList = _this.props.multiple ? [].concat(_toConsumableArray(_this.state.fileList), _toConsumableArray(files)) : (0, _head2.default)(files) || null;
76
77 _this.updateFileList(fileList);
78 }, _this.onDelete = function (fileToDelete) {
79 var fileList = _this.props.multiple ? _this.state.fileList.filter(function (file) {
80 return file !== fileToDelete;
81 }) : null;
82
83 _this.updateFileList(fileList);
84 }, _temp), _possibleConstructorReturn(_this, _ret);
85 }
86
87 /**
88 * Define the proptypes of the component.
89 *
90 * @type {Object}
91 */
92
93
94 /**
95 * Default props of the component.
96 *
97 * @type {Object}
98 */
99
100
101 /**
102 * Initial state of the component.
103 *
104 * @type {Object}
105 */
106
107
108 _createClass(FormDropzone, [{
109 key: 'componentWillMount',
110
111
112 /**
113 * Invoked when the component mounts.
114 *
115 * @return {void}
116 */
117 value: function componentWillMount() {
118 var value = this.props.input.value;
119
120 if ((0, _isObject2.default)(value) && value.__typename === 'Asset') {
121 this.setState({
122 fileList: [{
123 type: value.mimeType,
124 preview: value.path
125 }]
126 });
127 }
128 }
129
130 /**
131 * Invoked when new file(s) are added.
132 *
133 * @param {Array} files
134 * @return {void}
135 */
136
137
138 /**
139 * Invoked when a file is deleted.
140 *
141 * @param {File} file
142 * @return {void}
143 */
144
145 }, {
146 key: 'updateFileList',
147
148
149 /**
150 * Update the file list in the state and via the onchange to Redux Form.
151 *
152 * @return {void}
153 */
154 value: function updateFileList(fileList) {
155 this.setState({ fileList: fileList });
156 this.props.input.onChange(this.props.multiple ? fileList.map(function (file) {
157 return { file: file };
158 }) : { file: fileList });
159 }
160
161 /**
162 * Render the component.
163 *
164 * @return {ReactElement}
165 */
166
167 }, {
168 key: 'render',
169 value: function render() {
170 var _props = this.props,
171 input = _props.input,
172 meta = _props.meta,
173 className = _props.className,
174 other = _objectWithoutProperties(_props, ['input', 'meta', 'className']);
175
176 return _react2.default.createElement(
177 'div',
178 { className: className },
179 meta.touched && meta.error && _react2.default.createElement(
180 ErrorMessage,
181 null,
182 meta.error.file ? meta.error.file[0] : meta.error[0]
183 ),
184 _react2.default.createElement(_fileList2.default, { files: this.state.fileList, onDelete: this.onDelete }),
185 _react2.default.createElement(_dropzone2.default, _extends({}, input, other, {
186 onChange: function onChange() {},
187 onDrop: this.onDrop
188 }))
189 );
190 }
191 }]);
192
193 return FormDropzone;
194}(_react.Component);
195
196FormDropzone.propTypes = {
197 multiple: _react.PropTypes.bool,
198 className: _react.PropTypes.string,
199 meta: _react.PropTypes.shape({
200 touched: _react.PropTypes.bool,
201 error: _react.PropTypes.oneOfType([_react.PropTypes.object, _react.PropTypes.array])
202 }).isRequired,
203 input: _react.PropTypes.shape({
204 onChange: _react.PropTypes.func,
205 value: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.object, _react.PropTypes.array]),
206 name: _react.PropTypes.string.isRequired
207 }).isRequired
208};
209FormDropzone.defaultProps = {
210 multiple: false
211};
212exports.default = (0, _styledComponents2.default)(FormDropzone)(_templateObject2);
\No newline at end of file