-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3971 from Automattic/add/component-file-picker
Components: add `FilePicker` component
- Loading branch information
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
|
||
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters