Skip to content

Commit

Permalink
fix(NcDialog): Properly export Typescript types
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jul 7, 2024
1 parent 4ba63e1 commit 9141727
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
25 changes: 16 additions & 9 deletions src/components/NcDialog/NcDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export default {
</NcModal>
</template>

<script>
<script lang="ts">
import type { PropType } from 'vue'
import type { ComponentProps } from '../../utils/VueTypes.ts'

import { useElementSize } from '@vueuse/core'
import { computed, defineComponent, ref } from 'vue'

Expand All @@ -129,6 +132,8 @@ import NcDialogButton from '../NcDialogButton/index.js'

import GenRandomId from '../../utils/GenRandomId.js'

type NcDialogButtonProps = ComponentProps<typeof NcDialogButton>

export default defineComponent({
name: 'NcDialog',

Expand All @@ -152,7 +157,7 @@ export default defineComponent({

/** Additional elements to add to the focus trap */
additionalTrapElements: {
type: Array,
type: Array as PropType<(string|HTMLElement)[]>,
validator: (arr) => {
return (
Array.isArray(arr) && arr.every(
Expand Down Expand Up @@ -189,21 +194,24 @@ export default defineComponent({
* @type {'small'|'normal'|'large'|'full'}
*/
size: {
type: String,
type: String as PropType<'small'|'normal'|'large'|'full'>,
required: false,
default: 'small',
validator: (value) => typeof value === 'string' && ['small', 'normal', 'large', 'full'].includes(value),
validator: (value: string) => ['small', 'normal', 'large', 'full'].includes(value),
},

/**
* Buttons to display
* @default []
*/
buttons: {
type: Array,
type: Array as PropType<NcDialogButtonProps[]>,
required: false,
default: () => ([]),
validator: (value) => Array.isArray(value) && value.every((element) => typeof element === 'object'),
validator(value: unknown[]) {
return Array.isArray(value)
&& value.every((element) => typeof element === 'object')
},
},

/**
Expand Down Expand Up @@ -304,14 +312,13 @@ export default defineComponent({
setup(props, { emit, slots }) {
/**
* The dialog wrapper element
* @type {import('vue').Ref<HTMLDivElement>}
*/
const wrapper = ref()
const wrapper = ref<HTMLDivElement>()

/**
* We use the dialog width to decide if we collapse the navigation (flex direction row)
*/
const { width: dialogWidth } = useElementSize(wrapper, { width: 900 })
const { width: dialogWidth } = useElementSize(wrapper, { width: 900, height: 0 })

/**
* Whether the navigation is collapsed due to dialog and window size
Expand Down
14 changes: 8 additions & 6 deletions src/components/NcDialogButton/NcDialogButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Dialog button component used by NcDialog in the actions slot to display the butt
</NcButton>
</template>

<script>
import { defineComponent } from 'vue'
import NcButton from '../NcButton/index.ts'
<script lang="ts">
import { defineComponent, type PropType } from 'vue'
import NcButton, { ButtonType } from '../NcButton/index'
import NcIconSvgWrapper from '../NcIconSvgWrapper/index.js'

export default defineComponent({
Expand Down Expand Up @@ -67,10 +67,12 @@ export default defineComponent({
* @type {'primary'|'secondary'|'error'|'warning'|'success'}
*/
type: {
type: String,
type: String as PropType<ButtonType>,
default: ButtonType.Secondary,
required: false,
default: 'secondary',
validator: (type) => typeof type === 'string' && ['primary', 'secondary', 'error', 'warning', 'success'].includes(type),
validator(value: string) {
return Object.values(ButtonType).includes(value as ButtonType)
}

Check warning on line 75 in src/components/NcDialogButton/NcDialogButton.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
},

/**
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/components/NcModal/NcModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export default {
<script>
import { useSwipe } from '@vueuse/core'
import { createFocusTrap } from 'focus-trap'
import Vue from 'vue'
import { warn as VueWarn } from 'vue'

import { getTrapStack } from '../../utils/focusTrap.js'
import { t } from '../../l10n.js'
Expand Down Expand Up @@ -601,7 +601,7 @@ export default {
},
mounted() {
if (!this.name && !this.labelId) {
Vue.util.warn('[NcModal] You need either set the name or set a `labelId` for accessibility.')
VueWarn('[NcModal] You need either set the name or set a `labelId` for accessibility.')
}

// init clear view
Expand Down
11 changes: 11 additions & 0 deletions src/utils/VueTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { AllowedComponentProps, Component, VNodeProps } from 'vue'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ComponentProps<C extends Component> = C extends new (...args: any) => any
? Omit<InstanceType<C>['$props'], keyof VNodeProps | keyof AllowedComponentProps | `on${string}`>
: never;

0 comments on commit 9141727

Please sign in to comment.