Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/packages/loading/__test__/loading.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as React from 'react'

import { render } from '@testing-library/react'
import '@testing-library/jest-dom'

import { Star } from '@nutui/icons-react'
import { Loading } from '../loading'
import data from '@/packages/lottie/animation/light/loading.json'

test('type test', () => {
const { container } = render(<Loading type="circular" />)
Expand All @@ -30,19 +30,3 @@ test('custom icon test', () => {
)
expect(container.querySelector('svg')).toHaveClass('nut-icon-Star')
})

test('use lottie', () => {
const { container } = render(
<Loading
direction="vertical"
type="lottie"
jsonData={data}
lottieProps={{ autoplay: false, loop: false }}
>
正在奋力加载中,感谢您的等待
</Loading>
)
expect(container.querySelectorAll('g')[0].getAttribute('id')).toContain(
'lottie'
)
})
22 changes: 8 additions & 14 deletions src/packages/loading/demos/h5/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import React from 'react'
import { Cell, Loading } from '@nutui/nutui-react'
import data from '@nutui/nutui-react/dist/es/lottie/animation/light/loading.json'
import { Cell, Loading, Lottie } from '@nutui/nutui-react'
import lightLoading from '@nutui/nutui-react/dist/es/lottie/animation/light/loading.json'

const Demo1 = () => {
return (
<>
<Cell>
<Loading type="circular" />
</Cell>
<Cell>
<Loading type="spinner" />
</Cell>
<Cell>
<Loading direction="vertical" type="lottie" jsonData={data} />
</Cell>
<Cell>
<Loading
direction="vertical"
type="lottie"
jsonData={data}
lottieProps={{ autoplay: false, loop: false }}
>
正在奋力加载中,感谢您的等待
</Loading>
icon={
<>
<Lottie source={lightLoading} style={{ width: 56, height: 56 }} />
</>
}
/>
</Cell>
</>
)
Expand Down
29 changes: 7 additions & 22 deletions src/packages/loading/demos/taro/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
import React from 'react'
import { Cell, Loading } from '@nutui/nutui-react-taro'
import data from '@nutui/nutui-react-taro/dist/es/lottie/animation/light/loading.json'
import { Loading, Cell, Lottie } from '@nutui/nutui-react-taro'
import lightLoading from '@nutui/nutui-react-taro/dist/es/lottie/animation/light/loading.json'

const Demo1 = () => {
return (
<>
<Cell>
<Loading type="circular" />
</Cell>
<Cell>
<Loading type="spinner" />
</Cell>
<Cell>
<Loading
direction="vertical"
type="lottie"
jsonData={data}
lottieProps={{ style: { width: 56, height: 56 } }}
icon={
<>
<Lottie source={lightLoading} style={{ width: 56, height: 56 }} />
</>
}
/>
</Cell>
<Cell>
<Loading
direction="vertical"
type="lottie"
jsonData={data}
lottieProps={{
autoPlay: false,
loop: false,
style: { width: 56, height: 56 },
}}
>
正在奋力加载中,感谢您的等待
</Loading>
</Cell>
</>
)
}
Expand Down
7 changes: 0 additions & 7 deletions src/packages/loading/loading.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
}
}

.nut-loading-lottie-box {
width: 56px;
height: 56px;
border-radius: $radius-base;
background: $loading-lottie-background;
}

.nut-loading-text {
color: $loading-color;
font-size: $loading-font-size;
Expand Down
36 changes: 4 additions & 32 deletions src/packages/loading/loading.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { useRef } from 'react'
import React from 'react'
import classNames from 'classnames'
import {
Loading as IconLoading,
Loading1 as IconLoading1,
} from '@nutui/icons-react-taro'
import { View } from '@tarojs/components'
import Lottie from '../lottie/index.taro'
import { ComponentDefaults } from '@/utils/typings'
import { TaroLoadingProps, LoadingRef, LoadingType } from '@/types'
import { mergeProps } from '@/utils/merge-props'
import { LottieProps } from '@/packages/lottie' // 方便以后扩展设置为键值对形式
import { LoadingRef, LoadingType, TaroLoadingProps } from '@/types'

// 方便以后扩展设置为键值对形式
const loadingMap: { [key in LoadingType]?: any } = {
Expand All @@ -22,50 +19,25 @@ const defaultProps = {
// 对比一下,个人感觉还是Loading1比较好看一些,所以用它作为了默认的loading图标
type: 'circular',
direction: 'horizontal',
lottieProps: {},
} as TaroLoadingProps
const defaultLottieProps = {
loop: true,
autoplay: true,
}
Comment on lines 23 to 26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

移除未使用的 defaultLottieProps

与 Web 版本类似,Taro 版本中的 defaultLottieProps 对象也不再被使用,应该移除。

建议移除这个未使用的常量:

- const defaultLottieProps = {
-   loop: true,
-   autoplay: true,
- }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const defaultLottieProps = {
loop: true,
autoplay: true,
}

export const Loading = React.forwardRef<LoadingRef, Partial<TaroLoadingProps>>(
(props, ref) => {
const {
className,
style,
children,
direction,
icon,
lottieProps,
...rest
} = {
const { className, style, children, direction, icon, ...rest } = {
...defaultProps,
...props,
}
// @ts-ignore
const loadingLottieRef: React.MutableRefObject<LottieProps | null> =
useRef()
const mergedLottieProps = mergeProps(defaultLottieProps, lottieProps)
React.useImperativeHandle(ref, () => loadingLottieRef)

const classPrefix = 'nut-loading'
const getLoadingIcon = () => {
if (rest.type === 'lottie' && rest.jsonData) {
return (
<Lottie
{...mergedLottieProps}
// @ts-ignore
ref={loadingLottieRef}
source={rest.jsonData}
/>
)
}
const LoadingIcon = loadingMap[rest.type] || IconLoading1
return <LoadingIcon className={`${classPrefix}-icon`} />
}
const iconboxClassName = () => {
if (rest.type === 'lottie') return `${classPrefix}-lottie-box`
return `${classPrefix}-icon-box`
return !icon ? `${classPrefix}-icon-box` : ''
}
return (
<View
Expand Down
37 changes: 4 additions & 33 deletions src/packages/loading/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React, { useRef } from 'react'
import React from 'react'
import classNames from 'classnames'
import {
Loading as IconLoading,
Loading1 as IconLoading1,
} from '@nutui/icons-react'
import Lottie, { LottieProps } from '../lottie'
import { ComponentDefaults } from '@/utils/typings'
import { WebLoadingProps, LoadingRef } from '@/types'
import { mergeProps } from '@/utils/merge-props' // 方便以后扩展设置为键值对形式
import { LoadingRef, WebLoadingProps } from '@/types' // 方便以后扩展设置为键值对形式

// 方便以后扩展设置为键值对形式
const loadingMap = {
Expand All @@ -28,45 +26,18 @@ const defaultLottieProps = {
}
export const Loading = React.forwardRef<LoadingRef, Partial<WebLoadingProps>>(
(props, ref) => {
const {
className,
style,
children,
direction,
icon,
lottieProps,
...rest
} = {
const { className, style, children, direction, icon, ...rest } = {
...defaultProps,
...props,
}
const loadingLottieRef = useRef<LottieProps | null>(null)
// @ts-ignore
// const loadingLottieRef: React.MutableRefObject<LottieProps | null> =
// useRef()
const mergedLottieProps = mergeProps(defaultLottieProps, lottieProps)
React.useImperativeHandle(ref, () => loadingLottieRef)

const classPrefix = 'nut-loading'
const getLoadingIcon = () => {
if (!rest.jsonData) {
console.warn('Lottie animation requires jsonData prop')
}
if (rest.type === 'lottie') {
return (
<Lottie
{...mergedLottieProps}
ref={loadingLottieRef}
source={rest.jsonData}
/>
)
}
const LoadingIcon = loadingMap[rest.type] || IconLoading1
return <LoadingIcon className={`${classPrefix}-icon`} />
}
const iconboxClassName = () => {
if (rest.type === 'lottie') return `${classPrefix}-lottie-box`
return `${classPrefix}-icon-box`
return !icon ? `${classPrefix}-icon-box` : ''
}
return (
<div
Expand Down
2 changes: 1 addition & 1 deletion src/types/spec/loading/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseProps } from '../../base/props'
import { Direction } from '../../base/atoms'

export type LoadingRef = any
export type LoadingType = 'spinner' | 'circular' | 'lottie'
export type LoadingType = 'spinner' | 'circular'

export interface BaseLoading<LOTTIE_PROPS = any> extends BaseProps {
type: LoadingType
Expand Down