Skip to content

Commit c0c3447

Browse files
committed
refine input event loop
reduce unnecessary update_input[valid_status=null] sending
1 parent 45eea48 commit c0c3447

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

pywebio/io_ctrl.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def input_control(spec, preprocess_funcs, item_valid_funcs, onchange_funcs, form
280280
return data
281281

282282

283-
def check_item(name, data, valid_func, preprocess_func):
283+
def check_item(name, data, valid_func, preprocess_func, clear_invalid=False):
284284
try:
285285
data = preprocess_func(data)
286286
error_msg = valid_func(data)
@@ -295,7 +295,7 @@ def check_item(name, data, valid_func, preprocess_func):
295295
'invalid_feedback': error_msg
296296
}))
297297
return False
298-
else:
298+
elif clear_invalid:
299299
send_msg('update_input', dict(target_name=name, attributes={
300300
'valid_status': 0, # valid_status为0表示清空valid_status标志
301301
}))
@@ -334,6 +334,7 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
334334
:param onchange_funcs: map(name -> onchange_func)
335335
:return:
336336
"""
337+
data = None
337338
while True:
338339
event = yield next_client_event()
339340
event_name, event_data = event['event'], event['data']
@@ -342,7 +343,7 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
342343
if input_event == 'blur':
343344
onblur_name = event_data['name']
344345
check_item(onblur_name, event_data['value'], item_valid_funcs[onblur_name],
345-
preprocess_funcs[onblur_name])
346+
preprocess_funcs[onblur_name], clear_invalid=True)
346347
elif input_event == 'change':
347348
trigger_onchange(event_data, onchange_funcs)
348349

@@ -375,10 +376,9 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
375376
}))
376377

377378
if all_valid:
378-
break
379+
break # form event loop
379380
elif event_name == 'from_cancel':
380-
data = None
381-
break
381+
break # break event loop
382382
else:
383383
logger.warning("Unhandled Event: %s", event)
384384

webiojs/src/models/input/base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class InputItem {
2929

3030
// 检查输入项的有效性,在表单提交时调用
3131
check_valid(): boolean {
32+
this.update_input_helper(-1, {
33+
'valid_status': 0, // remove the valid status
34+
});
3235
return true;
3336
}
3437

@@ -69,7 +72,8 @@ export class InputItem {
6972

7073
if ('valid_status' in attributes) {
7174
let class_name = attributes.valid_status ? 'is-valid' : 'is-invalid';
72-
if (attributes.valid_status === 0) class_name = ''; // valid_status为0时,表示清空valid_status标志
75+
// valid_status为0/null时,表示清空valid_status标志
76+
if (attributes.valid_status === 0 || attributes.valid_status === null) class_name = '';
7377
input_elem.removeClass('is-valid is-invalid').addClass(class_name);
7478
delete attributes.valid_status;
7579
}

0 commit comments

Comments
 (0)