1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
| <template>
| <ConfigProvider :locale="getAntdLocale">
| <Modal v-bind="getProps">
| <Spin :spinning="loading">
| <div style="padding: 20px;">
| <div v-html="options.content" style="margin-bottom: 8px"></div>
| <BasicForm @register="registerForm">
| <template #customInput="{ model, field }">
| <Input ref="inputRef" v-model:value="model[field]" :placeholder="placeholder" @pressEnter="onSubmit" @input="onChange" />
| </template>
| </BasicForm>
| </div>
| </Spin>
| </Modal>
| </ConfigProvider>
| </template>
|
| <script lang="ts">
| import type { JPromptProps } from './typing';
| import type { ModalProps } from '/@/components/Modal';
| import { ref, defineComponent, computed, unref, onMounted, nextTick } from 'vue';
| import { BasicForm, useForm } from '/@/components/Form';
| import { Modal, Spin, Input, ConfigProvider } from 'ant-design-vue';
| import { useLocale } from '/@/locales/useLocale';
|
| export default defineComponent({
| name: 'JPrompt',
| components: {
| Modal,
| Spin,
| Input,
| BasicForm,
| ConfigProvider,
| },
| emits: ['register'],
| setup(props, { emit }) {
| const inputRef = ref();
| const { getAntdLocale } = useLocale();
| const visible = ref(false);
| // 当前是否正在加载中
| const loading = ref(false);
| const options = ref<JPromptProps>({});
| const placeholder = computed(() => options.value.placeholder ?? '请输入内容');
| // 注册表单
| const [registerForm, { clearValidate, setFieldsValue, validate, updateSchema }] = useForm({
| compact: true,
| wrapperCol: { span: 24 },
| schemas: [
| {
| label: '',
| field: 'input',
| component: 'Input',
| slot: 'customInput',
| },
| ],
| showActionButtonGroup: false,
| });
|
| // 弹窗最终props
| const getProps = computed(() => {
| let opt = options.value;
| let modalProps: Partial<ModalProps> = {
| width: (opt.width ?? 500) as number,
| title: (opt.title ?? 'prompt') as string,
| visible: unref(visible),
| confirmLoading: unref(loading),
| };
| let finalProps: Recordable = {
| ...modalProps,
| ...props,
| ...opt,
| onOk: onSubmit,
| onCancel() {
| if (typeof options.value.onCancel === 'function') {
| options.value.onCancel();
| }
| close();
| },
| };
| return finalProps;
| });
|
| onMounted(() => {
| emit('register', {
| openModal,
| setLoading,
| getVisible: visible,
| });
| });
|
| /** 弹窗开启 */
| async function openModal(opt: any) {
| document.body.focus();
|
| options.value = opt;
| visible.value = true;
| await nextTick();
| await updateSchema({
| field: 'input',
| required: options.value.required,
| rules: options.value.rules,
| dynamicRules: options.value.dynamicRules,
| } as any);
| await setFieldsValue({
| input: options.value.defaultValue ?? '',
| });
| await clearValidate();
| inputRef.value?.focus();
| }
|
| /** 弹窗关闭 */
| function close() {
| visible.value = false;
| }
|
| function onChange() {
| validate()
| }
|
| /** 提交表单 */
| async function onSubmit() {
| try {
| const { onOk } = options.value;
| // 表单验证
| let values = await validate();
| setLoading(true);
| if (typeof onOk === 'function') {
| let flag = await onOk(values.input);
| // 只有返回 false 才阻止关闭弹窗
| if (!(flag === false)) {
| close();
| }
| } else {
| close();
| }
| } finally {
| setLoading(false);
| }
| }
|
| /** 设置加载状态*/
| function setLoading(flag) {
| loading.value = flag;
| }
|
| return {
| inputRef,
| getProps,
| loading,
| options,
| placeholder,
| getAntdLocale,
| onChange,
| onSubmit,
|
| registerForm,
| };
| },
| });
| </script>
|
|