Skip to content

Commit

Permalink
feat: 添加skeleton组件
Browse files Browse the repository at this point in the history
  • Loading branch information
三少 committed Sep 16, 2021
1 parent 1726967 commit cebaf0a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ Button[完成]
Icon[完成]

Progress[完成]
Skeleton
Skeleton[完成]
CountDown
Tag
Sticky[完成]
Rate[完成]
Search
Search[完成]
NavBar[完成]
Tab
TabBar
4 changes: 4 additions & 0 deletions packages/vantui-demo/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Loading,
Rate,
Search,
Skeleton,
} from '@antmjs/vantui'

import './index.less'
Expand Down Expand Up @@ -51,6 +52,9 @@ export default function Index() {
<Icon name="chat" size={40} info="9" />
<Icon name="chat" size={40} info="99+" />
</View>
<View>
<Skeleton title avatar row={3} />
</View>
<View>
<Search
onSearch={(v) => {
Expand Down
90 changes: 90 additions & 0 deletions packages/vantui/src/components/skeleton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Taro from '@tarojs/taro'
import { useState, useEffect } from 'react'
import { View } from '@tarojs/components'
import * as utils from '../wxs/utils'
import { SkeletonProps } from '../../../types/skeleton'

export default function Index(props: SkeletonProps) {
const [state, setState] = useState({
isArray: false,
rowArray: [],
})
const { isArray, rowArray } = state
const {
row = 0,
animate = true,
avatar,
avatarShape = 'round',
avatarSize = Taro.pxTransform(64),
titleWidth = '40%',
title,
rowWidth = '100%',
loading = true,
children,
style,
className,
...others
} = props

useEffect(
function () {
setState((pre: any) => {
return { ...pre, rowArray: Array.from({ length: row }) }
})
},
[row],
)

useEffect(
function () {
setState((pre: any) => {
return { ...pre, isArray: (rowWidth as any) instanceof Array }
})
},
[rowWidth],
)

return loading ? (
<View
className={
'custom-class ' +
utils.bem('skeleton', [
{
animate,
},
]) +
` ${className}`
}
style={style}
{...others}
>
{avatar && (
<View
className={
'avatar-class ' + utils.bem('skeleton__avatar', [avatarShape])
}
style={'width:' + avatarSize + ';height:' + avatarSize}
></View>
)}
<View className={utils.bem('skeleton__content')}>
{title && (
<View
className={'title-class ' + utils.bem('skeleton__title')}
style={'width:' + titleWidth}
></View>
)}
{rowArray.map((_item, index) => {
return (
<View
key={index}
className={'row-class ' + utils.bem('skeleton__row')}
style={'width:' + (isArray ? rowWidth[index] : rowWidth)}
></View>
)
})}
</View>
</View>
) : (
<View className={utils.bem('skeleton__content')}>{children}</View>
)
}
1 change: 1 addition & 0 deletions packages/vantui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { default as Loading } from './components/loading'
export { default as Rate } from './components/rate'
export { default as Cell } from './components/cell'
export { default as Search } from './components/search'
export { default as Skeleton } from './components/skeleton'
1 change: 1 addition & 0 deletions packages/vantui/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { Row } from './row.d'
export { Rate } from './rate.d'
export { Cell } from './cell.d'
export { Search } from './search.d'
export { Skeleton } from './skeleton.d'
19 changes: 19 additions & 0 deletions packages/vantui/types/skeleton.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ComponentClass, ReactNode } from 'react'
import { StandardProps } from '@tarojs/components'

export interface SkeletonProps extends StandardProps {
row?: number
title?: boolean
avatar?: boolean
loading?: boolean
animate?: boolean
avatarSize?: string
avatarShape?: 'square' | 'round'
titleWidth?: string | string[]
rowWidth?: string
children?: ReactNode
}

declare const Skeleton: ComponentClass<SkeletonProps>

export { Skeleton }

0 comments on commit cebaf0a

Please sign in to comment.