Skip to content

Commit

Permalink
refine input event loop
Browse files Browse the repository at this point in the history
reduce unnecessary update_input[valid_status=null] sending
  • Loading branch information
wang0618 committed Jan 27, 2023
1 parent 45eea48 commit c0c3447
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pywebio/io_ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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标志
}))
Expand Down Expand Up @@ -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']
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion webiojs/src/models/input/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class InputItem {

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

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c0c3447

Please sign in to comment.