@@ -16,6 +16,8 @@ import { Toolbar } from './toolbar';
1616
1717export type Props = BaseProps & ParentProps & MountProps ;
1818
19+ const acceptValue = 'accept' ;
20+
1921/**
2022 * 提供了 {@link alert}、{@link confirm} 和 {@link prompt} 的方法,可用于替换对应的浏览器方法。
2123 */
@@ -46,7 +48,12 @@ function AlertProvider(props: BaseProps): JSX.Element {
4648 dlg . root ( ) . showModal ( ) ;
4749
4850 return new Promise < void > ( resolve => {
49- dlg . root ( ) . addEventListener ( 'close' , ( ) => resolve ( ) ) ;
51+ const close = ( ) => {
52+ resolve ( ) ;
53+ dlg . root ( ) . removeEventListener ( 'close' , close ) ;
54+ } ;
55+
56+ dlg . root ( ) . addEventListener ( 'close' , close ) ;
5057 } ) ;
5158 } ;
5259
@@ -95,9 +102,12 @@ function ConfirmProvider(props: BaseProps): JSX.Element {
95102 dlg . root ( ) . showModal ( ) ;
96103
97104 return new Promise < boolean > ( resolve => {
98- dlg . root ( ) . addEventListener ( 'close' , ( ) => {
99- resolve ( dlg . root ( ) . returnValue === 'true' ) ;
100- } ) ;
105+ const close = ( ) => {
106+ resolve ( dlg . root ( ) . returnValue === acceptValue ) ;
107+ dlg . root ( ) . removeEventListener ( 'close' , close ) ;
108+ } ;
109+
110+ dlg . root ( ) . addEventListener ( 'close' , close ) ;
101111 } ) ;
102112 } ;
103113
@@ -111,7 +121,7 @@ function ConfirmProvider(props: BaseProps): JSX.Element {
111121 }
112122 class = "min-w-60"
113123 ref = { el => ( dlg = el ) }
114- footer = { < Actions /> }
124+ footer = { < Actions acceptValue = { acceptValue } /> }
115125 >
116126 < p > { msg ( ) } </ p >
117127 </ Root >
@@ -144,10 +154,16 @@ function PromptProvider(props: BaseProps): JSX.Element {
144154 dlg . root ( ) . showModal ( ) ;
145155
146156 return new Promise < string | null > ( resolve => {
147- dlg . root ( ) . addEventListener ( 'close' , ( ) => {
148- resolve ( dlg . root ( ) . returnValue ?? null ) ;
157+ const close = ( ) => {
158+ if ( dlg . root ( ) . returnValue === acceptValue ) {
159+ const v = access . getValue ( ) ;
160+ resolve ( v ) ;
161+ }
149162 access . setValue ( '' ) ;
150- } ) ;
163+ dlg . root ( ) . removeEventListener ( 'close' , close ) ;
164+ } ;
165+
166+ dlg . root ( ) . addEventListener ( 'close' , close ) ;
151167 } ) ;
152168 } ;
153169
@@ -161,7 +177,7 @@ function PromptProvider(props: BaseProps): JSX.Element {
161177 }
162178 ref = { el => ( dlg = el ) }
163179 class = "min-w-60"
164- footer = { < Actions /> }
180+ footer = { < Actions acceptValue = { acceptValue } /> }
165181 >
166182 < TextField . Root class = "w-full" layout = "vertical" label = { msg ( ) } accessor = { access } />
167183 </ Root >
0 commit comments