@@ -25,41 +25,36 @@ export interface TimeInputHookProps {
2525 defaultValue ?: TimeString ;
2626 onChange ?: ( time : TimeString ) => void ;
2727 onCommit : CommitHandler < HTMLInputElement , Date > ;
28- showTemplateWhileEditing ?: boolean ;
2928 value ?: TimeString ;
3029}
3130
3231export const useTimeInput = ( {
3332 defaultValue,
3433 onChange,
3534 onCommit,
36- showTemplateWhileEditing = true ,
3735 value,
3836} : TimeInputHookProps ) => {
3937 console . log ( `useTimeInput defaultValue = ${ defaultValue } value=${ value } ` ) ;
4038 const mousedDownRef = useRef ( false ) ;
41- const maskedInput = useMemo < MaskedInput > ( ( ) => {
42- const mask = new MaskedInput ( defaultValue , null , showTemplateWhileEditing ) ;
43- mask . on ( "change" , ( value ) => {
44- onChange ?.( value ) ;
45- } ) ;
46- return mask ;
47- } , [ defaultValue , onChange , showTemplateWhileEditing ] ) ;
48-
39+ const maskedInputRef = useRef < MaskedInput | undefined > ( undefined ) ;
4940 useMemo ( ( ) => {
50- if ( isValidTimeString ( value ) ) {
51- maskedInput . value = value ;
41+ if ( maskedInputRef . current === undefined ) {
42+ maskedInputRef . current = new MaskedInput ( defaultValue , null ) ;
43+ maskedInputRef . current . on ( "change" , ( value ) => {
44+ onChange ?.( value ) ;
45+ } ) ;
5246 }
53- } , [ maskedInput , value ] ) ;
5447
55- const setInputEl = useCallback < RefCallback < HTMLInputElement > > (
56- ( el ) => {
57- if ( el ) {
58- maskedInput . input = el ;
59- }
60- } ,
61- [ maskedInput ] ,
62- ) ;
48+ if ( isValidTimeString ( value ) && value !== maskedInputRef . current ?. value ) {
49+ maskedInputRef . current . value = value ;
50+ }
51+ } , [ defaultValue , onChange , value ] ) ;
52+
53+ const setInputEl = useCallback < RefCallback < HTMLInputElement > > ( ( el ) => {
54+ if ( el && maskedInputRef . current ) {
55+ maskedInputRef . current . input = el ;
56+ }
57+ } , [ ] ) ;
6358 const back = useRef ( false ) ;
6459
6560 const commitValue = useCallback < CommitHandler < HTMLInputElement , string > > (
@@ -79,49 +74,51 @@ export const useTimeInput = ({
7974
8075 const handleKeyDown = useCallback < KeyboardEventHandler < HTMLInputElement > > (
8176 ( e ) => {
82- console . log ( `handleKeyDown ${ e . key } cursorPos ${ maskedInput . cursorPos } ` ) ;
83- if ( e . key === "Backspace" ) {
84- maskedInput . backspace ( ) ;
85- back . current = true ;
86- } else if ( isDigit ( e . key ) ) {
87- maskedInput . update ( e . key ) ;
88- } else if ( e . key === "ArrowLeft" ) {
89- maskedInput . moveFocus ( "left" ) ;
90- } else if ( e . key === "ArrowRight" ) {
91- maskedInput . moveFocus ( "right" ) ;
92- } else if ( e . key === "ArrowUp" ) {
93- maskedInput . incrementValue ( ) ;
94- } else if ( e . key === "ArrowDown" ) {
95- maskedInput . decrementValue ( ) ;
96- } else if ( e . key === "v" && e . metaKey ) {
97- // keyboard paste, do not prevent default
98- return ;
99- } else if ( e . key === "Tab" ) {
100- return ;
101- } else if ( e . key === "Enter" ) {
102- commitValue ( e , maskedInput . value ) ;
77+ const { current : maskedInput } = maskedInputRef ;
78+ if ( maskedInput ) {
79+ if ( e . key === "Backspace" ) {
80+ maskedInput . backspace ( ) ;
81+ back . current = true ;
82+ } else if ( isDigit ( e . key ) ) {
83+ maskedInput . update ( e . key ) ;
84+ } else if ( e . key === "ArrowLeft" ) {
85+ maskedInput . moveFocus ( "left" ) ;
86+ } else if ( e . key === "ArrowRight" ) {
87+ maskedInput . moveFocus ( "right" ) ;
88+ } else if ( e . key === "ArrowUp" ) {
89+ maskedInput . incrementValue ( ) ;
90+ } else if ( e . key === "ArrowDown" ) {
91+ maskedInput . decrementValue ( ) ;
92+ } else if ( e . key === "v" && e . metaKey ) {
93+ // keyboard paste, do not prevent default
94+ return ;
95+ } else if ( e . key === "Tab" ) {
96+ return ;
97+ } else if ( e . key === "Enter" ) {
98+ commitValue ( e , maskedInput . value ) ;
99+ }
103100 }
104101 e . preventDefault ( ) ;
105102 } ,
106- [ commitValue , maskedInput ] ,
103+ [ commitValue ] ,
107104 ) ;
108105
109106 const handleClick = useCallback ( ( ) => {
110107 // maskedInput.click();
111108 } , [ ] ) ;
112109
113110 const handleDoubleClick = useCallback ( ( ) => {
114- maskedInput . doubleClick ( ) ;
115- } , [ maskedInput ] ) ;
111+ maskedInputRef . current ? .doubleClick ( ) ;
112+ } , [ ] ) ;
116113
117114 const handlePaste = useCallback < ClipboardEventHandler < HTMLInputElement > > (
118115 ( e ) => {
119116 const value = e . clipboardData . getData ( "text" ) ;
120117 if ( isValidTimeString ( value ) ) {
121- maskedInput . pasteValue ( value ) ;
118+ maskedInputRef . current ? .pasteValue ( value ) ;
122119 }
123120 } ,
124- [ maskedInput ] ,
121+ [ ] ,
125122 ) ;
126123
127124 const handleChange = useCallback < ChangeEventHandler < HTMLInputElement > > (
@@ -135,9 +132,9 @@ export const useTimeInput = ({
135132 if ( mousedDownRef . current ) {
136133 mousedDownRef . current = false ;
137134 } else {
138- maskedInput . focus ( ) ;
135+ maskedInputRef . current ? .focus ( ) ;
139136 }
140- } , [ maskedInput ] ) ;
137+ } , [ ] ) ;
141138
142139 const handleMouseDown = useCallback < MouseEventHandler > ( ( e ) => {
143140 mousedDownRef . current = true ;
@@ -157,15 +154,15 @@ export const useTimeInput = ({
157154 if ( input . selectionStart === 0 && input . selectionEnd === 8 ) {
158155 console . log ( "full select" ) ;
159156 }
160- maskedInput . click ( ) ;
157+ maskedInputRef . current ? .click ( ) ;
161158 } ,
162- [ maskedInput ] ,
159+ [ ] ,
163160 ) ;
164161
165162 return {
166163 inputRef : setInputEl ,
167164 eventHandlers : {
168- onBlur : maskedInput . blur ,
165+ onBlur : maskedInputRef . current ? .blur ,
169166 onChange : handleChange ,
170167 onClick : handleClick ,
171168 onDoubleClick : handleDoubleClick ,
0 commit comments