Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created May 2, 2018 14:47
Show Gist options
  • Save calebporzio/4c2a2a71bbb18ecba02d698494946688 to your computer and use it in GitHub Desktop.
Save calebporzio/4c2a2a71bbb18ecba02d698494946688 to your computer and use it in GitHub Desktop.
Little Vue component for turning JSON into native HTML for inputs
<script>
import _ from 'lodash'
export default {
props: [ 'name', 'value' ],
methods: {
flatInputsFromDeepJson(item, key, h) {
if (typeof item === 'object') {
return _.flatMapDeep(item, (value, newKey) => {
return this.flatInputsFromDeepJson(value, key.concat(newKey), h)
})
}
return h('input', { attrs: {
class: 'hidden',
type: 'text',
name: this.name + this.wrapInBrackets(key),
value: item,
}})
},
wrapInBrackets(keys) {
return keys.length
? '[' + Array.from(keys).join('][') + ']'
: ''
}
},
render(h) {
return h('div', [
this.flatInputsFromDeepJson(this.value, [], h)
])
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment