gatsby remark embed snippet inclusion

This commit is contained in:
Vishal Raj
2017-12-22 12:11:29 +05:30
parent dfa21190d3
commit 33aa0f6b4b
2 changed files with 4 additions and 28 deletions

View File

@@ -199,34 +199,9 @@ In HTML, a `<input type="file">` lets the user choose one or more files from the
In React, a `<input type="file" />` works similarly to a normal `<input/>` with some major differences. The `<input type="file" />` in React is read-only and hence setting the value of a file programmatically is impossible. You can use the various File management API provided by Javascript to handle the files that are uploaded by the user.
```javascript{7-10,17}
class FileInput extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
`embed:forms/input-type-file.js`
handleSubmit (event) {
event.preventDefault();
alert(`Selected file - ${this.fileInput.files[0].name}`);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Upload file:
<input type='file' ref={input => {this.fileInput = input}} />
</label>
<br/>
<button type='submit'>Submit</button>
</form>
);
}
}
```
[Try it on CodePen](codepen://components-and-props/input-type-file)
[Try it on CodePen](codepen://forms/input-type-file)
Note how a `ref` to the file input is used to access the file(s) in the submit handler

View File

@@ -3,7 +3,7 @@ class FileInput extends React.Component {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
// highlight-range{1-4}
handleSubmit (event) {
event.preventDefault();
alert(`Selected file - ${this.fileInput.files[0].name}`);
@@ -14,6 +14,7 @@ class FileInput extends React.Component {
<form onSubmit={this.handleSubmit}>
<label>
Upload file:
{/* highlight-next-line */}
<input type='file' ref={input => {this.fileInput = input}} />
</label>
<br/>