This gist was generated by the Svelte REPL. Visit https://svelte.technology/repl?gist=a2ae82a2c430618064864745af52cc90 to see it.
-
-
Save aldomendez/a2ae82a2c430618064864745af52cc90 to your computer and use it in GitHub Desktop.
Svelte component
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
<h1>File reader API example</h1> | |
<p>taken from an example from <a href="https://alligator.io/vuejs/file-reader-component/"> alligator</a></p> | |
<textarea rows=5 bind:value="text"></textarea> | |
<FileReader on:load="set({'text':event.content})"/> | |
<InputText textvalue="{{textvalue}}" label="Dinamic Label" on:change="set({'textvalue':event.content})" /> | |
<script> | |
import FileReader from './FileReader.html' | |
import InputText from './InputText.html' | |
export default { | |
components: {FileReader, InputText} | |
} | |
</script> |
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
{ | |
"text": "dfg", | |
"textvalue": "erterterdfgdfgsg" | |
} |
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
<label class="text-reader"> | |
Read file | |
<input type="file" on:change="loadTextFromFile(event)"> | |
</label> | |
<script> | |
export default { | |
methods: { | |
loadTextFromFile(ev) { | |
const file = ev.target.files[0] | |
const reader = new FileReader() | |
reader.onload = e => (this.fire('load',{content:e.target.result})) | |
reader.readAsText(file) | |
} | |
} | |
} | |
</script> | |
<style> | |
.text-reader { | |
position: relative; | |
overflow: hidden; | |
display: inline-block; | |
border: 2px solid black; | |
border-radius: 5px; | |
padding: 8px 12px; | |
cursor: pointer; | |
} | |
.text-reader input { | |
position: absolute; | |
top: 0; | |
left: 0; | |
z-index: -1; | |
opacity: 0; | |
} | |
</style> |
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
<label>{{label}}</label> | |
<input type="text" bind:value="textvalue" on:keyup="notify(event)"> | |
<script> | |
export default { | |
data(){ | |
return { | |
textvalue:'srtsert', | |
label:"input label" | |
} | |
}, | |
methods:{ | |
notify(ev){ | |
this.fire('change',{content:ev.target.value}) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment