@@ -205,22 +205,43 @@ class UIManager {
205205 }
206206 }
207207
208- /** @param {string } title @param {string } label @param {string } [defaultValue] @returns {Promise<string | null> } */
209- prompt ( title , label , defaultValue = '' ) {
208+ /**
209+ * @param {string } title
210+ * @param {string } label
211+ * @param {string } [defaultValue]
212+ * @param {{ validate?: (v: string) => string | null } } [options]
213+ * @returns {Promise<string | null> }
214+ */
215+ prompt ( title , label , defaultValue = '' , { validate } = { } ) {
210216 return new Promise ( resolve => {
211217 const dialog = /** @type {HTMLDialogElement } */ ( $ ( '#prompt-dialog' ) )
212218 const form = $ ( '#prompt-form' )
213219 const input = /** @type {HTMLInputElement } */ ( $ ( '#prompt-input' ) )
220+ const errorEl = $ ( '#prompt-error' )
214221 $ ( '#prompt-title' ) . textContent = title
215222 $ ( '#prompt-label' ) . textContent = label
216223 input . value = defaultValue
224+ errorEl . textContent = ''
225+ errorEl . hidden = true
217226
218227 /** @type {string | null } */
219228 let result = null
220229
230+ /** @returns {boolean } */
231+ const checkValid = ( ) => {
232+ if ( ! validate ) return true
233+ const err = validate ( input . value )
234+ errorEl . textContent = err ?? ''
235+ errorEl . hidden = ! err
236+ return ! err
237+ }
238+
239+ const onInput = ( ) => checkValid ( )
240+
221241 /** @param {Event } e */
222242 const onSubmit = e => {
223243 e . preventDefault ( )
244+ if ( ! checkValid ( ) ) { input . focus ( ) ; return }
224245 result = input . value . trim ( ) || null
225246 dialog . close ( )
226247 }
@@ -234,12 +255,14 @@ class UIManager {
234255
235256 const onClose = ( ) => {
236257 form . removeEventListener ( 'submit' , onSubmit )
258+ input . removeEventListener ( 'input' , onInput )
237259 $ ( '#prompt-cancel' ) . removeEventListener ( 'click' , onCancel )
238260 dialog . removeEventListener ( 'click' , onBackdropClick )
239261 resolve ( result )
240262 }
241263
242264 form . addEventListener ( 'submit' , onSubmit )
265+ input . addEventListener ( 'input' , onInput )
243266 $ ( '#prompt-cancel' ) . addEventListener ( 'click' , onCancel )
244267 dialog . addEventListener ( 'click' , onBackdropClick )
245268 dialog . addEventListener ( 'close' , onClose , { once : true } )
0 commit comments