Skip to content

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

Add types to basic components (#1233)

Olga Bulatdac2bdc88492
diff --git a/src/components/VAllResultsGrid/VImageCellSquare.vue b/src/components/VAllResultsGrid/VImageCellSquare.vue
index 13c52c879..424a98470 100644
--- a/src/components/VAllResultsGrid/VImageCellSquare.vue
+++ b/src/components/VAllResultsGrid/VImageCellSquare.vue
@@ -34,7 +34,7 @@
<script>
import VLink from '~/components/VLink.vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const errorImage = require('~/assets/image_not_available_placeholder.png')
diff --git a/src/components/VAudioTrack/layouts/VBoxLayout.vue b/src/components/VAudioTrack/layouts/VBoxLayout.vue
index 3d4aa8bd3..54210f25f 100644
--- a/src/components/VAudioTrack/layouts/VBoxLayout.vue
+++ b/src/components/VAudioTrack/layouts/VBoxLayout.vue
@@ -33,7 +33,7 @@
<script>
import { computed, defineComponent } from '@nuxtjs/composition-api'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
export default defineComponent({
name: 'VBoxLayout',
diff --git a/src/components/VAudioTrack/layouts/VRowLayout.vue b/src/components/VAudioTrack/layouts/VRowLayout.vue
index cbf7b80aa..eaa8aa326 100644
--- a/src/components/VAudioTrack/layouts/VRowLayout.vue
+++ b/src/components/VAudioTrack/layouts/VRowLayout.vue
@@ -88,7 +88,7 @@ import { computed, defineComponent, useContext } from '@nuxtjs/composition-api'
import { useBrowserIsBlink } from '~/composables/use-browser-detection'
import VAudioThumbnail from '~/components/VAudioThumbnail/VAudioThumbnail.vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
import VLink from '~/components/VLink.vue'
export default defineComponent({
diff --git a/src/components/VButton.vue b/src/components/VButton.vue
index 5f5e0aa59..47fcb6ed7 100644
--- a/src/components/VButton.vue
+++ b/src/components/VButton.vue
@@ -31,7 +31,6 @@ import {
toRefs,
computed,
PropType,
- Ref,
} from '@nuxtjs/composition-api'
import { warn } from '~/utils/console'
@@ -51,6 +50,7 @@ const buttonVariants = [
'action-menu-muted',
'plain',
'plain-dangerous',
+ 'full',
] as const
type ButtonVariant = typeof buttonVariants[number]
@@ -61,7 +61,7 @@ type ButtonSize = typeof buttonSizes[number]
const buttonTypes = ['button', 'submit', 'reset'] as const
-type ButtonType = typeof buttonTypes[number]
+export type ButtonType = typeof buttonTypes[number]
/**
* A button component that behaves just like a regular HTML `button` element
@@ -108,7 +108,7 @@ const VButton = defineComponent({
variant: {
type: String as PropType<ButtonVariant>,
default: 'primary',
- validate: (val: ButtonVariant) => buttonVariants.includes(val),
+ validate: (v: ButtonVariant) => buttonVariants.includes(v),
},
/**
* Allows for programmatically setting the pressed state of a button,
@@ -163,17 +163,17 @@ const VButton = defineComponent({
type: {
type: String as PropType<ButtonType>,
default: 'button',
- validate: (val: ButtonType) => buttonTypes.includes(val),
+ validate: (v: ButtonType) => buttonTypes.includes(v),
},
},
setup(props, { attrs }) {
const propsRef = toRefs(props)
- const disabledAttributeRef: Ref<boolean | undefined> = ref(
+ const disabledAttributeRef = ref<boolean | undefined>(
propsRef.disabled.value
)
- const ariaDisabledRef = ref()
- const trulyDisabledRef = ref()
- const typeRef: Ref<ButtonType | undefined> = ref(propsRef.type.value)
+ const ariaDisabledRef = ref<boolean>()
+ const trulyDisabledRef = ref<boolean>()
+ const typeRef = ref<ButtonType | undefined>(propsRef.type.value)
const supportsDisabledAttributeRef = ref(true)
const isActive = computed(() => {
diff --git a/src/components/VCheckbox/meta/VCheckbox.stories.js b/src/components/VCheckbox/meta/VCheckbox.stories.js
index e5e1a12f9..7cf973d1f 100644
--- a/src/components/VCheckbox/meta/VCheckbox.stories.js
+++ b/src/components/VCheckbox/meta/VCheckbox.stories.js
@@ -1,5 +1,5 @@
import VCheckbox from '~/components/VCheckbox/VCheckbox.vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
export default {
title: 'Components/VCheckbox',
diff --git a/src/components/VFilters/VFilterChecklist.vue b/src/components/VFilters/VFilterChecklist.vue
index 2863da465..9bb049dd2 100644
--- a/src/components/VFilters/VFilterChecklist.vue
+++ b/src/components/VFilters/VFilterChecklist.vue
@@ -60,7 +60,7 @@ import { useSearchStore } from '~/stores/search'
import VLicenseExplanation from '~/components/VFilters/VLicenseExplanation.vue'
import VCheckbox from '~/components/VCheckbox/VCheckbox.vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
import VButton from '~/components/VButton.vue'
import VIcon from '~/components/VIcon/VIcon.vue'
import VIconButton from '~/components/VIconButton/VIconButton.vue'
diff --git a/src/components/VIcon/VIcon.vue b/src/components/VIcon/VIcon.vue
index 8e2b3406c..8bef6bf6c 100644
--- a/src/components/VIcon/VIcon.vue
+++ b/src/components/VIcon/VIcon.vue
@@ -16,6 +16,15 @@ import { defineComponent } from '@nuxtjs/composition-api'
import { isTest } from '~/utils/node-env'
+export type IconPath = string
+export type IconProps = {
+ iconPath: IconPath
+ viewBox?: string
+ gId?: string
+ size?: number
+ rtlFlip?: boolean
+}
+
/**
* Displays the given icon in a 24px × 24px square.
*/
diff --git a/src/components/VIconButton/VIconButton.vue b/src/components/VIconButton/VIconButton.vue
index 67fadb752..98e470366 100644
--- a/src/components/VIconButton/VIconButton.vue
+++ b/src/components/VIconButton/VIconButton.vue
@@ -15,13 +15,32 @@
</VButton>
</template>
-<script>
-import { computed } from '@nuxtjs/composition-api'
+<script lang="ts">
+import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
-import VIcon from '~/components/VIcon/VIcon.vue'
+import VIcon, { IconProps } from '~/components/VIcon/VIcon.vue'
import VButton from '~/components/VButton.vue'
+import type { ButtonType } from '~/components/VButton.vue'
-export default {
+const SIZE_MAP = Object.freeze({
+ tiny: { icon: ['w-6', 'h-6'], button: ['w-6', 'h-6'] },
+ small: { icon: ['w-6', 'h-6'], button: ['w-10', 'h-10'] },
+ 'search-small': {
+ icon: ['w-6', 'h-6'],
+ button: ['w-10', 'md:w-12', 'h-10', 'md:h-12'],
+ },
+ 'search-medium': { icon: ['w-6', 'h-6'], button: ['w-12', 'h-12'] },
+ 'search-large': { icon: ['w-6', 'h-6'], button: ['w-14', 'h-14'] },
+ 'search-standalone': {
+ icon: ['w-6', 'h-6'],
+ button: ['w-14', 'md:w-[69px]', 'h-14', 'md:h-[69px]'],
+ },
+ medium: { icon: ['w-8', 'h-8'], button: ['w-14', 'h-14'] },
+ large: { icon: ['w-12', 'h-12'], button: ['w-20', 'h-20'] },
+} as const)
+type Size = keyof typeof SIZE_MAP
+
+export default defineComponent({
name: 'VIconButton',
components: { VIcon, VButton },
props: {
@@ -30,25 +49,18 @@ export default {
* itself and the icon inside it.
*/
size: {
- type: String,
+ type: String as PropType<Size>,
default: 'medium',
- validator: (val) =>
- [
- 'tiny',
- 'small',
- 'search-small',
- 'search-medium',
- 'search-large',
- 'search-standalone',
- 'medium',
- 'large',
- ].includes(val),
+ validator: (val: string) => Object.keys(SIZE_MAP).includes(val),
},
/**
* props to pass down to the `VIcon` component nested inside the button; See
* documentation on `VIcon`.
*/
- iconProps: {},
+ iconProps: {
+ type: Object as PropType<IconProps>,
+ required: true,
+ },
/**
* props to pass down to the `VButton` component nested inside the button;
* See documentation on `VButton`.
@@ -56,31 +68,10 @@ export default {
buttonProps: {},
},
setup(props, { attrs }) {
- const type = attrs['type'] ?? 'button'
+ const type = (attrs['type'] ?? 'button') as ButtonType
- const buttonSizeClasses = computed(
- () =>
- ({
- tiny: ['w-6', 'h-6'],
- small: ['w-10', 'h-10'],
- 'search-small': ['w-10', 'md:w-12', 'h-10', 'md:h-12'],
- 'search-medium': ['w-12', 'h-12'],
- 'search-large': ['w-14', 'h-14'],
- 'search-standalone': ['w-14', 'md:w-[69px]', 'h-14', 'md:h-[69px]'],
- medium: ['w-14', 'h-14'],
- large: ['w-20', 'h-20'],
- }[props.size])
- )
- const iconSizeClasses = computed(
- () =>
- ({
- tiny: ['w-6', 'h-6'],
- small: ['w-6', 'h-6'],
- search: ['w-6', 'h-6'],
- medium: ['w-8', 'h-8'],
- large: ['w-12', 'h-12'],
- }[props.size])
- )
+ const buttonSizeClasses = computed(() => SIZE_MAP[props.size].button)
+ const iconSizeClasses = computed(() => SIZE_MAP[props.size].icon)
return {
type,
@@ -89,5 +80,5 @@ export default {
iconSizeClasses,
}
},
-}
+})
</script>
diff --git a/src/components/VImageGrid/VImageCell.vue b/src/components/VImageGrid/VImageCell.vue
index 6b6cd9692..70b48568c 100644
--- a/src/components/VImageGrid/VImageCell.vue
+++ b/src/components/VImageGrid/VImageCell.vue
@@ -35,7 +35,7 @@
</template>
<script>
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
import VLink from '~/components/VLink.vue'
// eslint-disable-next-line @typescript-eslint/no-var-requires
diff --git a/src/components/License/VLicense.vue b/src/components/VLicense/VLicense.vue
similarity index 66%
rename from src/components/License/VLicense.vue
rename to src/components/VLicense/VLicense.vue
index cb420eb11..47c3b4b9e 100644
--- a/src/components/License/VLicense.vue
+++ b/src/components/VLicense/VLicense.vue
@@ -16,29 +16,27 @@
</div>
</template>
-<script>
-import { computed, useContext } from '@nuxtjs/composition-api'
+<script lang="ts">
+import {
+ computed,
+ defineComponent,
+ PropType,
+ useContext,
+} from '@nuxtjs/composition-api'
-import { ALL_LICENSES } from '~/constants/license'
+import { ALL_LICENSES, License, licenseIcons } from '~/constants/license'
-import { isCc, getFullLicenseName } from '~/utils/license'
+import { isCc, getFullLicenseName, licenseToElements } from '~/utils/license'
import VIcon from '~/components/VIcon/VIcon.vue'
-import by from '~/assets/licenses/by.svg'
-import cc0 from '~/assets/licenses/cc0.svg'
import ccLogo from '~/assets/licenses/cc-logo.svg'
-import nc from '~/assets/licenses/nc.svg'
-import nd from '~/assets/licenses/nd.svg'
-import pdm from '~/assets/licenses/pdm.svg'
-import sa from '~/assets/licenses/sa.svg'
-import samplingPlus from '~/assets/licenses/sampling-plus.svg'
/**
* Displays the icons for the license along with a readable display name for the
* license.
*/
-export default {
+export default defineComponent({
name: 'VLicense',
components: { VIcon },
props: {
@@ -47,9 +45,9 @@ export default {
* @values
*/
license: {
- type: String,
+ type: String as PropType<License>,
required: true,
- validator: (val) => ALL_LICENSES.includes(val),
+ validator: (val: string) => ALL_LICENSES.includes(val as License),
},
/**
* Whether to display icons filled with a white background or leave them transparent.
@@ -72,37 +70,30 @@ export default {
const isCcLicense = computed(() => isCc(props.license))
const icons = computed(() => {
- let iconList = props.license.split(/[-\s]/)
- if (isCcLicense.value) iconList = ['ccLogo', ...iconList]
- return iconList
+ if (isCcLicense.value) {
+ return ['ccLogo', ...licenseToElements(props.license)] as const
+ } else {
+ return [...licenseToElements(props.license)] as const
+ }
})
const licenseName = computed(() => {
return {
- readable: i18n.t(`license-readable-names.${props.license}`),
+ readable: i18n.t(`license-readable-names.${props.license}`).toString(),
full: getFullLicenseName(props.license, '', i18n),
}
})
return {
ccLogo,
- svgs: {
- ccLogo,
- by,
- cc0,
- nc,
- nd,
- pdm,
- sa,
- 'sampling+': samplingPlus,
- },
+ svgs: { ...licenseIcons, ccLogo },
icons,
isCcLicense,
licenseName,
}
},
-}
+})
</script>
<style scoped>
diff --git a/src/components/License/meta/VLicense.stories.mdx b/src/components/VLicense/meta/VLicense.stories.mdx
similarity index 98%
rename from src/components/License/meta/VLicense.stories.mdx
rename to src/components/VLicense/meta/VLicense.stories.mdx
index efa61eaa7..71c5695da 100644
--- a/src/components/License/meta/VLicense.stories.mdx
+++ b/src/components/VLicense/meta/VLicense.stories.mdx
@@ -6,7 +6,7 @@ import {
Story,
} from '@storybook/addon-docs'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
<Meta title="Components/VLicense" component={VLicense} />
diff --git a/src/components/VLicenseElements.vue b/src/components/VLicenseElements.vue
index 4d3da500a..5bb0e682b 100644
--- a/src/components/VLicenseElements.vue
+++ b/src/components/VLicenseElements.vue
@@ -20,50 +20,36 @@
</ul>
</template>
-<script>
-import { computed, defineComponent } from '@nuxtjs/composition-api'
+<script lang="ts">
+import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
-import VIcon from '~/components/VIcon/VIcon.vue'
+import type { License } from '~/constants/license'
+import { licenseIcons } from '~/constants/license'
+import { licenseToElements } from '~/utils/license'
-import by from '~/assets/licenses/by.svg'
-import cc0 from '~/assets/licenses/cc0.svg'
-import nc from '~/assets/licenses/nc.svg'
-import nd from '~/assets/licenses/nd.svg'
-import pdm from '~/assets/licenses/pdm.svg'
-import sa from '~/assets/licenses/sa.svg'
-import sampling from '~/assets/licenses/sampling.svg'
-import samplingPlus from '~/assets/licenses/sampling-plus.svg'
+import VIcon from '~/components/VIcon/VIcon.vue'
export default defineComponent({
name: 'VLicenseElements',
components: { VIcon },
props: {
license: {
- type: String,
+ type: String as PropType<License>,
required: true,
},
size: {
- type: String,
+ type: String as PropType<'big' | 'small'>,
default: 'big',
- validator: (val) => ['big', 'small'].includes(val),
+ validator: (val: string) => ['big', 'small'].includes(val),
},
},
setup(props) {
- const elements = computed(() => props.license.split('-'))
+ const elements = computed(() => licenseToElements(props.license))
const isSmall = computed(() => props.size === 'small')
return {
- icons: {
- by,
- nc,
- nd,
- sa,
- cc0,
- pdm,
- sampling,
- 'sampling+': samplingPlus,
- },
+ icons: licenseIcons,
elements,
isSmall,
}
diff --git a/src/components/VLoadMore.vue b/src/components/VLoadMore.vue
index e3619ec6e..da4cf0bdd 100644
--- a/src/components/VLoadMore.vue
+++ b/src/components/VLoadMore.vue
@@ -9,7 +9,7 @@
{{ buttonLabel }}
</VButton>
</template>
-<script>
+<script lang="ts">
import { computed, defineComponent, useContext } from '@nuxtjs/composition-api'
import VButton from '~/components/VButton.vue'
@@ -25,7 +25,7 @@ export default defineComponent({
default: true,
},
},
- setup(props, { emit }) {
+ setup(_, { emit }) {
const { i18n } = useContext()
const buttonLabel = computed(() => {
diff --git a/src/constants/license.ts b/src/constants/license.ts
index 7f193fe02..da1710fac 100644
--- a/src/constants/license.ts
+++ b/src/constants/license.ts
@@ -1,3 +1,13 @@
+import by from '~/assets/licenses/by.svg'
+import nc from '~/assets/licenses/nc.svg'
+import nd from '~/assets/licenses/nd.svg'
+import sa from '~/assets/licenses/sa.svg'
+import cc0 from '~/assets/licenses/cc0.svg'
+import pdm from '~/assets/licenses/pdm.svg'
+import sampling from '~/assets/licenses/sampling.svg'
+import samplingPlus from '~/assets/licenses/sampling-plus.svg'
+import ccLogo from '~/assets/licenses/cc-logo.svg'
+
export const CC_LICENSES = [
'by',
'by-sa',
@@ -23,3 +33,16 @@ export type License = typeof ALL_LICENSES[number]
export const LICENSE_VERSIONS = ['', '1.0', '2.0', '2.5', '3.0', '4.0'] as const
export type LicenseVersion = typeof LICENSE_VERSIONS[number]
+
+export const licenseIcons = {
+ ccLogo,
+ by,
+ nc,
+ nd,
+ sa,
+ cc0,
+ pdm,
+ sampling,
+ 'sampling+': samplingPlus,
+}
+export type LicenseElement = keyof typeof licenseIcons
diff --git a/src/pages/search/index.vue b/src/pages/search/index.vue
index fb9a56e29..7a3be35e1 100644
--- a/src/pages/search/index.vue
+++ b/src/pages/search/index.vue
@@ -5,8 +5,8 @@
<script>
import { useMeta, defineComponent } from '@nuxtjs/composition-api'
-import { propTypes } from '~/pages/search/search-page.types'
import { useLoadMore } from '~/composables/use-load-more'
+import { propTypes } from '~/pages/search/search-page.types'
import VAllResultsGrid from '~/components/VAllResultsGrid/VAllResultsGrid.vue'
diff --git a/src/pages/search/search-page.types.js b/src/pages/search/search-page.types.js
deleted file mode 100644
index a87292033..000000000
--- a/src/pages/search/search-page.types.js
+++ /dev/null
@@ -1,24 +0,0 @@
-export const propTypes = {
- resultItems: {
- type: /** @type {import('../../store/types').Media[]} */ (Object),
- required: true,
- },
- fetchState: {
- type: /** @type {import('../../store/types').FetchState[]} */ (Object),
- required: true,
- },
- isFilterVisible: {
- type: Boolean,
- required: false,
- },
- searchTerm: {
- type: String,
- required: true,
- },
- supported: {
- type: Boolean,
- required: false,
- },
-}
-
-/** @typedef {import('@nuxtjs/composition-api').ExtractPropTypes<typeof propTypes>} Props */
diff --git a/src/pages/search/search-page.types.ts b/src/pages/search/search-page.types.ts
new file mode 100644
index 000000000..d68aa6f80
--- /dev/null
+++ b/src/pages/search/search-page.types.ts
@@ -0,0 +1,29 @@
+import type { SupportedMediaType } from '~/constants/media'
+import type { Media } from '~/models/media'
+import type { FetchState } from '~/composables/use-fetch-state'
+
+import type { ExtractPropTypes, PropType } from '@nuxtjs/composition-api'
+
+export const propTypes = {
+ resultItems: {
+ type: Object as PropType<Record<SupportedMediaType, Media[]>>,
+ required: true,
+ },
+ fetchState: {
+ type: Object as PropType<FetchState>,
+ required: true,
+ },
+ isFilterVisible: {
+ type: Boolean,
+ required: false,
+ },
+ searchTerm: {
+ type: String,
+ required: true,
+ },
+ supported: {
+ type: Boolean,
+ required: false,
+ },
+}
+export type SearchPageProps = ExtractPropTypes<typeof propTypes>
diff --git a/src/utils/license.ts b/src/utils/license.ts
index 4ef8eff42..c503e1e89 100644
--- a/src/utils/license.ts
+++ b/src/utils/license.ts
@@ -2,6 +2,7 @@ import type { License, LicenseVersion } from '~/constants/license'
import {
CC_LICENSES,
DEPRECATED_CC_LICENSES,
+ LicenseElement,
PUBLIC_DOMAIN_MARKS,
} from '~/constants/license'
@@ -86,3 +87,10 @@ export const isCc = (license: License): boolean =>
license == 'cc0' ||
(CC_LICENSES as ReadonlyArray<License>).includes(license) ||
(DEPRECATED_CC_LICENSES as ReadonlyArray<License>).includes(license)
+
+/**
+ * Splits license slug by `-` and returns an array of license elements
+ * @param license - the license slug
+ */
+export const licenseToElements = (license: License) =>
+ (license as string).split(/[-\s]/) as LicenseElement[]
diff --git a/test/unit/specs/components/v-checkbox.spec.js b/test/unit/specs/components/v-checkbox.spec.js
index 8a421006b..c3a26047f 100644
--- a/test/unit/specs/components/v-checkbox.spec.js
+++ b/test/unit/specs/components/v-checkbox.spec.js
@@ -2,7 +2,7 @@ import Vue from 'vue'
import { fireEvent, render, screen } from '@testing-library/vue'
import VCheckbox from '~/components/VCheckbox/VCheckbox.vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
const TestWrapperStringLabel = ({
id = 'simple',
diff --git a/test/unit/specs/components/v-license.spec.js b/test/unit/specs/components/v-license.spec.js
index 262e0bc1e..4b9a92798 100644
--- a/test/unit/specs/components/v-license.spec.js
+++ b/test/unit/specs/components/v-license.spec.js
@@ -1,6 +1,6 @@
import { render, screen } from '@testing-library/vue'
-import VLicense from '~/components/License/VLicense.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
describe('VLicense', () => {
let options = {
diff --git a/tsconfig.json b/tsconfig.json
index 88e084b7b..177ca132b 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -36,11 +36,15 @@
"include": [
"**/*.ts",
"test/proxy.js",
- "src/components/VLink.vue",
- "src/components/VIcon.vue",
"src/components/VButton.vue",
"src/components/VContentPage.vue",
"src/components/VMetaSearch/*.vue",
+ "src/components/VLicenseElements.vue",
+ "src/components/VLink.vue",
+ "src/components/VLoadMore.vue",
+ "src/components/VIcon/VIcon.vue",
+ "src/components/VIconButton/VIconButton.vue",
+ "src/components/VLicense/VLicense.vue",
"src/composables/types.js",
"src/composables/use-event-listener-outside.js",
"src/composables/use-active-audio.js",