Skip to content

refactor: fixed overlay zIndex is not effective, and extract types of dialog, overlay, fixednav, popup, toast #2954

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

Merged
merged 22 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
"dd": true
},
{
"version": "2.0.0",
"version": "3.0.0",
"name": "FixedNav",
"type": "component",
"cName": "悬浮导航",
Expand All @@ -323,7 +323,7 @@
"show": true,
"taro": true,
"author": "Ymm0008",
"dd": false
"dd": true
},
{
"version": "3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cascader/cascader.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, {
import classNames from 'classnames'
import { Loading, Check } from '@nutui/icons-react-taro'
import { ScrollView, View } from '@tarojs/components'
import { Popup, PopupProps } from '@/packages/popup/popup.taro'
import Popup, { PopupProps } from '@/packages/popup/index.taro'
import { Tabs } from '@/packages/tabs/tabs.taro'
import Tree, { convertListToOptions } from './utils'
import {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cascader/cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
} from 'react'
import classNames from 'classnames'
import { Loading, Check } from '@nutui/icons-react'
import { Popup, PopupProps } from '@/packages/popup/popup'
import Popup, { PopupProps } from '@/packages/popup/index'
import { Tabs } from '@/packages/tabs/tabs'
import Tree, { convertListToOptions } from './utils'
import {
Expand Down Expand Up @@ -476,7 +476,7 @@
return (
<>
{popup ? (
<Popup

Check failure on line 479 in src/packages/cascader/cascader.tsx

View workflow job for this annotation

GitHub Actions / build

Type '{ children: Element; visible: boolean; position: string; round: true; closeIcon: ReactNode; closeable: boolean; closeIconPosition: string; title: ReactNode; ... 20 more ...; onOpen?: (() => void) | undefined; }' is not assignable to type 'Omit<HTMLAttributes<HTMLDivElement>, "title">'.
{...popupProps}
visible={innerVisible}
position="bottom"
Expand Down
58 changes: 0 additions & 58 deletions src/packages/dialog/config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/packages/dialog/confirm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Dialog } from './dialog'
import { destroyList, DialogConfirmProps, DialogReturnProps } from './config'
import { destroyList, DialogConfirmProps, DialogReturnProps } from './types'
import { render as reactRender, unmount } from '@/utils/render'

function ConfirmDialog(props: DialogConfirmProps) {
Expand Down Expand Up @@ -100,7 +100,7 @@
const update = (newConfig: DialogConfirmProps) => {
dialogConfig = {
...dialogConfig,
title: config.title, // 避免 newConfig 未传递 title 时,icon 出现多个的问题

Check failure on line 103 in src/packages/dialog/confirm.tsx

View workflow job for this annotation

GitHub Actions / build

'title' is specified more than once, so this usage will be overwritten.
Copy link
Collaborator

Choose a reason for hiding this comment

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

newConfig 中存在 title 时,无法被覆盖,值一直是 config.title

...newConfig,
}

Expand Down
50 changes: 25 additions & 25 deletions src/packages/dialog/content.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { FunctionComponent, ReactNode, HTMLAttributes } from 'react'
import React, { FunctionComponent, HTMLAttributes } from 'react'
import classNames from 'classnames'
import { ITouchEvent, View } from '@tarojs/components'
import { BasicComponent } from '@/utils/typings'
import { View } from '@tarojs/components'
import { ContentProps } from './types'

interface ContentProps extends BasicComponent {
visible: boolean
title: ReactNode
header: ReactNode
footer: ReactNode
close: ReactNode
footerDirection: string
onClick: (event: ITouchEvent) => void
export const defaultContentProps: ContentProps = {
visible: false,
title: '',
header: null,
footer: null,
close: '',
footerDirection: '',
onClick: () => {},
}

export const Content: FunctionComponent<
Expand All @@ -24,28 +24,28 @@ export const Content: FunctionComponent<
footer,
close,
footerDirection,
onClick,
children,
} = props
onClick,
} = { ...defaultContentProps, ...props }

const classPrefix = 'nut-dialog'

const renderHeader = () => {
return title ? (
<View className={`${classPrefix}-header`}>{title}</View>
) : null
return title && <View className={`${classPrefix}-header`}>{title}</View>
}

const renderFooter = () => {
return footer ? (
<View
className={classNames(`${classPrefix}-footer`, {
[footerDirection as any]: footerDirection,
})}
>
{footer}
</View>
) : null
return (
footer && (
<View
className={classNames(`${classPrefix}-footer`, {
[footerDirection as any]: footerDirection,
})}
>
{footer}
</View>
)
)
}

const handleClick = (e: any) => {
Expand Down
49 changes: 28 additions & 21 deletions src/packages/dialog/content.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React, { FunctionComponent, ReactNode, HTMLAttributes } from 'react'
import React, { FunctionComponent, HTMLAttributes } from 'react'
import classNames from 'classnames'
import { ContentProps } from './types'

interface ContentProps {
visible: boolean
title: ReactNode
header: ReactNode
footer: ReactNode
close: ReactNode
footerDirection: string
export const defaultContentProps: ContentProps = {
visible: false,
title: '',
header: null,
footer: null,
close: '',
footerDirection: '',
onClick: () => {},
}

export const Content: FunctionComponent<
Partial<ContentProps> & HTMLAttributes<HTMLDivElement>
Partial<ContentProps> &
Omit<HTMLAttributes<HTMLDivElement>, 'title' | 'content'>
> = (props) => {
const {
visible,
Expand All @@ -22,7 +25,9 @@ export const Content: FunctionComponent<
footerDirection,
onClick,
children,
} = props
style,
className,
} = { ...defaultContentProps, ...props }

const classPrefix = 'nut-dialog'

Expand All @@ -31,15 +36,17 @@ export const Content: FunctionComponent<
}

const renderFooter = () => {
return footer ? (
<div
className={classNames(`${classPrefix}-footer`, {
[footerDirection as any]: footerDirection,
})}
>
{footer}
</div>
) : null
return (
footer && (
<div
className={classNames(`${classPrefix}-footer`, {
[footerDirection as any]: footerDirection,
})}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

类型转换需要改进

使用 as any 进行类型转换是不安全的做法,可能会在运行时引发问题。

建议定义一个明确的类型:

- [footerDirection as any]: footerDirection,
+ [footerDirection as 'horizontal' | 'vertical']: footerDirection,
📝 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
[footerDirection as any]: footerDirection,
[footerDirection as 'horizontal' | 'vertical']: footerDirection,

>
{footer}
</div>
)
)
}

const handleClick = (e: any) => {
Expand All @@ -48,8 +55,8 @@ export const Content: FunctionComponent<

return (
<div
className={classNames(`${classPrefix}-outer`, props.className)}
style={props.style}
className={classNames(`${classPrefix}-outer`, className)}
style={style}
onClick={(e) => handleClick(e)}
>
{close}
Expand Down
4 changes: 0 additions & 4 deletions src/packages/dialog/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
padding: $dialog-padding;
box-sizing: border-box;

&-overlay {
--nutui-overlay-zIndex: 1200;
}

&-outer {
position: fixed;
max-height: 100%;
Expand Down
45 changes: 25 additions & 20 deletions src/packages/dialog/dialog.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { CSSTransition } from 'react-transition-group'
import { View } from '@tarojs/components'
import { Failure, Close } from '@nutui/icons-react-taro'
import Button from '@/packages/button/index.taro'
import { DialogBasicProps } from './config'
import { Content } from './content.taro'
import { DialogBasicProps } from './types'
import { Content, defaultContentProps } from './content.taro'
import { useConfig } from '@/packages/configprovider/configprovider.taro'
import Overlay from '@/packages/overlay/index.taro'
import {
Expand All @@ -15,33 +15,37 @@ import {
useCustomEventsPath,
useParams,
} from '@/utils/use-custom-event'
import { BasicComponent } from '@/utils/typings'
import { useLockScrollTaro } from '@/utils/use-lock-scoll-taro'
import { mergeProps } from '@/utils/merge-props'
import { defaultOverlayProps } from '@/packages/overlay/overlay.taro'

export type DialogProps = DialogBasicProps & BasicComponent
const defaultProps = {
const defaultProps: DialogBasicProps = {
...defaultOverlayProps,
...defaultContentProps,
title: '',
content: '',
header: '',
footer: '',
confirmText: '',
cancelText: '',
overlay: true,
closeOnOverlayClick: true,
overlayStyle: {},
overlayClassName: 'nut-dialog-overlay',
hideConfirmButton: false,
hideCancelButton: false,
disableConfirmButton: false,
footerDirection: 'horizontal',
lockScroll: true,
closeIconPosition: 'bottom',
closeIcon: false,
zIndex: 1200,
beforeCancel: () => true,
beforeClose: () => true,
onCancel: () => {},
onClose: () => {},
onOverlayClick: () => true,
} as DialogProps
}
Comment on lines +21 to +45
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

需要修复类型不匹配问题

defaultProps 的类型定义与 DialogBasicProps 不匹配,这会导致编译错误。

建议检查并确保所有必需的属性都已定义,特别是 afterClose 属性。

🧰 Tools
🪛 GitHub Check: build

[failure] 21-21:
Type '{ title: string; content: string; header: string; footer: string; confirmText: string; cancelText: string; overlay: true; overlayStyle: {}; overlayClassName: string; hideConfirmButton: false; hideCancelButton: false; ... 21 more ...; afterClose: () => void; }' is not assignable to type 'DialogBasicProps'.

🪛 GitHub Actions: CI

[error] 21-21: Type mismatch error: Object type is not assignable to type 'DialogBasicProps'. Check the property types in the dialog component.


export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {
export const BaseDialog: FunctionComponent<Partial<DialogBasicProps>> & {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议优化事件处理和类型定义

  1. 使用可选链操作符可以使代码更简洁安全
  2. 参数类型需要更精确的定义

建议如下修改:

- setParams({ ...options, visible: status })
+ setParams?.({ ...options, visible: status })

Also applies to: 94-94

open: typeof open
close: typeof close
} = (props) => {
Expand Down Expand Up @@ -70,8 +74,11 @@ export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {
confirmText,
cancelText,
overlay,
overlayStyle,
overlayClassName,
closeIconPosition,
closeIcon,
zIndex,
onClose,
onCancel,
onConfirm,
Expand All @@ -84,11 +91,7 @@ export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {
useCustomEvent(
id as string,
({ status, options }: { status: boolean; options: any }) => {
if (status) {
setParams({ ...options, visible: true })
} else {
setParams({ ...options, visible: false })
}
setParams({ ...options, visible: status })
}
)
const refObject = useLockScrollTaro(!!(visible && lockScroll))
Expand Down Expand Up @@ -181,9 +184,9 @@ export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {

const onHandleClickOverlay = (e: any) => {
if (closeOnOverlayClick && visible && e.target === e.currentTarget) {
const closed = onOverlayClick && onOverlayClick()
closed && onClose?.()
closed && onCancel?.()
const closed = onOverlayClick && onOverlayClick(e)
closed && onClose && onClose()
closed && onCancel && onCancel()
}
}

Expand All @@ -194,15 +197,17 @@ export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {
catchMove={lockScroll}
>
<>
{overlay ? (
{overlay && (
<Overlay
zIndex={zIndex}
visible={visible}
style={overlayStyle}
className={overlayClassName}
closeOnOverlayClick={closeOnOverlayClick}
lockScroll={lockScroll}
onClick={onHandleClickOverlay}
className={classNames('nut-dialog-overlay')}
/>
) : null}
)}

<CSSTransition
in={visible}
Expand Down
Loading
Loading