antd-tiny-vue/components/button/loading-icon.tsx

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-04-19 08:30:24 +08:00
import { Transition, defineComponent, nextTick } from 'vue'
2023-04-19 08:16:23 +08:00
import { booleanType, someType, stringType } from '@v-c/utils'
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined'
export interface LoadingIconProps {
prefixCls: string
existIcon: boolean
loading?: boolean | object
}
export const loadingIconProps = {
prefixCls: stringType(),
existIcon: booleanType(),
loading: someType<boolean | object>([Boolean, Object])
}
2023-04-19 08:30:24 +08:00
const getCollapsedWidth = (el: Element) => {
const node: HTMLElement = el as HTMLElement
if (node) {
node.style.width = '0'
node.style.opacity = '0'
node.style.transform = 'scale(0)'
}
}
const getRealWidth = (el: Element) => {
const node: HTMLElement = el as HTMLElement
nextTick(() => {
if (node) {
node.style.width = `${node.scrollWidth}px`
node.style.opacity = '1'
node.style.transform = 'scale(1)'
}
}).then()
}
2023-04-19 08:16:23 +08:00
const LoadingIcon = defineComponent({
name: 'LoadingIcon',
props: loadingIconProps,
setup(props) {
return () => {
2023-04-19 08:30:24 +08:00
const { loading, existIcon, prefixCls } = props
const visible = !!loading
2023-04-19 08:16:23 +08:00
if (existIcon) {
return (
<span class={`${prefixCls}-loading-icon`}>
<LoadingOutlined />
</span>
)
}
2023-04-19 08:30:24 +08:00
return (
<Transition
name={`${prefixCls}-loading-icon-motion`}
onBeforeEnter={getCollapsedWidth}
onEnter={getRealWidth}
>
{visible ? (
<span class={`${prefixCls}-loading-icon`}>
<LoadingOutlined />
</span>
) : null}
</Transition>
)
2023-04-19 08:16:23 +08:00
}
}
})
export default LoadingIcon