Skip to content

Commit

Permalink
fix(project): fix/exporting-fix-for-volar (element-plus#849)
Browse files Browse the repository at this point in the history
- Using `defineComponent` to wrap component up for Volar support, this should close element-plus#841
- Also made changes for some typing
- Removed `merge.ts` since `Object.assign` are now supported natively
  • Loading branch information
jw-foss committed Dec 6, 2020
1 parent e8a9074 commit 2280dd5
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 99 deletions.
6 changes: 3 additions & 3 deletions packages/badge/src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</template>

<script lang="ts">
import { computed } from 'vue'
import { computed, defineComponent } from 'vue'
interface IBadgeProps {
value: string | number
Expand All @@ -33,7 +33,7 @@ interface IBadgeProps {
interface IBadgeSetups {
content: number | string
}
export default {
export default defineComponent({
name: 'ElBadge',
props: {
value: {
Expand Down Expand Up @@ -69,5 +69,5 @@ export default {
content,
}
},
}
})
</script>
5 changes: 3 additions & 2 deletions packages/carousel/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

<script lang="ts">
import {
defineComponent,
reactive,
computed,
ref,
Expand All @@ -76,7 +77,7 @@ import {
} from '@element-plus/utils/resize-event'
import { ICarouselProps, CarouselItem, InjectCarouselScope } from './carousel'
export default {
export default defineComponent({
name: 'ElCarousel',
props: {
initialIndex: {
Expand Down Expand Up @@ -386,5 +387,5 @@ export default {
root,
}
},
}
})
</script>
42 changes: 17 additions & 25 deletions packages/color-picker/src/color.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hsv2hsl = function(hue, sat, val) {
const hsv2hsl = function(hue: number, sat: number, val: number) {
return [
hue,
(sat * val / ((hue = (2 - sat) * val) < 1 ? hue : 2 - hue)) || 0,
Expand All @@ -8,39 +8,39 @@ const hsv2hsl = function(hue, sat, val) {

// Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
// <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
const isOnePointZero = function(n) {
const isOnePointZero = function(n: unknown) {
return typeof n === 'string' && n.indexOf('.') !== -1 && parseFloat(n) === 1
}

const isPercentage = function(n) {
const isPercentage = function(n: unknown) {
return typeof n === 'string' && n.indexOf('%') !== -1
}

// Take input from [0, n] and return it as [0, 1]
const bound01 = function(value: number | string, max) {
const bound01 = function(value: number | string, max: number | string) {
if (isOnePointZero(value)) value = '100%'

const processPercent = isPercentage(value)
value = Math.min(max, Math.max(0, parseFloat(value + '')))
value = Math.min((max as number), Math.max(0, parseFloat(value + '')))

// Automatically convert percentage into number
if (processPercent) {
value = parseInt((value * max) + '', 10) / 100
value = parseInt((value * (max as number)) + '', 10) / 100
}

// Handle floating point rounding errors
if ((Math.abs(value - max) < 0.000001)) {
if ((Math.abs(value - (max as number)) < 0.000001)) {
return 1
}

// Convert into [0, 1] range if it isn't already
return (value % max) / parseFloat(max)
return (value % (max as number)) / parseFloat(max as string)
}

const INT_HEX_MAP = { 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F' }

const toHex = function({ r, g, b }) {
const hexOne = function(value) {
const hexOne = function(value: number) {
value = Math.min(Math.round(value), 255)
const high = Math.floor(value / 16)
const low = value % 16
Expand All @@ -54,7 +54,7 @@ const toHex = function({ r, g, b }) {

const HEX_INT_MAP = { A: 10, B: 11, C: 12, D: 13, E: 14, F: 15 }

const parseHexChannel = function(hex) {
const parseHexChannel = function(hex: string) {
if (hex.length === 2) {
return (HEX_INT_MAP[hex[0].toUpperCase()] || +hex[0]) * 16 + (HEX_INT_MAP[hex[1].toUpperCase()] || +hex[1])
}
Expand Down Expand Up @@ -153,23 +153,15 @@ export interface Options {
}

export default class Color {
private _hue: number
private _saturation: number
private _value: number
private _alpha: number
public enableAlpha: boolean
public format: string
public value: string
private _hue = 0
private _saturation = 100
private _value = 100
private _alpha = 100
public enableAlpha = false
public format = 'hex'
public value = ''
public selected?: boolean
constructor(options?: Options) {
this._hue = 0
this._saturation = 100
this._value = 100
this._alpha = 100

this.enableAlpha = false
this.format = 'hex'
this.value = ''

options = options || {} as Options

Expand Down
31 changes: 19 additions & 12 deletions packages/color-picker/src/draggable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import isServer from '@element-plus/utils/isServer'
import { on, off } from '@element-plus/utils/dom'
let isDragging = false

export declare interface IOptions {
Expand All @@ -7,30 +8,36 @@ export declare interface IOptions {
end?: (event: Event) => void
}

const noopFunc = () => false

export default function(element: HTMLElement, options: IOptions) {
if (isServer) return
const moveFn = function(event) {

const moveFn = function(event: Event) {
options.drag?.(event)
}
const upFn = function(event) {
document.onmousemove = null
document.onmouseup = null
document.onselectstart = null
document.ondragstart = null

const upFn = function(event: Event) {

off(document, 'mousemove',moveFn)
off(document, 'mouseup', upFn)
off(document, 'selectstart', noopFunc)
off(document, 'dragstart', noopFunc)

isDragging = false

options.end?.(event)
}
element.onmousedown = function(event) {

on(element, 'mousedown', function(event) {
if (isDragging) return
document.onselectstart = () => false
document.ondragstart = () => false
on(document, 'selectstart', noopFunc)
on(document, 'dragstart', noopFunc)
on(document, 'mousemove', moveFn)
on(document, 'mouseup', upFn)

document.onmousemove = moveFn
document.onmouseup = upFn
isDragging = true

options.start?.(event)
}
})
}
5 changes: 3 additions & 2 deletions packages/date-picker/src/date-picker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { defineComponent } from 'vue'
import { DEFAULT_FORMATS_DATE, DEFAULT_FORMATS_DATEPICKER } from '@element-plus/time-picker'
import { CommonPicker } from '@element-plus/time-picker'
import DatePickPanel from './date-picker-com/panel-date-pick.vue'
Expand Down Expand Up @@ -31,7 +32,7 @@ const getPanel = function(type) {
return DatePickPanel
}

export default {
export default defineComponent({
name: 'ElDatePicker',
install: null,
props: {
Expand All @@ -51,4 +52,4 @@ export default {
default: scopedProps => h(getPanel(props.type), scopedProps),
})
},
}
})
2 changes: 1 addition & 1 deletion packages/dialog/src/useDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const CLOSED_EVENT = 'closed'
export const OPENED_EVENT = 'opened'
export { UPDATE_MODEL_EVENT }

export default function(props: UseDialogProps, ctx: SetupContext) {
export default function useDialog(props: UseDialogProps, ctx: SetupContext) {
const visible = ref(false)
const closed = ref(false)
const dialogRef = ref(null)
Expand Down
5 changes: 3 additions & 2 deletions packages/directives/repeat-click/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { on, once } from '@element-plus/utils/dom'

import type { ObjectDirective } from 'vue'
export default {
beforeMount(el, binding) {
let interval = null
let startTime
let startTime: number
const handler = () => binding.value && binding.value()
const clear = () => {
if (Date.now() - startTime < 100) {
Expand All @@ -21,4 +22,4 @@ export default {
interval = setInterval(handler, 100)
})
},
}
} as ObjectDirective
5 changes: 3 additions & 2 deletions packages/menu/src/menu-collapse-transition.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
</transition>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { addClass, removeClass, hasClass } from '@element-plus/utils/dom'
export default {
export default defineComponent({
name: 'ElMenuCollapseTransition',
setup() {
return {
Expand Down Expand Up @@ -51,5 +52,5 @@ export default {
},
}
},
}
})
</script>
5 changes: 3 additions & 2 deletions packages/menu/src/menuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</template>
<script lang="ts">
import {
defineComponent,
computed,
onMounted,
onBeforeUnmount,
Expand All @@ -50,7 +51,7 @@ import { IMenuItemProps, RootMenuProvider, SubMenuProvider } from './menu'
import useMenu from './useMenu'
import ElTooltip from '@element-plus/tooltip'
export default {
export default defineComponent({
name: 'ElMenuItem',
componentName: 'ElMenuItem',
Expand Down Expand Up @@ -160,5 +161,5 @@ export default {
onMouseLeave,
}
},
}
})
</script>
5 changes: 3 additions & 2 deletions packages/menu/src/submenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<script lang="ts">
import mitt from 'mitt'
import {
defineComponent,
computed,
ref,
provide,
Expand All @@ -105,7 +106,7 @@ import { ISubmenuProps, RootMenuProvider, SubMenuProvider } from './menu'
import useMenu from './useMenu'
import ElPopper from '@element-plus/popper'
export default {
export default defineComponent({
name: 'ElSubmenu',
componentName: 'ElSubmenu',
components: { ElCollapseTransition, ElPopper },
Expand Down Expand Up @@ -428,5 +429,5 @@ export default {
verticalTitleRef,
}
},
}
})
</script>
3 changes: 3 additions & 0 deletions packages/popover/src/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default {
mounted(el, binding, vnode) {
attachEvents(el, binding, vnode)
},
updated(el, binding, vnode) {
attachEvents(el, binding, vnode)
},
} as ObjectDirective

export const VPopover = 'popover'
6 changes: 3 additions & 3 deletions packages/radio/src/radio-button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
</label>
</template>
<script lang="ts">
import { computed } from 'vue'
import { computed, defineComponent } from 'vue'
import { useRadio, useRadioAttrs } from './useRadio'
export default {
export default defineComponent({
name: 'ElRadioButton',
props: {
Expand Down Expand Up @@ -106,5 +106,5 @@ export default {
activeStyle,
}
},
}
})
</script>
5 changes: 3 additions & 2 deletions packages/radio/src/radio-group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<script lang="ts">
import {
defineComponent,
nextTick,
computed,
provide,
Expand All @@ -30,7 +31,7 @@ import radioGroupKey from './token'
import type { PropType } from 'vue'
import type { ElFormItemContext } from '@element-plus/form'
export default {
export default defineComponent({
name: 'ElRadioGroup',
componentName: 'ElRadioGroup',
Expand Down Expand Up @@ -127,6 +128,6 @@ export default {
radioGroup,
}
},
}
})
</script>

6 changes: 3 additions & 3 deletions packages/scrollbar/src/bar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineComponent, h, computed, ref, getCurrentInstance, onUnmounted, inject, Ref } from 'vue'
import { on, off } from '@element-plus/utils/dom'
import { renderThumbStyle, BAR_MAP } from './util'
import { h, computed, ref, getCurrentInstance, onUnmounted, inject, Ref } from 'vue'

export default {
export default defineComponent({
name: 'Bar',

props: {
Expand Down Expand Up @@ -83,4 +83,4 @@ export default {
}),
)
},
}
})
Loading

0 comments on commit 2280dd5

Please sign in to comment.