فهرست منبع

refine input event loop

reduce unnecessary update_input[valid_status=null] sending
wangweimin 2 سال پیش
والد
کامیت
c0c3447ac2
2فایلهای تغییر یافته به همراه11 افزوده شده و 7 حذف شده
  1. 6 6
      pywebio/io_ctrl.py
  2. 5 1
      webiojs/src/models/input/base.ts

+ 6 - 6
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)
 

+ 5 - 1
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;
         }