Skip to content

Commit 1bdfbad

Browse files
authored
feat(dialog): close和cancel各司其职 (#3282)
* feat(dialog): close和cancel各司其职 * fix(dialog): 修复test * fix: 在 overlay点击时只暴露cancel回调
1 parent 5a52205 commit 1bdfbad

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

src/packages/dialog/__test__/dialog.spec.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import '@testing-library/jest-dom'
44
import { Dialog } from '../dialog'
55

66
test('show dialog base info display ', async () => {
7-
const onClose = vi.fn()
7+
const onCancel = vi.fn()
88
const { container } = render(
9-
<Dialog title="title" data-testid="test" visible onClose={onClose}>
9+
<Dialog title="title" data-testid="test" visible onCancel={onCancel}>
1010
<div>content</div>
1111
</Dialog>
1212
)
@@ -28,7 +28,7 @@ test('show dialog base info display ', async () => {
2828

2929
expect(wrapEle).toBeNull()
3030
fireEvent.click(footerCancelEle)
31-
expect(onClose).toBeCalled()
31+
expect(onCancel).toBeCalled()
3232
})
3333

3434
test('show dialog custom footer-direction ', async () => {
@@ -116,7 +116,6 @@ test('dialog close icon position adjustment', async () => {
116116
expect(closeBtn).toBeInTheDocument()
117117
fireEvent.click(closeBtn)
118118
expect(onClose).toBeCalled()
119-
expect(onCancel).toBeCalled()
120119
})
121120

122121
test('should display loading when onConfirm returns a promise', async () => {

src/packages/dialog/demos/h5/demo2.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const Demo2 = () => {
1010
title="基础弹框"
1111
visible={visible}
1212
onConfirm={() => setVisible(false)}
13-
onCancel={() => setVisible(false)}
13+
onCancel={() => {
14+
setVisible(false)
15+
}}
1416
>
1517
支持函数调用和组件调用两种方式。
1618
</Dialog>

src/packages/dialog/demos/h5/demo6.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ const Demo6 = () => {
2222
'--nutui-dialog-close-color': '#888B94',
2323
}}
2424
onConfirm={() => setVisible1(false)}
25-
onCancel={() => setVisible1(false)}
25+
onClose={() => {
26+
setVisible1(false)
27+
console.log('onClose')
28+
}}
29+
onCancel={() => {
30+
// do sth else
31+
console.log('onCancel')
32+
}}
2633
>
2734
支持函数调用和组件调用两种方式。
2835
</Dialog>

src/packages/dialog/dialog.taro.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ export const BaseDialog: FunctionComponent<Partial<TaroDialogProps>> & {
101101
const handleCancel = (e: ITouchEvent | MouseEvent<HTMLButtonElement>) => {
102102
e.stopPropagation()
103103
if (!beforeCancel?.()) return
104-
if (!beforeClose?.()) return
105-
onClose()
106104
onCancel()
107105
}
108106

@@ -189,19 +187,17 @@ export const BaseDialog: FunctionComponent<Partial<TaroDialogProps>> & {
189187

190188
const renderCloseIcon = () => {
191189
if (!closeIcon) return null
192-
const handleCancel = () => {
193-
if (!beforeCancel?.()) return
190+
const handleClose = () => {
194191
if (!beforeClose?.()) return
195192
onClose()
196-
onCancel()
197193
}
198194
const closeClasses = classNames({
199195
[`${classPrefix}-close`]: true,
200196
[`${classPrefix}-close-${closeIconPosition}`]: true,
201197
})
202198
const systomIcon = closeIconPosition !== 'bottom' ? <Close /> : <Failure />
203199
return (
204-
<View className={closeClasses} onClick={handleCancel}>
200+
<View className={closeClasses} onClick={handleClose}>
205201
{React.isValidElement(closeIcon) ? closeIcon : systomIcon}
206202
</View>
207203
)
@@ -210,7 +206,6 @@ export const BaseDialog: FunctionComponent<Partial<TaroDialogProps>> & {
210206
const onHandleClickOverlay = (e: ITouchEvent) => {
211207
if (closeOnOverlayClick && visible && e.target === e.currentTarget) {
212208
const closed = onOverlayClick && onOverlayClick(e)
213-
closed && onClose()
214209
closed && onCancel()
215210
}
216211
}

src/packages/dialog/dialog.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ const BaseDialog: ForwardRefRenderFunction<unknown, Partial<WebDialogProps>> = (
9090
) => {
9191
e.stopPropagation()
9292
if (!beforeCancel?.()) return
93-
if (!beforeClose?.()) return
94-
onClose()
9593
onCancel()
9694
}
9795

@@ -179,19 +177,17 @@ const BaseDialog: ForwardRefRenderFunction<unknown, Partial<WebDialogProps>> = (
179177

180178
const renderCloseIcon = () => {
181179
if (!closeIcon) return null
182-
const handleCancel = () => {
183-
if (!beforeCancel?.()) return
180+
const handleClose = () => {
184181
if (!beforeClose?.()) return
185182
onClose()
186-
onCancel()
187183
}
188184
const closeClasses = classNames({
189185
[`${classPrefix}-close`]: true,
190186
[`${classPrefix}-close-${closeIconPosition}`]: true,
191187
})
192188
const systomIcon = closeIconPosition !== 'bottom' ? <Close /> : <Failure />
193189
return (
194-
<div className={closeClasses} onClick={handleCancel}>
190+
<div className={closeClasses} onClick={handleClose}>
195191
{React.isValidElement(closeIcon) ? closeIcon : systomIcon}
196192
</div>
197193
)
@@ -200,7 +196,6 @@ const BaseDialog: ForwardRefRenderFunction<unknown, Partial<WebDialogProps>> = (
200196
const onHandleClickOverlay = (e: MouseEvent) => {
201197
if (closeOnOverlayClick && visible && e.target === e.currentTarget) {
202198
const closed = onOverlayClick && onOverlayClick(e)
203-
closed && onClose()
204199
closed && onCancel()
205200
}
206201
}

0 commit comments

Comments
 (0)