@@ -49,7 +49,7 @@ export class InputComponent implements LanguageUpdateable {
4949 * @param onChange 值变化回调
5050 * @returns HTMLElement
5151 */
52- render ( id : string , placeholder ?: string , value ?: string , label ?: string , onChange ?: ( value : string ) => void ) : HTMLElement {
52+ render ( id : string , value ?: string , placeholder ?: string , label ?: string , onChange ?: ( value : string ) => void ) : HTMLElement {
5353 this . currentPlaceholder = placeholder ;
5454 this . currentLabel = label ;
5555
@@ -78,15 +78,31 @@ export class InputComponent implements LanguageUpdateable {
7878 // 为输入框设置特殊类名,用于识别真正为空的输入框
7979 input . classList . add ( 'js-script-hook-empty-input' ) ;
8080
81+ // 先设置placeholder, 这样可以在后续逻辑中进行比较
8182 if ( placeholder ) {
8283 input . placeholder = placeholder ;
8384 }
8485
85- // 只有当value是有效值时,才设置value并移除空输入框标记
86- if ( value !== undefined && value !== null && value !== '' ) {
87- input . value = value ;
88- input . classList . remove ( 'js-script-hook-empty-input' ) ;
89- }
86+ // 当焦点进入时,清除可能被错误填充的placeholder文本
87+ input . addEventListener ( 'focus' , function ( ) {
88+ // 如果输入框的值与placeholder相同,或者被标记为空,则清空值
89+ const currentPlaceholder = this . placeholder || '' ;
90+ if ( ( this . value === currentPlaceholder && this . value !== '' ) ||
91+ ( this . classList . contains ( 'js-script-hook-empty-input' ) && this . value !== '' ) ) {
92+ // 清空并保持聚焦
93+ this . value = '' ;
94+ inputLogger . debug ( `焦点事件: 清空了输入框 ${ id } 的值,可能是placeholder错误设置为了值` ) ;
95+ }
96+ } ) ;
97+
98+ // 当失去焦点时的处理
99+ input . addEventListener ( 'blur' , function ( ) {
100+ // 如果输入框为空,确保添加空输入框标记
101+ if ( this . value === '' ) {
102+ this . classList . add ( 'js-script-hook-empty-input' ) ;
103+ inputLogger . debug ( `失焦事件: 输入框 ${ id } 为空,添加空输入标记` ) ;
104+ }
105+ } ) ;
90106
91107 // 添加事件处理
92108 if ( onChange ) {
@@ -104,14 +120,14 @@ export class InputComponent implements LanguageUpdateable {
104120 } ) ;
105121 }
106122
107- // 当焦点进入时,清除可能被错误填充的placeholder文本
108- input . addEventListener ( 'focus' , function ( ) {
109- // 如果输入框标记为空但有显示的值,说明可能是placeholder被错误地作为值
110- if ( this . classList . contains ( 'js-script-hook-empty-input' ) && this . value !== '' ) {
111- // 清空并聚焦
112- this . value = '' ;
113- }
114- } ) ;
123+ // 只有当value是有效值时,才设置value并移除空输入框标记
124+ if ( value !== undefined && value !== null && value !== '' ) {
125+ input . value = value ;
126+ input . classList . remove ( 'js-script-hook-empty-input' ) ;
127+ inputLogger . debug ( `设置输入框 ${ id } 的值为: ${ value } ` ) ;
128+ } else {
129+ inputLogger . debug ( `输入框 ${ id } 没有设置初始值,保持为空` ) ;
130+ }
115131
116132 this . containerElement . appendChild ( input ) ;
117133
0 commit comments