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

Components: add FilePicker component #3971

Merged
merged 5 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions client/components/file-picker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FilePicker
==========

This component opens a native file picker when its children are clicked on.
It is a very thin wrapper around
[`component/file-picker`](https://github.com/component/file-picker)

#### How to use:

```js
var FilePicker = require( 'components/file-picker' );

render: function() {
return (
<FilePicker multiple accept="image/*" onPick={ console.log.bind(console) } >
<a href="#">Select a few images!</a>
</FilePicker>
);
}
```

#### Props

* `multiple`: (bool) Allow selecting multiple files (Defaults to `false`).
* `directory`: (bool) Allow selecting of a directory (Defaults to `false`).
* `accept`: (string) Content type MIME to accept. Wildcards (`*`) are supported.
* `onPick`: (func) Function to call when the user has selected one or more files.

68 changes: 68 additions & 0 deletions client/components/file-picker/docs/example.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* External dependencies
*/
import React from 'react';

/**
* Internal dependencies
*/
import Card from 'components/card';
import Button from 'components/button';
import FilePicker from 'components/file-picker';

export default class FilePickers extends React.Component {
constructor( props ) {
super( props );
}

onSingle( files ) {
alert( 'Selected file: ' + JSON.stringify( files[0].name ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe switch these to use debug ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You think so? For a doc example I thought it would be good to see the "results" of the user action kind of in-your-face. Gridicons uses prompt() for example, which is similar.

We should have some standard way of displaying results in the docs (a built-in log-dump thing would be cool).

}

onMulti( files ) {
alert( 'Selected files:\n' + [].slice.call( files ).map( ( file ) => {
return ' ' + JSON.stringify( file.name );
} ).join( '\n' ) );
}

render() {
return (
<div className="design-assets__group">
<h2>
<a href="/devdocs/design/file-pickers">File Picker</a>
</h2>

<Card>

<h4>Select a single file:</h4>
<FilePicker onPick={ this.onSingle } >
<Button>Single Item</Button>
</FilePicker>

<h4>Select a multiple files:</h4>
<FilePicker multiple onPick={ this.onMulti } >
<Button>Multiple Items</Button>
</FilePicker>

<h4>Select a directory:</h4>
<FilePicker directory onPick={ this.onMulti } >
<Button>Directory</Button>
</FilePicker>

<h4>Select an image file:</h4>
<FilePicker accept="image/*" onPick={ this.onSingle } >
<Button>JPEG / PNG / GIF</Button>
</FilePicker>

<h4>Any internal content works:</h4>
<FilePicker onPick={ this.onSingle } >
<a href="#">Select File…</a>
</FilePicker>

</Card>
</div>
);
}
}

FilePickers.displayName = 'FilePickers';
44 changes: 44 additions & 0 deletions client/components/file-picker/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** @ssr-ready **/

/**
* External dependencies
*/
import React from 'react';
import assign from 'lodash/assign';
import noop from 'lodash/noop';
import pick from 'component-file-picker';

export default class FilePicker extends React.Component {
constructor( props ) {
super( props );
this.showPicker = this.showPicker.bind( this );
}

showPicker() {
pick( assign( {}, this.props ), this.props.onPick );
}

render() {
return (
<span className="file-picker" onClick={ this.showPicker } >
{ this.props.children }
</span>
);
}
}

FilePicker.displayName = 'FilePicker';

FilePicker.propTypes = {
multiple: React.PropTypes.bool,
directory: React.PropTypes.bool,
accept: React.PropTypes.string,
onPick: React.PropTypes.func
};

FilePicker.defaultProps = {
multiple: false,
directory: false,
accept: null,
onPick: noop
};
2 changes: 2 additions & 0 deletions client/devdocs/design/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Version from 'components/version/docs/example';
import BulkSelect from 'components/bulk-select/docs/example';
import ExternalLink from 'components/external-link/docs/example';
import FeatureGate from 'components/feature-example/docs/example';
import FilePickers from 'components/file-picker/docs/example';
import Collection from 'devdocs/design/search-collection';

export default React.createClass( {
Expand Down Expand Up @@ -94,6 +95,7 @@ export default React.createClass( {
<DropZones searchKeywords="drag" />
<ExternalLink />
<FeatureGate />
<FilePickers />
<FoldableCard />
<FormFields searchKeywords="input textbox textarea radio"/>
<Gauge />
Expand Down
8 changes: 8 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"commander": "2.3.0",
"component-classes": "1.2.1",
"component-closest": "0.1.4",
"component-file-picker": "0.2.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I need to do something with npm-shrinkwrap.json about this?

Copy link
Contributor

Choose a reason for hiding this comment

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

yup, install clingwrap and clingwrap component-file-picker.

might need to be using npm@2.14.12 when you do it too? that bit is still unclear to me. @gwwar might be able to shed some light on it.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/Automattic/wp-calypso/blob/master/docs/shrinkwrap.md#modifying-dependencies

And we're currently using node 4.3.0 with npm 2.14.12

Feel free to ping me, if the instructions don't make sense

"component-tip": "2.5.0",
"component-uid": "0.0.2",
"cookie": "0.1.2",
Expand Down