|
@@ -1,5 +1,6 @@
|
|
|
const { h, Component } = require('preact')
|
|
|
const classNames = require('classnames')
|
|
|
+const cuid = require('cuid')
|
|
|
const getFileTypeIcon = require('../../utils/getFileTypeIcon')
|
|
|
const ignoreEvent = require('../../utils/ignoreEvent.js')
|
|
|
const FilePreview = require('../FilePreview')
|
|
@@ -19,15 +20,8 @@ class FileCard extends Component {
|
|
|
this.state = {
|
|
|
formState: storedMetaData,
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- saveOnEnter = (ev) => {
|
|
|
- if (ev.keyCode === 13) {
|
|
|
- ev.stopPropagation()
|
|
|
- ev.preventDefault()
|
|
|
- const file = this.props.files[this.props.fileCardFor]
|
|
|
- this.props.saveFileCard(this.state.formState, file.id)
|
|
|
- }
|
|
|
+ this.form.id = cuid()
|
|
|
}
|
|
|
|
|
|
updateMeta = (newVal, name) => {
|
|
@@ -39,7 +33,10 @@ class FileCard extends Component {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- handleSave = () => {
|
|
|
+ form = document.createElement('form');
|
|
|
+
|
|
|
+ handleSave = (e) => {
|
|
|
+ e.preventDefault()
|
|
|
const fileID = this.props.fileCardFor
|
|
|
this.props.saveFileCard(this.state.formState, fileID)
|
|
|
}
|
|
@@ -48,6 +45,17 @@ class FileCard extends Component {
|
|
|
this.props.toggleFileCard(false)
|
|
|
}
|
|
|
|
|
|
+ // TODO(aduh95): move this to `UNSAFE_componentWillMount` when updating to Preact X+.
|
|
|
+ componentWillMount () {
|
|
|
+ this.form.addEventListener('submit', this.handleSave)
|
|
|
+ document.body.appendChild(this.form)
|
|
|
+ }
|
|
|
+
|
|
|
+ componentWillUnmount () {
|
|
|
+ this.form.removeEventListener('submit', this.handleSave)
|
|
|
+ document.body.removeChild(this.form)
|
|
|
+ }
|
|
|
+
|
|
|
renderMetaFields = () => {
|
|
|
const metaFields = this.getMetaFields() || []
|
|
|
const fieldCSSClasses = {
|
|
@@ -56,6 +64,7 @@ class FileCard extends Component {
|
|
|
|
|
|
return metaFields.map((field) => {
|
|
|
const id = `uppy-Dashboard-FileCard-input-${field.id}`
|
|
|
+ const required = this.props.requiredMetaFields.includes(field.id)
|
|
|
return (
|
|
|
<fieldset key={field.id} className="uppy-Dashboard-FileCard-fieldset">
|
|
|
<label className="uppy-Dashboard-FileCard-label" htmlFor={id}>{field.name}</label>
|
|
@@ -64,17 +73,18 @@ class FileCard extends Component {
|
|
|
value: this.state.formState[field.id],
|
|
|
onChange: (newVal) => this.updateMeta(newVal, field.id),
|
|
|
fieldCSSClasses,
|
|
|
+ required,
|
|
|
+ form: this.form.id,
|
|
|
}, h)
|
|
|
: (
|
|
|
<input
|
|
|
className={fieldCSSClasses.text}
|
|
|
id={id}
|
|
|
+ form={this.form.id}
|
|
|
type={field.type || 'text'}
|
|
|
+ required={required}
|
|
|
value={this.state.formState[field.id]}
|
|
|
placeholder={field.placeholder}
|
|
|
- onKeyUp={this.saveOnEnter}
|
|
|
- onKeyDown={this.saveOnEnter}
|
|
|
- onKeyPress={this.saveOnEnter}
|
|
|
onInput={ev => this.updateMeta(ev.target.value, field.id)}
|
|
|
data-uppy-super-focusable
|
|
|
/>
|
|
@@ -112,6 +122,7 @@ class FileCard extends Component {
|
|
|
<button
|
|
|
className="uppy-DashboardContent-back"
|
|
|
type="button"
|
|
|
+ form={this.form.id}
|
|
|
title={this.props.i18n('finishEditingFile')}
|
|
|
onClick={this.handleCancel}
|
|
|
>
|
|
@@ -128,6 +139,7 @@ class FileCard extends Component {
|
|
|
type="button"
|
|
|
className="uppy-u-reset uppy-c-btn uppy-Dashboard-FileCard-edit"
|
|
|
onClick={() => this.props.openFileEditor(file)}
|
|
|
+ form={this.form.id}
|
|
|
>
|
|
|
{this.props.i18n('editFile')}
|
|
|
</button>
|
|
@@ -141,8 +153,8 @@ class FileCard extends Component {
|
|
|
<div className="uppy-Dashboard-FileCard-actions">
|
|
|
<button
|
|
|
className="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Dashboard-FileCard-actionsBtn"
|
|
|
- type="button"
|
|
|
- onClick={this.handleSave}
|
|
|
+ type="submit"
|
|
|
+ form={this.form.id}
|
|
|
>
|
|
|
{this.props.i18n('saveChanges')}
|
|
|
</button>
|
|
@@ -150,6 +162,7 @@ class FileCard extends Component {
|
|
|
className="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-FileCard-actionsBtn"
|
|
|
type="button"
|
|
|
onClick={this.handleCancel}
|
|
|
+ form={this.form.id}
|
|
|
>
|
|
|
{this.props.i18n('cancel')}
|
|
|
</button>
|