Last active
May 15, 2020 12:20
-
-
Save padcom/ccbd7d5cdc0c882573081ae39dd72b50 to your computer and use it in GitHub Desktop.
CWP Episode 2 - Creating a DynamicTable component with Vue.js - https://youtu.be/LNRKVo6vhxk
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Editor component</title> | |
<script src="https://unpkg.com/vue"></script> | |
</head> | |
<body> | |
<div id="app"> | |
<h3>{{ message }}</h3> | |
<p> | |
<input-with-label | |
name="input-with-label" | |
label="Message" | |
v-model="message" | |
@focus="focused"> | |
</input-with-label> | |
</p> | |
<p> | |
<message-editor | |
name="message-editor" | |
label="Message" | |
v-model="message" | |
@focus="focused"> | |
</message-editor> | |
</p> | |
</div> | |
<template id="input-with-label"> | |
<label> | |
{{ label }} | |
<input v-bind="$attrs" v-on="listeners"> | |
</label> | |
</template> | |
<script> | |
Vue.component('input-with-label', { | |
template: '#input-with-label', | |
inheritAttrs: false, | |
props: { | |
label: { type: String, default: 'Label' } | |
}, | |
computed: { | |
listeners() { | |
return Object.assign({}, this.$listeners, { | |
input: e => { this.$emit('input', e.target.value) } | |
}) | |
} | |
} | |
}) | |
</script> | |
<template id="message-editor"> | |
<label> | |
{{ label }} | |
<input v-bind="$attrs" v-on="listeners" v-model="internalValue"> | |
<button @click="$emit('input', internalValue)">{{ button }}</button> | |
</label> | |
</template> | |
<script> | |
Vue.component('message-editor', { | |
template: '#message-editor', | |
inheritAttrs: false, | |
props: { | |
label: { type: String, default: 'Label' }, | |
button: { type: String, default: 'Save' }, | |
value: { type: String, required: true }, | |
}, | |
data() { | |
return { | |
internalValue: this.value | |
} | |
}, | |
computed: { | |
listeners() { | |
return Object.assign({}, this.$listeners, { | |
input: () => {} | |
}) | |
} | |
}, | |
watch: { | |
value(newValue) { | |
this.internalValue = newValue | |
} | |
} | |
}) | |
</script> | |
<script> | |
new Vue({ | |
el: '#app', | |
data: { | |
message: 'Hello, world!' | |
}, | |
methods: { | |
focused(e) { | |
console.log('Focused!', e.target.name) | |
} | |
} | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment