From c0c3447ac2d82216d1998ba8c61ba24c12a405a4 Mon Sep 17 00:00:00 2001 From: wangweimin Date: Fri, 27 Jan 2023 11:35:23 +0800 Subject: [PATCH] refine input event loop reduce unnecessary update_input[valid_status=null] sending --- pywebio/io_ctrl.py | 12 ++++++------ webiojs/src/models/input/base.ts | 6 +++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pywebio/io_ctrl.py b/pywebio/io_ctrl.py index 5bc9c710..a08de499 100644 --- a/pywebio/io_ctrl.py +++ b/pywebio/io_ctrl.py @@ -280,7 +280,7 @@ def input_control(spec, preprocess_funcs, item_valid_funcs, onchange_funcs, form return data -def check_item(name, data, valid_func, preprocess_func): +def check_item(name, data, valid_func, preprocess_func, clear_invalid=False): try: data = preprocess_func(data) error_msg = valid_func(data) @@ -295,7 +295,7 @@ def check_item(name, data, valid_func, preprocess_func): 'invalid_feedback': error_msg })) return False - else: + elif clear_invalid: send_msg('update_input', dict(target_name=name, attributes={ 'valid_status': 0, # valid_status为0表示清空valid_status标志 })) @@ -334,6 +334,7 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc :param onchange_funcs: map(name -> onchange_func) :return: """ + data = None while True: event = yield next_client_event() 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 if input_event == 'blur': onblur_name = event_data['name'] check_item(onblur_name, event_data['value'], item_valid_funcs[onblur_name], - preprocess_funcs[onblur_name]) + preprocess_funcs[onblur_name], clear_invalid=True) elif input_event == 'change': trigger_onchange(event_data, onchange_funcs) @@ -375,10 +376,9 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc })) if all_valid: - break + break # form event loop elif event_name == 'from_cancel': - data = None - break + break # break event loop else: logger.warning("Unhandled Event: %s", event) diff --git a/webiojs/src/models/input/base.ts b/webiojs/src/models/input/base.ts index 48a52f16..44ac1bae 100644 --- a/webiojs/src/models/input/base.ts +++ b/webiojs/src/models/input/base.ts @@ -29,6 +29,9 @@ export class InputItem { // 检查输入项的有效性,在表单提交时调用 check_valid(): boolean { + this.update_input_helper(-1, { + 'valid_status': 0, // remove the valid status + }); return true; } @@ -69,7 +72,8 @@ export class InputItem { if ('valid_status' in attributes) { let class_name = attributes.valid_status ? 'is-valid' : 'is-invalid'; - if (attributes.valid_status === 0) class_name = ''; // valid_status为0时,表示清空valid_status标志 + // valid_status为0/null时,表示清空valid_status标志 + if (attributes.valid_status === 0 || attributes.valid_status === null) class_name = ''; input_elem.removeClass('is-valid is-invalid').addClass(class_name); delete attributes.valid_status; }