Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileField: show image thumbnails [WiP] #3397

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions fields/types/file/FileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@ import { Button, FormField, FormInput, FormNote } from 'elemental';
import FileChangeMessage from '../../components/FileChangeMessage';
import HiddenFileInput from '../../components/HiddenFileInput';

const FileThumb = ({ url }) => {
const isPicture = url && url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
if (!isPicture) {
// TODO generic icons
return false;
}
return (
<div style={{ width: 150, marginRight: 10, flexShrink: 0 }}>
<img style={{ width: '100%', height: '100%' }} src={url}/>
</div>
);
};

const FileDom = ({ url, filename }) => {
return (
<div style={{ display: 'flex' }}>
<FileThumb {...{ url }}/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait. why are you sending an object containing url when only url is interesting to the FileThumb component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adnasa This is just React, it's exactly the same as saying <FileThumb url={url}/> and gets transpiled to the exact same React.createElement call.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth changing that to <FileThumb url={url} /> – I wasn't 100% sure what that did either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping that JSX will allow writing <FileThumb {url}/>, see facebook/jsx#23

Would that have been less confusing? If so, some upvotes would be nice :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it is a single prop

<Component singleProp={value} />

is much more readable than

<Component {...{ singleProp } />

<div style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'flex-start',
minHeight: 100,
width: '100%',
}}>
<FileChangeMessage>
{url ? (
<a href={url}>{filename}</a>
) : (
filename
)}
</FileChangeMessage>
{url && (
<span style={{ fontSize: 10 }}>
url: {url}
</span>
)}
</div>
</div>
);
};

let uploadInc = 1000;

const buildInitialState = (props) => ({
Expand Down Expand Up @@ -43,8 +85,10 @@ module.exports = Field.create({
return this.props.collapse && !this.hasExisting();
},
componentWillUpdate (nextProps) {
const value = this.props.value || {};
const nextVal = nextProps.value || {};
// Show the new filename when it's finished uploading
if (this.props.value.filename !== nextProps.value.filename) {
if (value.filename !== nextVal.filename) {
this.setState(buildInitialState(nextProps));
}
},
Expand All @@ -60,17 +104,19 @@ module.exports = Field.create({
return this.props.value && !!this.props.value.filename;
},
getFilename () {
return this.state.userSelectedFile
? this.state.userSelectedFile.name
: this.props.value.filename;
const { value } = this.props;
const { userSelectedFile } = this.state;
return userSelectedFile
? userSelectedFile.name
: value && (value.originalname || value.filename);
},

// ==============================
// METHODS
// ==============================

triggerFileBrowser () {
this.refs.fileInput.clickDomNode();
this.fileInput && this.fileInput.clickDomNode();
},
handleFileChange (event) {
const userSelectedFile = event.target.files[0];
Expand Down Expand Up @@ -113,14 +159,16 @@ module.exports = Field.create({
// ==============================

renderFileNameAndChangeMessage () {
const href = this.props.value ? this.props.value.url : undefined;
const { value } = this.props;
let url;
let filename;
if (this.hasFile() && !this.state.removeExisting) {
url = value && value.url;
filename = this.getFilename();
}
return (
<div>
{(this.hasFile() && !this.state.removeExisting) ? (
<FileChangeMessage href={href} target="_blank">
{this.getFilename()}
</FileChangeMessage>
) : null}
{filename && <FileDom {...{ url, filename }}/>}
{this.renderChangeMessage()}
</div>
);
Expand Down Expand Up @@ -202,15 +250,17 @@ module.exports = Field.create({
key={this.state.uploadFieldPath}
name={this.state.uploadFieldPath}
onChange={this.handleFileChange}
ref="fileInput"
ref={el => { this.fileInput = el; }}
/>
{this.renderActionInput()}
</div>
) : (
<div>
{this.hasFile()
? this.renderFileNameAndChangeMessage()
: <FormInput noedit>no file</FormInput>}
{this.hasFile() ? (
this.renderFileNameAndChangeMessage()
) : (
<FormInput noedit>no file</FormInput>
)}
</div>
)}
{!!this.props.note && <FormNote note={this.props.note} />}
Expand Down