Skip to content

Commit

Permalink
fix(events): use methods for event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Aug 22, 2022
1 parent c655b7b commit e06ca82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
13 changes: 12 additions & 1 deletion nuxt/components/form/FormCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class="rounded"
type="checkbox"
:checked="value"
@change="$emit('change', $event.target.checked)"
@change="onChange"
/>
<label class="pl-2" :for="`input-${formKey}`"><slot /></label>
</div>
Expand All @@ -26,5 +26,16 @@ export default defineComponent({
type: Boolean as PropType<boolean | undefined>,
},
},
setup(_props, { emit }) {
const methods = {
onChange(payload: Event) {
emit('change', (payload.target as HTMLInputElement).checked)
},
}
return {
...methods,
}
},
})
</script>
24 changes: 18 additions & 6 deletions nuxt/components/form/FormRadioButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:name="groupName"
type="radio"
:value="value ? value : titleSlug"
@change="$emit('change', $event.target.value)"
@change="onChange"
/>
<label class="pl-2" :for="`input-${groupName}-${titleSlug}`">
{{ title }}
Expand All @@ -17,7 +17,7 @@
<script lang="ts">
import slugify from 'slugify'
import { defineComponent, PropType } from '#app'
import { computed, defineComponent, PropType } from '#app'
export default defineComponent({
props: {
Expand All @@ -38,10 +38,22 @@ export default defineComponent({
type: String,
},
},
computed: {
titleSlug(): string {
return slugify(this.title, { lower: true, strict: true })
},
setup(props, { emit }) {
const methods = {
onChange(payload: Event) {
emit('change', (payload.target as HTMLInputElement).value)
},
}
const computations = {
titleSlug: computed(() => {
return slugify(props.title, { lower: true, strict: true })
}),
}
return {
...methods,
...computations,
}
},
})
</script>
34 changes: 23 additions & 11 deletions nuxt/components/form/input/FormInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:placeholder="placeholder"
:type="type"
:value="value?.$model"
@input="$emit('input', $event.target?.value)"
@input="onInput"
/>
<div v-if="validationProperty && isValidatable">
<FormInputIconWrapper v-if="validationProperty.$pending">
Expand Down Expand Up @@ -106,6 +106,8 @@
<script lang="ts">
import consola from 'consola'
import { Validation } from 'vuelidate/vuelidate'
import { defineComponent, PropType } from '#app'
const FormInput = defineComponent({
Expand Down Expand Up @@ -152,17 +154,23 @@ const FormInput = defineComponent({
},
value: {
default: undefined,
type: Object as PropType<object | undefined>,
type: Object as PropType<Validation | undefined>,
},
warning: {
default: false,
type: Boolean,
},
},
created() {
setup(props, { emit }) {
const methods = {
onInput(payload: Event) {
emit('input', (payload.target as HTMLInputElement)?.value)
},
}
if (
!this.placeholder &&
this.type &&
!props.placeholder &&
props.type &&
![
'checkbox',
'datetime-local',
Expand All @@ -171,17 +179,21 @@ const FormInput = defineComponent({
'textarea',
'tiptap',
'radio',
].includes(this.type)
].includes(props.type)
) {
consola.warn(`placeholder is missing for ${this.idLabel}!`)
consola.warn(`placeholder is missing for ${props.idLabel}!`)
}
if (
!this.value &&
this.type &&
!['checkbox', 'select'].includes(this.type)
!props.value &&
props.type &&
!['checkbox', 'select'].includes(props.type)
) {
consola.warn(`value is missing for ${this.idLabel}!`)
consola.warn(`value is missing for ${props.idLabel}!`)
}
return {
...methods,
}
},
})
Expand Down

0 comments on commit e06ca82

Please sign in to comment.