Skip to content

Commit 884772a

Browse files
committed
provide native select component as fallback
1 parent 2577560 commit 884772a

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

pywebio/session/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ def set_env(**env_info):
517517
* ``input_auto_focus`` (bool): Whether to focus on input automatically after showing input panel, default is ``True``
518518
* ``output_max_width`` (str): The max width of the page content area (in pixel or percentage,
519519
e.g. ``'1080px'``, ``'80%'``. Default is 880px).
520+
* ``native_select`` (bool): Whether to use native select component, default is ``False`` . It's a fallback in case
521+
the default select component is not rendered correctly in some cases.
520522
521523
Example::
522524
@@ -528,7 +530,8 @@ def set_env(**env_info):
528530
"""
529531
from ..io_ctrl import send_msg
530532
assert all(k in ('title', 'output_animation', 'auto_scroll_bottom', 'http_pull_interval', 'output_max_width',
531-
'input_panel_min_height', 'input_panel_init_height', 'input_panel_fixed', 'input_auto_focus')
533+
'input_panel_min_height', 'input_panel_init_height', 'input_panel_fixed', 'input_auto_focus',
534+
'native_select')
532535
for k in env_info.keys())
533536
send_msg('set_env', spec=env_info)
534537

webiojs/src/handlers/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class EnvSettingHandler implements CommandHandler {
2626
}
2727
}
2828

29+
if (spec.native_select !== undefined) {
30+
config.disableSelectPicker = spec.native_select;
31+
}
32+
2933
if (spec.http_pull_interval !== undefined) {
3034
if (state.CurrentSession instanceof HttpSession)
3135
state.CurrentSession.change_pull_interval(spec.http_pull_interval);

webiojs/src/models/input/select.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {InputItem} from "./base";
22
import {deep_copy, make_set} from "../../utils"
3+
import {config} from "../../state";
34

45
const options_tpl = `
56
{{#options}}
@@ -36,8 +37,10 @@ export class Select extends InputItem {
3637
this.element = $(html);
3738
this.setup_select_options(this.element, spec.options);
3839

39-
// @ts-ignore
40-
this.element.find('select').selectpicker();
40+
if (!config.disableSelectPicker) {
41+
// @ts-ignore
42+
this.element.find('select').selectpicker();
43+
}
4144

4245
if (spec.onblur) {
4346
// blur事件时,发送当前值到服务器
@@ -68,8 +71,10 @@ export class Select extends InputItem {
6871
input_elem.attr(key, this.spec[key]);
6972
}
7073

71-
// @ts-ignore
72-
input_elem.selectpicker('refresh');
74+
if (!config.disableSelectPicker) {
75+
// @ts-ignore
76+
input_elem.selectpicker('refresh');
77+
}
7378
}
7479

7580
update_input(spec: any): any {
@@ -94,8 +99,10 @@ export class Select extends InputItem {
9499
$(this).prop('selected', true);
95100
}
96101
});
97-
// @ts-ignore
98-
this.element.find('select').selectpicker('render');
102+
if (!config.disableSelectPicker) {
103+
// @ts-ignore
104+
this.element.find('select').selectpicker('render');
105+
}
99106
delete attributes['value'];
100107
}
101108

@@ -104,10 +111,11 @@ export class Select extends InputItem {
104111

105112
on_reset(e: any) {
106113
// need to wait some time to get the select element be reset, and then update `selectpicker`
107-
setTimeout(() => {
108-
// @ts-ignore
109-
this.element.find('select').selectpicker('render');
110-
}, 100)
114+
if (!config.disableSelectPicker)
115+
setTimeout(() => {
116+
// @ts-ignore
117+
this.element.find('select').selectpicker('render');
118+
}, 100)
111119
}
112120

113121
get_value(): any {

webiojs/src/state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ export let state = {
77
ShowDuration: 200, // ms, 显示表单的过渡动画时长
88
InputPanelMinHeight: 300, // 输入panel的最小高度
99
InputPanelInitHeight: 300, // 输入panel的初始高度
10-
FixedInputPanel:true,
11-
AutoFocusOnInput:true,
10+
FixedInputPanel: true,
11+
AutoFocusOnInput: true,
1212
};
1313

1414
// App config
1515
export let config = {
1616
codeMirrorModeURL: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.52.2/mode/%N/%N.min.js",
1717
codeMirrorThemeURL: "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.52.2/theme/%N.min.css",
1818
outputAnimation: true, // 启用内容输出动画
19+
disableSelectPicker: false,
1920
httpPullInterval: 1000, // HttpSession 拉取消息的周期(ms)
2021
debug: false, // 调试模式, 打印所有交互的消息
2122
};

0 commit comments

Comments
 (0)