Last active
December 2, 2019 22:10
-
-
Save jakedohm/cd836752acf3396c282478ee3e32f988 to your computer and use it in GitHub Desktop.
Basic concept/idea for managing forms in Vue
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
<template> | |
<div class="input-group"> | |
<label class="input-label"><slot></slot></label> | |
<input | |
class="input" | |
v-model="fieldValue" | |
:type="type" | |
:class="inputClass" | |
/> | |
</div> | |
</template> | |
<script> | |
export default { | |
props: { | |
name: null, | |
value: null, | |
type: { | |
type: String, | |
default: 'text', | |
}, | |
inputClass: { | |
type: String, | |
default: '', | |
}, | |
}, | |
computed: { | |
identifier() { | |
return this.name ? this.name : this.label.toLowerCase().replace(/ /g, '-') | |
}, | |
fieldValue: { | |
get() { | |
return this.getFieldValue(this.identifier) | |
}, | |
set(value) { | |
this.setFieldValue(this.identifier, value) | |
}, | |
}, | |
label() { | |
return this.$slots.default[0].text | |
}, | |
}, | |
methods: { | |
localRegisterField() { | |
const initialValue = this.value !== undefined ? this.value : '' | |
this.registerField(this.identifier, initialValue) | |
}, | |
}, | |
created() { | |
this.localRegisterField() | |
}, | |
inject: ['getFieldValue', 'setFieldValue', 'registerField'], | |
} | |
</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
<template> | |
<form><slot /></form> | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
fields: {}, | |
} | |
}, | |
methods: { | |
getFieldValue(field) { | |
return this.fields[field] | |
}, | |
setFieldValue(field, value) { | |
// Create new fields object | |
const fieldsUpdated = Object.assign({}, this.fields, { | |
[field]: value, | |
}) | |
this.fields = fieldsUpdated | |
this.emitUpdate() | |
}, | |
registerField(name, value) { | |
this.fields[name] = value | |
}, | |
emitUpdate() { | |
this.$emit('input', this.fields) | |
}, | |
}, | |
provide() { | |
const { getFieldValue, setFieldValue, registerField } = this | |
return { | |
getFieldValue, | |
setFieldValue, | |
registerField, | |
} | |
}, | |
mounted() { | |
this.emitUpdate() | |
}, | |
} | |
</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
<template> | |
<Form v-model="form"> | |
<Field>Name</Field> | |
<Field name="Phone" type="tel">Phone <Icon type="phone" />> | |
</Form> | |
</template> | |
<script> | |
export default { | |
data: () => { | |
form: {} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment