-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
727484d
5ba920b
0a7b3ef
fa5f68a
82713b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
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'; |
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 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"commander": "2.3.0", | ||
"component-classes": "1.2.1", | ||
"component-closest": "0.1.4", | ||
"component-file-picker": "0.2.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to do something with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, install might need to be using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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).