refactor(spin): reimplement customize icon codes

This commit is contained in:
07akioni 2021-07-02 00:20:56 +08:00
parent 21698de271
commit 930ecab4c8
9 changed files with 92 additions and 129 deletions

View File

@ -33,17 +33,13 @@ export default defineComponent({
show: {
type: Boolean,
default: true
},
rotate: {
type: Boolean,
default: true
}
},
setup (props) {
useStyle('BaseLoading', style, toRef(props, 'clsPrefix'))
},
render () {
const { clsPrefix, radius, strokeWidth, stroke, scale, $slots } = this
const { clsPrefix, radius, strokeWidth, stroke, scale } = this
const scaledRadius = radius / scale
return (
<div class={`${clsPrefix}-base-loading`} role="img" aria-label="loading">
@ -51,13 +47,6 @@ export default defineComponent({
{{
default: () =>
this.show ? (
$slots.icon ?
<div class={[
`${clsPrefix}-base-loading__icon-slot`,
this.rotate && `${clsPrefix}-base-loading__icon-slot--rotate`
]}>
{$slots.icon()}
</div> :
<svg
class={`${clsPrefix}-base-loading__icon`}
viewBox={`0 0 ${2 * scaledRadius} ${2 * scaledRadius}`}

View File

@ -1,44 +1,28 @@
import { c, cB, cE, cM } from '../../../../_utils/cssr'
import { cB, cE } from '../../../../_utils/cssr'
import iconSwitchTransition from '../../../../_styles/transitions/icon-switch.cssr'
export default c([
c('@keyframes icon-rotate-animation', `
0% {
transform:rotate(0deg);
}
100% {
transform:rotate(360deg);
}
`),
cB('base-loading', `
position: relative;
line-height: 0;
width: 1em;
height: 1em;
export default cB('base-loading', `
position: relative;
line-height: 0;
width: 1em;
height: 1em;
`, [
cE('placeholder', `
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
`, [
cE('placeholder', {
position: 'absolute',
iconSwitchTransition({
left: '50%',
top: '50%',
transform: 'translateX(-50%) translateY(-50%)'
}, [
iconSwitchTransition({
left: '50%',
top: '50%',
originalTransform: 'translateX(-50%) translateY(-50%)'
})
]),
cE('icon', `
height: 1em;
width: 1em;
`, [
iconSwitchTransition()
]),
cE('icon-slot', [
cM('rotate', `
display: inline-block;
animation: icon-rotate-animation 1s linear infinite;
`)
])
originalTransform: 'translateX(-50%) translateY(-50%)'
})
]),
cE('icon', `
height: 1em;
width: 1em;
`, [
iconSwitchTransition()
])
])

View File

@ -1,26 +1,11 @@
# Customize Icon
```html
<n-space vertical>
<n-spin size="small">
<template #icon>
<n-icon>
<Reload />
</n-icon>
</template>
</n-spin>
<n-spin :show="show">
<n-alert title="La La La" type="success">
Leave it till tomorrow to unpack my case. Honey disconnect the phone.
</n-alert>
<template #icon>
<n-icon>
<Reload />
</n-icon>
</template>
</n-spin>
<n-button @click="show = !show">Click to Spin</n-button>
</n-space>
<n-spin :show="show" #icon>
<n-icon>
<Reload />
</n-icon>
</n-spin>
```
```js

View File

@ -14,15 +14,15 @@ customize-icon
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| rotate | `boolean` | `true` | If slot icon is rotate. |
| size | `'small' \| 'medium' \| 'large' \| number` | `'medium'` | |
| rotate | `boolean` | `true` | If icon is rotating, only works for custom icon. |
| size | `'small' \| 'medium' \| 'large' \| number` | `'medium'` | Size of the spin. |
| show | `boolean` | `true` | If spin is active. |
| stroke-width | `number` | `undefined` | Relative width of spin's stroke, you can assume the outer radius of spin is 100. |
| stroke | `string` | `undefined` | Color of the spin. |
## Slots
| Name | Parameters | Description |
| ------- | ---------- | ---------------------------------- |
| default | `()` | If set, spin will wrap the content |
| icon | `()` | Customize the spin icon |
| Name | Parameters | Description |
| ------- | ---------- | ----------------------------------- |
| default | `()` | If set, spin will wrap the content. |
| icon | `()` | Customize the spin icon. |

View File

@ -1,26 +1,13 @@
# 自定义图标
# 自定义图标
```html
<n-space vertical>
<n-spin size="small" >
<template #icon>
<n-icon>
<Reload />
</n-icon>
</template>
</n-spin>
<n-spin :show="show">
<n-alert title="La La La" type="success">
明天再打开行李箱。宝贝,挂电话啦。
</n-alert>
<template #icon>
<n-icon>
<Reload />
</n-icon>
</template>
</n-spin>
<n-button @click="show = !show">点出来加载</n-button>
</n-space>
<n-spin :show="show" #icon>
<n-icon>
<Reload />
</n-icon>
</n-spin>
```
```js

View File

@ -15,7 +15,7 @@ customize-icon
| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| rotate | `boolean` | `true` | 自定义加载图标是否有旋转动画 |
| size | `'small' \| 'medium' \| 'large' \| number` | `'medium'` | |
| size | `'small' \| 'medium' \| 'large' \| number` | `'medium'` | Spin 的尺寸 |
| show | `boolean` | `true` | Spin 在填入内容的状态是否激活 |
| stroke-width | `number` | `undefined` | Spin 边缘的相对宽度,假定 Spin 的外侧半径是 100 |
| stroke | `string` | `undefined` | Spin 的颜色 |
@ -25,4 +25,4 @@ customize-icon
| 名称 | 参数 | 说明 |
| ------- | ---- | ------------------------------- |
| default | `()` | 如果填入Spin 会包裹填入的内容 |
| icon | `()` | 自定义加载图标 |
| icon | `()` | 自定义加载图标 |

View File

@ -100,6 +100,26 @@ export default defineComponent({
},
render () {
const { $slots, mergedClsPrefix } = this
const rotate = $slots.icon && this.rotate
const icon = $slots.icon ? (
<div
class={[
`${mergedClsPrefix}-spin`,
rotate && `${mergedClsPrefix}-spin--rotate`
]}
style={$slots.default ? '' : (this.cssVars as CSSProperties)}
>
{$slots.icon()}
</div>
) : (
<NBaseLoading
clsPrefix={mergedClsPrefix}
style={$slots.default ? '' : (this.cssVars as CSSProperties)}
stroke={this.stroke}
stroke-width={this.mergedStrokeWidth}
class={`${mergedClsPrefix}-spin`}
/>
)
return $slots.default ? (
<div
class={`${mergedClsPrefix}-spin-container`}
@ -115,32 +135,12 @@ export default defineComponent({
</div>
<Transition name="fade-in-transition">
{{
default: () =>
this.compitableShow ? (
<NBaseLoading
clsPrefix={mergedClsPrefix}
stroke={this.stroke}
strokeWidth={this.mergedStrokeWidth}
rotate={this.rotate}
class={`${mergedClsPrefix}-spin`}
>
{{ icon: $slots.icon }}
</NBaseLoading>
) : null
default: () => (this.compitableShow ? icon : null)
}}
</Transition>
</div>
) : (
<NBaseLoading
clsPrefix={mergedClsPrefix}
style={this.cssVars as CSSProperties}
stroke={this.stroke}
stroke-width={this.mergedStrokeWidth}
rotate={this.rotate}
class={`${mergedClsPrefix}-spin`}
>
{{ icon: $slots.icon }}
</NBaseLoading>
icon
)
}
})

View File

@ -7,6 +7,14 @@ import fadeInTransition from '../../../_styles/transitions/fade-in.cssr'
// --size
// --color
export default c([
c('@keyframes spin-rotate', `
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
`),
cB('spin-container', {
position: 'relative'
}, [
@ -30,10 +38,14 @@ export default c([
])
]),
cB('spin', `
display: inline-block;
display: inline-flex;
height: var(--size);
width: var(--size);
font-size: var(--size);
color: var(--color);
`)
`, [
cM('rotate', `
animation: spin-rotate 2s linear infinite;
`)
])
])

View File

@ -11,9 +11,10 @@ describe('n-spin', () => {
it('should work with icon slot', () => {
const wrapper = mount(NSpin, {
slots: {
icon: () => h(NIcon, null, {
default: () => h(Reload)
})
icon: () =>
h(NIcon, null, {
default: () => h(Reload)
})
}
})
@ -23,15 +24,20 @@ describe('n-spin', () => {
it('rotate should work on icon slot', async () => {
const wrapper = mount(NSpin, {
slots: {
icon: () => h(NIcon, null, {
default: () => h(Reload)
})
icon: () =>
h(NIcon, null, {
default: () => h(Reload)
})
}
})
expect(wrapper.find('.n-base-loading__icon-slot--rotate').exists()).toBe(true)
expect(wrapper.find('.n-base-loading__icon-slot--rotate').exists()).toBe(
true
)
await wrapper.setProps({
rotate: false
})
expect(wrapper.find('.n-base-loading__icon-slot--rotate').exists()).toBe(false)
expect(wrapper.find('.n-base-loading__icon-slot--rotate').exists()).toBe(
false
)
})
})