Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Toast): 鸿蒙 适配 #2299

Merged
merged 20 commits into from
Nov 7, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"dependencies": {
"@babel/runtime": "^7.23.9",
"@nutui/icons-react": "^1.0.5",
"@nutui/icons-react-taro": "^1.0.5",
"@nutui/icons-react-taro": "2.0.0-beta.0",
"@nutui/jdesign-icons-react-taro": "1.0.6-beta.2",
"@nutui/touch-emulator": "^1.0.0",
"@react-spring/web": "~9.6.1",
Expand Down
64 changes: 21 additions & 43 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@
"author": "Ymm0008"
},
{
"version": "2.0.0",
"version": "3.0.0",
"name": "Toast",
"type": "component",
"tarodoc": true,
Expand Down
8 changes: 5 additions & 3 deletions src/packages/overlay/overlay.harmony.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.nut-overlay {
/* #ifdef rn */
position: absolute;
/* #endif */
/* #ifndef rn */
position: fixed;
/* #endif */
top: 0;
left: 0;
bottom: 0;
Expand All @@ -19,9 +24,6 @@
.nut-overflow-hidden {
overflow: hidden !important;
}
.nut-overflow-hidden .taro_page {
overflow: hidden !important;
}

@keyframes nut-fade-in {
0% {
Expand Down
38 changes: 16 additions & 22 deletions src/packages/toast/Notification.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import classNames from 'classnames'
import { Check, Loading, Failure, Tips } from '@nutui/icons-react'
import { Success, Loading, Failure, Tips } from '@nutui/icons-react'
import { render, unmount } from '@/utils/render'
import Overlay from '@/packages/overlay/index'
import { BasicComponent } from '@/utils/typings'
Expand Down Expand Up @@ -83,23 +83,12 @@ export default class Notification extends React.PureComponent<
renderIcon() {
const { icon } = this.props
if (typeof icon === 'string') {
let iconNode = null
switch (icon) {
case 'success':
iconNode = <Check />
break
case 'loading':
iconNode = <Loading className="nut-icon-loading" />
break
case 'fail':
iconNode = <Failure />
break
case 'warn':
iconNode = <Tips />
break
default:
break
}
const iconNode = {
success: <Success />,
fail: <Failure />,
warn: <Tips />,
loading: <Loading className="nut-icon-loading" />,
}[icon]
return <p className={`${classPrefix}-icon-wrapper`}>{iconNode}</p>
}
return icon
Expand Down Expand Up @@ -134,7 +123,6 @@ export default class Notification extends React.PureComponent<

const classes = classNames({
'nut-toast-has-icon': icon,
[`nut-toast-${size}`]: true,
})
return (
<>
Expand All @@ -150,14 +138,20 @@ export default class Notification extends React.PureComponent<
>
<div className={`${classPrefix} ${classes}`} id={`toast-${id}`}>
<div
className={`${classPrefix}-inner ${classPrefix}-${position} ${contentClassName} ${wordBreak}`}
style={contentStyle}
className={`${classPrefix}-inner ${classPrefix}-${position} ${contentClassName} ${classPrefix}-inner-${size} ${classPrefix}-inner-${wordBreak}`}
style={{
...contentStyle,
}}
>
{this.renderIcon()}
{title ? (
<div className={`${classPrefix}-title`}>{title}</div>
) : null}
<span className={`${classPrefix}-text`}>{content}</span>
<span
className={`${classPrefix}-text ${content ? '' : `${classPrefix}-text-empty`}`}
>
{content}
</span>
</div>
</div>
</Overlay>
Expand Down
8 changes: 6 additions & 2 deletions src/packages/toast/__test__/toast.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ test('test toast props', async () => {
fireEvent.click(getByTestId('emit-click'))
expect(onClickToast).toBeCalled()
expect(document.querySelector('.nut-toast-text')?.innerHTML).toBe('文案')
expect(document.querySelector('.nut-toast')).toHaveClass('nut-toast-small')
expect(document.querySelector('.nut-toast-inner')).toHaveClass(
'nut-toast-inner-small'
)
expect(document.querySelector('.nut-toast')).toHaveClass(
'nut-toast-has-icon'
)
Expand All @@ -83,7 +85,9 @@ test('event show-success-toast', async () => {
await waitFor(() => {
fireEvent.click(getByTestId('emit-click'))
expect(onClickToast).toBeCalled()
expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon-Check')
expect(document.querySelector('.nut-icon')).toHaveClass(
'nut-icon nut-icon-Success'
)
Comment on lines +88 to +90
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

图标类名规范化调整

图标类名添加了基础类 nut-icon,使其更符合组件库的命名规范。建议:

  1. 检查其他图标类型(fail、warn、loading)是否也需要类似的更新
  2. 确保这个改动在跨平台场景下的样式表现一致

建议统一更新其他图标的测试断言,例如:

-expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon-Failure')
+expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon nut-icon-Failure')

-expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon-Tips')
+expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon nut-icon-Tips')

-expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon-loading')
+expect(document.querySelector('.nut-icon')).toHaveClass('nut-icon nut-icon-loading')

Committable suggestion skipped: line range outside the PR's diff.

expect(document.querySelector('.nut-toast-text')?.innerHTML).toBe('success')
})
})
Expand Down
23 changes: 9 additions & 14 deletions src/packages/toast/demo.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Toast } from '@nutui/nutui-react-taro'
import { ScrollView, View } from '@tarojs/components'
import { Toast } from '@nutui/nutui-react-taro'
import Header from '@/sites/components/header'
import { useTranslate } from '@/sites/assets/locale/taro'
import Demo1 from './demos/taro/demo1'
Expand Down Expand Up @@ -32,20 +32,10 @@ const ToastDemo = () => {
},
})

function demoClass() {
if (web()) {
return 'web'
}
if (!harmonyAndRn()) {
return 'full'
}
return ''
}

return (
<>
<Header />
<ScrollView className={`demo ${demoClass()}`}>
<ScrollView className={`demo ${web() ? 'web' : ''}`}>
<View className="h2">{translated.basic}</View>
<Demo1 />
<View className="h2">{translated.toastFunction}</View>
Expand All @@ -55,8 +45,13 @@ const ToastDemo = () => {
<Demo3 />
<View className="h2">{translated.toastCustomIcon}</View>
<Demo4 />
<View className="h2">{translated.toastWordBreak}</View>
<Demo5 />
{/* rn和 鸿蒙不支持 break-all */}
{harmonyAndRn() ? null : (
<>
<View className="h2">{translated.toastWordBreak}</View>
<Demo5 />
</>
)}
</ScrollView>
</>
)
Expand Down
3 changes: 2 additions & 1 deletion src/packages/toast/demos/h5/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const Demo1 = () => {
title="成功提示"
onClick={() =>
Toast.show({
content: '成功提示',
title: '成功提示',
content: '成功提示成功提示成功提示',
icon: 'success',
})
}
Expand Down
1 change: 1 addition & 0 deletions src/packages/toast/demos/h5/demo5.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Demo5 = () => {
Toast.show({
content: `Let's try ABCDEFGHIJKLMN here.`,
wordBreak: mode,
contentStyle: { width: '200px' },
})
}
return (
Expand Down
16 changes: 8 additions & 8 deletions src/packages/toast/demos/taro/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const Demo1 = () => {
}}
/>
<Cell
title="Text文字提示"
title="文字提示"
onClick={() => {
openToast('text', '网络失败,请稍后再试~')
setShowToast(true)
}}
/>
<Cell
title="Toast 标题提示"
title="标题提示"
onClick={() => {
openToast(
'text',
Expand All @@ -61,30 +61,30 @@ const Demo1 = () => {
}}
/>
<Cell
title="Success 成功提示"
title="成功提示"
onClick={() => {
openToast('success', '成功提示')
openToast('success', '成功提示成功提示成功提示')
setShowToast(true)
}}
/>
<Cell
title="Error 失败提示"
title="失败提示"
onClick={() => {
openToast('fail', '失败提示')
setShowToast(true)
}}
/>
<Cell
title=" Warning 警告提示"
title="警告提示"
onClick={() => {
openToast('warn', '警告提示')
setShowToast(true)
}}
/>
<Cell
title=" Loading 加载提示"
title="加载提示"
onClick={() => {
openToast('loading', '加载中')
openToast('loading', '加载提示')
setShowToast(true)
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/packages/toast/demos/taro/demo2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const Demo5 = () => {
title="函数调用"
onClick={() => {
Toast.show('test', {
title: '函数调用',
content: '函数调用',
title: '函数调用函数调用',
content: '函数调用函数调用函数调用函数调用函数',
type: 'fail',
duration: 2,
position: 'center',
Expand Down
15 changes: 13 additions & 2 deletions src/packages/toast/demos/taro/demo5.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useState } from 'react'
import { Cell, Toast, ToastWordBreak } from '@nutui/nutui-react-taro'
import pxTransform from '@/utils/px-transform'

const Demo5 = () => {
const [state, setState] = useState<{
content: string
content?: string
wordBreak: ToastWordBreak
}>({
content: `Let's try ABCDEFGHIJKLMN here.`,
Expand All @@ -19,10 +20,20 @@ const Demo5 = () => {
onClose={() => {
setShow(false)
}}
contentStyle={{ width: pxTransform(200) }}
wordBreak={state.wordBreak}
/>
<Cell.Group>
<Cell title="换行时截断单词" onClick={() => setShow(true)} />
<Cell
title="换行时截断单词"
onClick={() => {
setState({
content: `Let's try ABCDEFGHIJKLMN here.`,
wordBreak: 'break-all',
})
setShow(true)
}}
/>
<Cell
title="换行时不截断单词"
onClick={() => {
Expand Down
Loading
Loading