Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add defineEvent emit documentation helper (#1330)

sarayourfriend49dad26f8808
diff --git a/src/components/VHeader/VSearchBar/VSearchBar.vue b/src/components/VHeader/VSearchBar/VSearchBar.vue
index 0df5d04c1..72f281458 100644
--- a/src/components/VHeader/VSearchBar/VSearchBar.vue
+++ b/src/components/VHeader/VSearchBar/VSearchBar.vue
@@ -29,6 +29,8 @@ import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
import { useMatchHomeRoute } from '~/composables/use-match-routes'
+import { defineEvent } from '~/types/emits'
+
import VInputField, {
FIELD_SIZES,
} from '~/components/VInputField/VInputField.vue'
@@ -61,7 +63,10 @@ const VSearchBar = defineComponent({
required: false,
},
},
- emits: ['input', 'submit'],
+ emits: {
+ input: defineEvent<[string]>(),
+ submit: defineEvent(),
+ },
setup(props, { emit }) {
const { matches: isHomeRoute } = useMatchHomeRoute()
diff --git a/src/components/VInputField/VInputField.vue b/src/components/VInputField/VInputField.vue
index f542af6c2..a4663e540 100644
--- a/src/components/VInputField/VInputField.vue
+++ b/src/components/VInputField/VInputField.vue
@@ -29,6 +29,8 @@
<script lang="ts">
import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
+import { defineEvent } from '~/types/emits'
+
export const FIELD_SIZES = {
small: 'h-10 text-md',
medium: 'h-12',
@@ -92,7 +94,9 @@ export default defineComponent({
},
},
// using non-native event name to ensure the two are not mixed
- emits: ['update:modelValue'],
+ emits: {
+ 'update:modelValue': defineEvent<[string]>(),
+ },
setup(props, { emit, attrs }) {
const type = typeof attrs['type'] === 'string' ? attrs['type'] : 'text'
diff --git a/src/components/VNotificationBanner.vue b/src/components/VNotificationBanner.vue
index 4d9acf589..f25d4eadf 100644
--- a/src/components/VNotificationBanner.vue
+++ b/src/components/VNotificationBanner.vue
@@ -29,6 +29,7 @@
<script lang="ts">
import { defineComponent, PropType } from '@nuxtjs/composition-api'
+import { defineEvent } from '~/types/emits'
import { useI18n } from '~/composables/use-i18n'
import { useStorage } from '~/composables/use-storage'
@@ -55,7 +56,9 @@ export default defineComponent({
required: true,
},
},
- emits: ['close'],
+ emits: {
+ close: defineEvent(),
+ },
setup(props, { emit }) {
const i18n = useI18n()
const shouldShow = useStorage(`banner:show-${props.id}`, true)
diff --git a/src/types/emits.ts b/src/types/emits.ts
new file mode 100644
index 000000000..88fb4e637
--- /dev/null
+++ b/src/types/emits.ts
@@ -0,0 +1,47 @@
+/**
+ * Type utility function for defining `emits` values on components
+ * without needing to provide an actual validator function.
+ *
+ * This works by casting `null` (which acts as a "skip" validation
+ * signal) to a function type based on the array of parameters passed
+ * in to the function.
+ *
+ * This is also a way around TS and ESLint rules about unused parameters.
+ * The currently recommended way to document emits with Composition API,
+ * is to use a function that always returns `true`, so validation is effectively
+ * skipped as well. However, then TS and ESlint will complain about unused
+ * parameters depending on your configuration. We'd like to keep those rules
+ * and compiler flags on as they are useful for catching bugs, but needing to
+ * disable them for every emits is cumbersome! Casting null, therefore, is a
+ * clean and relatively straightforward solution, at least until we're able
+ * to move to the `script setup` syntax and can use `defineEmit`, which solves
+ * this problem.
+ *
+ * @example
+ * ```typescript
+ * export default defineComponent({
+ * emits: {
+ * click: defineEvent<[MouseEvent]>(), // (e: MouseEvent) => void
+ * open: defineEvent(), // () => void
+ * close: defineEvent<[{ reason: CloseReason }]>(), // (p: { reason: CloseReason }) => void
+ * },
+ * setup(_, { emit }) {
+ * const state = ref('closed')
+ * const handleClick = (e: MouseEvent) => {
+ * // this will cause a TS error if you don't pass a `MouseEvent` payload
+ * emit('click', e)
+ * if (state.value === 'closed') {
+ * emit('open', { reason: 'click' }) // this will cause an error because the `open` event takes no payload
+ * } else {
+ * emit('close') // this will cause an error because the `close` event requires a payload
+ * }
+ * }
+ * }
+ * })
+ * ```
+ */
+export function defineEvent<Payload extends unknown[] | null = null>() {
+ return null as unknown as Payload extends [...infer A]
+ ? (...args: [...A]) => void
+ : () => void
+}