@@ -40,6 +40,7 @@ export const Modal = ({ closed, backgroundColor = "rgba(0,0,0,.5)", blurBackgrou
40
40
"background-color" : "white" ,
41
41
...modalStyleOverrides ,
42
42
} ;
43
+ document . activeElement instanceof HTMLElement && document . activeElement . blur ( ) ;
43
44
return ( ) => closed . val ? null : div ( { class : backgroundClass , style : toStyleStr ( backgroundStyle ) } , div ( { class : modalClass , style : toStyleStr ( modalStyle ) } , children ) ) ;
44
45
} ;
45
46
let tabsId = 0 ;
@@ -436,7 +437,8 @@ export const FloatingWindow = ({ title, closed = van.state(false), x = 100, y =
436
437
style : toStyleStr ( childrenContainerStyleOverrides )
437
438
} , children ) ) ;
438
439
} ;
439
- export const choose = ( { label, options, selectedColor = "#f5f5f5" , customModalProps = { } , textFilterClass = "" , textFilterStyleOverrides = { } , optionsContainerClass = "" , optionsContainerStyleOverrides = { } , optionClass = "" , optionStyleOverrides = { } , selectedClass = "" , selectedStyleOverrides = { } , } ) => {
440
+ let chooseId = 0 ;
441
+ export const choose = ( { label, options, showTextFilter = false , selectedColor = "#f5f5f5" , customModalProps = { } , textFilterClass = "" , textFilterStyleOverrides = { } , optionsContainerClass = "" , optionsContainerStyleOverrides = { } , optionClass = "" , optionStyleOverrides = { } , selectedClass = "" , selectedStyleOverrides = { } , } ) => {
440
442
const closed = van . state ( false ) ;
441
443
const { modalStyleOverrides, ...otherModalProps } = customModalProps ;
442
444
const modalProps = {
@@ -464,28 +466,33 @@ export const choose = ({ label, options, selectedColor = "#f5f5f5", customModalP
464
466
"flex-grow" : 1 ,
465
467
...optionsContainerStyleOverrides ,
466
468
} ;
467
- const textFilterDom = input ( {
469
+ const textFilterDom = showTextFilter ? input ( {
468
470
type : "text" ,
469
471
class : textFilterClass ,
470
472
style : toStyleStr ( textFilterStyle ) ,
471
473
oninput : e => query . val = e . target . value
472
- } ) ;
473
- const modalDom = div ( Modal ( modalProps , div ( label ) , div ( textFilterDom ) , ( ) => div ( { class : optionsContainerClass , style : toStyleStr ( optionsContainerStyle ) } , filtered . val . map ( ( o , i ) => div ( {
474
- class : ( ) => [ ] . concat ( optionClass ? optionClass : [ ] , i === index . val ? "vanui-choose-selected" : [ ] , i === index . val && selectedClass ? selectedClass : [ ] ) . join ( " " ) ,
475
- style : ( ) => toStyleStr ( {
476
- padding : "0.5rem" ,
477
- "background-color" : i === index . val ? selectedColor : "" ,
478
- ...optionStyleOverrides ,
479
- ...i === index . val ? selectedStyleOverrides : { } ,
480
- } ) ,
474
+ } ) : undefined ;
475
+ const optionStyle = {
476
+ padding : "0.5rem" ,
477
+ ...optionStyleOverrides ,
478
+ } ;
479
+ const selectedStyle = {
480
+ "background-color" : selectedColor ,
481
+ ...selectedStyleOverrides ,
482
+ } ;
483
+ const id = "vanui-choose-" + ( ++ chooseId ) ;
484
+ document . head . appendChild ( van . tags [ "style" ] ( `#${ id } .vanui-choose-selected, #${ id } .vanui-choose-option:hover { ${ toStyleStr ( selectedStyle ) } }` ) ) ;
485
+ van . add ( document . body , Modal ( modalProps , div ( label ) , showTextFilter ? div ( textFilterDom ) : undefined , ( ) => div ( { id, class : optionsContainerClass , style : toStyleStr ( optionsContainerStyle ) } , filtered . val . map ( ( o , i ) => div ( {
486
+ class : ( ) => [ "vanui-choose-option" ] . concat ( optionClass ? optionClass : [ ] , i === index . val ? "vanui-choose-selected" : [ ] , i === index . val && selectedClass ? selectedClass : [ ] ) . join ( " " ) ,
487
+ style : toStyleStr ( optionStyle ) ,
481
488
onclick : ( ) => ( resolve ( o ) , closed . val = true ) ,
482
489
} , o ) ) ) ) ) ;
483
- van . add ( document . body , modalDom ) ;
490
+ textFilterDom ?. focus ( ) ;
484
491
van . derive ( ( ) => {
485
492
index . val ;
486
- setTimeout ( ( ) => modalDom . querySelector ( " .vanui-choose-selected" ) ?. scrollIntoView ( false ) , 10 ) ;
493
+ setTimeout ( ( ) => document . querySelector ( `# ${ id } .vanui-choose-selected` ) ?. scrollIntoView ( false ) , 10 ) ;
487
494
} ) ;
488
- modalDom . addEventListener ( "keydown" , e => {
495
+ const navByKey = ( e ) => {
489
496
if ( e . key === "Enter" && index . val < filtered . val . length ) {
490
497
resolve ( filtered . val [ index . val ] ) ;
491
498
closed . val = true ;
@@ -498,7 +505,8 @@ export const choose = ({ label, options, selectedColor = "#f5f5f5", customModalP
498
505
index . val = index . val + 1 < filtered . val . length ? index . val + 1 : 0 ;
499
506
else if ( e . key === "ArrowUp" )
500
507
index . val = index . val > 0 ? index . val - 1 : filtered . val . length - 1 ;
501
- } ) ;
502
- textFilterDom . focus ( ) ;
508
+ } ;
509
+ document . addEventListener ( "keydown" , navByKey ) ;
510
+ van . derive ( ( ) => closed . val && document . removeEventListener ( "keydown" , navByKey ) ) ;
503
511
return res ;
504
512
} ;
0 commit comments