@@ -6,6 +6,8 @@ import { fr } from "../fr";
66import { assert } from "tsafe/assert" ;
77import { is } from "tsafe/is" ;
88import { useConstCallback } from "../tools/powerhooks/useConstCallback" ;
9+ import { observeInputValue } from "../tools/observeInputValue" ;
10+ import { id } from "tsafe/id" ;
911
1012export type SearchButtonProps = {
1113 searchInputId : string ;
@@ -19,16 +21,17 @@ export function SearchButton(props: SearchButtonProps) {
1921
2022 const [ , forceUpdate ] = useReducer ( x => x + 1 , 0 ) ;
2123
22- const [ getInputValue , setGetInputValue ] = useState ( ( ) => ( ) => "" ) ;
23-
24- const [ resetInputValue , setResetInputValue ] = useState < ( ) => void > ( ( ) => ( ) => {
25- /* do nothing */
26- } ) ;
27- const [ focusInputElement , setFocusInputElement ] = useState < ( ) => void > ( ( ) => ( ) => {
28- /* do nothing */
29- } ) ;
30-
31- const [ isInputFocused , setIsInputFocused ] = useState ( false ) ;
24+ const [ { focusInputElement, getInputValue, resetInputValue, getIsInputFocused } , setInputApi ] =
25+ useState ( ( ) => ( {
26+ "getInputValue" : id < ( ) => string > ( ( ) => "" ) ,
27+ "resetInputValue" : id < ( ) => void > ( ( ) => {
28+ /* do nothing */
29+ } ) ,
30+ "focusInputElement" : id < ( ) => void > ( ( ) => {
31+ /* do nothing */
32+ } ) ,
33+ "getIsInputFocused" : id < ( ) => boolean > ( ( ) => false )
34+ } ) ) ;
3235
3336 const onClick = useConstCallback ( ( ) => {
3437 const inputValue = getInputValue ( ) ;
@@ -55,54 +58,22 @@ export function SearchButton(props: SearchButtonProps) {
5558
5659 assert ( is < HTMLInputElement > ( inputElement ) ) ;
5760
58- setGetInputValue ( ( ) => ( ) => inputElement . value ) ;
59-
60- const cleanups : ( ( ) => void ) [ ] = [ ] ;
61-
62- inputElement . addEventListener (
63- "input" ,
64- ( ( ) => {
65- const callback = ( ) => {
66- forceUpdate ( ) ;
67- } ;
68-
69- cleanups . push ( ( ) => inputElement . removeEventListener ( "input" , callback ) ) ;
70-
71- return callback ;
72- } ) ( )
73- ) ;
74-
75- inputElement . addEventListener (
76- "keydown" ,
77- ( ( ) => {
78- const callback = ( event : KeyboardEvent ) => {
79- if ( event . key !== "Escape" ) {
80- return ;
81- }
82-
83- forceUpdate ( ) ;
84- } ;
61+ setInputApi ( {
62+ "focusInputElement" : ( ) => inputElement . focus ( ) ,
63+ "getInputValue" : ( ) => inputElement . value ,
64+ "resetInputValue" : ( ) => ( inputElement . value = "" ) ,
65+ "getIsInputFocused" : ( ) => document . activeElement === inputElement
66+ } ) ;
8567
86- cleanups . push ( ( ) => inputElement . removeEventListener ( "keydown" , callback ) ) ;
87-
88- return callback ;
89- } ) ( )
90- ) ;
68+ observeInputValue ( inputElement , ( ) => forceUpdate ( ) ) ;
9169
92- const resetInputValue = ( ) => {
93- inputElement . value = "" ;
94- inputElement . dispatchEvent ( new Event ( "input" ) ) ;
95- } ;
96-
97- setResetInputValue ( ( ) => resetInputValue ) ;
98-
99- setFocusInputElement ( ( ) => ( ) => inputElement . focus ( ) ) ;
70+ const cleanups : ( ( ) => void ) [ ] = [ ] ;
10071
10172 if ( isControlledByUser ) {
10273 inputElement . addEventListener (
10374 "focus" ,
10475 ( ( ) => {
105- const callback = ( ) => setIsInputFocused ( true ) ;
76+ const callback = ( ) => forceUpdate ( ) ;
10677
10778 cleanups . push ( ( ) => inputElement . removeEventListener ( "focus" , callback ) ) ;
10879
@@ -113,7 +84,7 @@ export function SearchButton(props: SearchButtonProps) {
11384 inputElement . addEventListener (
11485 "blur" ,
11586 ( ( ) => {
116- const callback = ( ) => setIsInputFocused ( false ) ;
87+ const callback = ( ) => forceUpdate ( ) ;
11788
11889 cleanups . push ( ( ) => inputElement . removeEventListener ( "blur" , callback ) ) ;
11990
@@ -149,7 +120,7 @@ export function SearchButton(props: SearchButtonProps) {
149120 return ;
150121 }
151122
152- resetInputValue ( ) ;
123+ inputElement . value = "" ;
153124 inputElement . blur ( ) ;
154125 } ;
155126
@@ -165,7 +136,7 @@ export function SearchButton(props: SearchButtonProps) {
165136 } ;
166137 } , [ searchInputId , isControlledByUser ] ) ;
167138
168- if ( onClick_props === undefined && ( isInputFocused || getInputValue ( ) !== "" ) ) {
139+ if ( onClick_props === undefined && ( getIsInputFocused ( ) || getInputValue ( ) !== "" ) ) {
169140 return null ;
170141 }
171142
0 commit comments