@form-create/iview 2.6.3 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/core/api.js CHANGED
@@ -11,7 +11,7 @@ function tidyBtnProp(btn, def) {
11
11
  }
12
12
 
13
13
  export default function extendApi(api, h) {
14
- extend(api, {
14
+ return {
15
15
  formEl() {
16
16
  return h.$manager.form();
17
17
  },
@@ -37,16 +37,14 @@ export default function extendApi(api, h) {
37
37
  })
38
38
  });
39
39
  },
40
- validateField: (field, callback) => {
40
+ validateField(field, callback) {
41
41
  return new Promise((resolve, reject) => {
42
42
  const ctx = h.getFieldCtx(field);
43
43
  if (!ctx) return;
44
44
  const sub = h.subForm[ctx.id];
45
45
  const all = [h.$manager.validateField(ctx.id)];
46
46
  toArray(sub).forEach(v => {
47
- all.push(v.validate().catch(() => {
48
- return Promise.reject('子表单验证未通过');
49
- }));
47
+ all.push(v.validate());
50
48
  })
51
49
  Promise.all(all).then(() => {
52
50
  resolve(null);
@@ -116,21 +114,18 @@ export default function extendApi(api, h) {
116
114
  api.refreshOptions();
117
115
  },
118
116
  submit(successFn, failFn) {
119
- api.validate((valid) => {
120
- if (valid) {
117
+ return new Promise((resolve, reject) => {
118
+ api.validate().then(() => {
121
119
  let formData = api.formData();
122
- if (is.Function(successFn))
123
- invoke(() => successFn(formData, this));
124
- else {
125
- is.Function(h.options.onSubmit) && invoke(() => h.options.onSubmit(formData, this));
126
- h.vm.$emit('submit', formData, this);
127
- }
128
- } else {
129
- is.Function(failFn) && invoke(() => failFn(this, ...arguments));
130
- }
131
- }).catch(e=>{});
120
+ is.Function(successFn) && invoke(() => successFn(formData, api));
121
+ is.Function(h.options.onSubmit) && invoke(() => h.options.onSubmit(formData, api));
122
+ h.vm.emit('submit', formData, api);
123
+ resolve(formData);
124
+ }).catch((...args) => {
125
+ is.Function(failFn) && invoke(() => failFn(api, ...args));
126
+ reject(...args)
127
+ })
128
+ });
132
129
  },
133
- });
134
-
135
- return api;
130
+ };
136
131
  }
package/src/core/index.js CHANGED
@@ -6,6 +6,7 @@ import FormCreateFactory from '@form-create/core/src/index';
6
6
  import makers from './maker';
7
7
  import '../style/index.css';
8
8
  import extendApi from './api';
9
+ import required from "./provider";
9
10
 
10
11
  function install(FormCreate) {
11
12
  FormCreate.componentAlias(alias);
@@ -14,6 +15,8 @@ function install(FormCreate) {
14
15
  FormCreate.component(component.name, component);
15
16
  });
16
17
 
18
+ FormCreate.register(required);
19
+
17
20
  parsers.forEach((parser) => {
18
21
  FormCreate.parser(parser);
19
22
  });
@@ -1,6 +1,7 @@
1
1
  import getConfig, {info, iviewConfig} from './config';
2
2
  import mergeProps from '@form-create/utils/lib/mergeprops';
3
3
  import is, {hasProperty} from '@form-create/utils/lib/type';
4
+ import toString from '@form-create/utils/lib/tostring';
4
5
  import extend from '@form-create/utils/lib/extend';
5
6
 
6
7
  function tidy(props, name) {
@@ -20,28 +21,32 @@ function tidyBool(opt, name) {
20
21
  }
21
22
  }
22
23
 
24
+ function tidyRule(rule) {
25
+ const _rule = {...rule};
26
+ delete _rule.children;
27
+ return _rule;
28
+ }
29
+
23
30
  export default {
24
31
  validate() {
25
32
  const form = this.form();
26
33
  if (form) {
27
- return new Promise((resolve, reject) => {
28
- form.validate().then(flag => {
29
- flag ? resolve(true) : reject(false);
30
- })
31
- });
34
+ return form.validate();
35
+ } else {
36
+ return new Promise(v => v());
32
37
  }
33
- return new Promise(v => v());
34
38
  },
35
39
  validateField(field) {
36
- const form = this.form();
37
- if (form) {
38
- return new Promise((resolve, reject) => {
39
- form.validateField(field, e => {
40
- e ? reject(e) : resolve(null);
41
- })
42
- })
43
- }
44
- return new Promise(v => v());
40
+ return new Promise((resolve, reject) => {
41
+ const form = this.form();
42
+ if (form) {
43
+ form.validateField(field, (res, err) => {
44
+ err ? reject(err) : resolve(res);
45
+ });
46
+ } else {
47
+ resolve();
48
+ }
49
+ });
45
50
  },
46
51
  clearValidateState(ctx) {
47
52
  const fItem = this.vm.$refs[ctx.wrapRef];
@@ -62,17 +67,15 @@ export default {
62
67
  return prop;
63
68
  },
64
69
  mergeProp(ctx) {
65
- ctx.prop = mergeProps([{
66
- info: this.options.info || {},
67
- wrap: this.options.wrap || {},
68
- col: this.options.col || {},
69
- title: this.options.title || {},
70
- }, ctx.prop], {
70
+ const def = {
71
71
  info: info(),
72
72
  title: {},
73
73
  col: {span: 24},
74
74
  wrap: {},
75
- }, {normal: ['title', 'info', 'col', 'wrap']});
75
+ };
76
+ ['info', 'wrap', 'col', 'title'].forEach(name => {
77
+ ctx.prop[name] = mergeProps([this.options[name] || {}, ctx.prop[name] || {}], def[name]);
78
+ });
76
79
  this.setSize(ctx.prop.props);
77
80
  },
78
81
  setSize(props) {
@@ -92,7 +95,7 @@ export default {
92
95
  e.preventDefault();
93
96
  }
94
97
  },
95
- class: [form.className, form.class, 'form-create'],
98
+ class: [form.className, form.class, 'form-create', this.$handle.preview ? 'is-preview' : ''],
96
99
  style: form.style,
97
100
  type: 'form',
98
101
  };
@@ -102,34 +105,34 @@ export default {
102
105
  extend(this.rule, {key, ref});
103
106
  extend(this.rule.props, {
104
107
  model: $handle.formData,
105
- // rules: $handle.validate(),
106
108
  });
107
109
  },
108
110
  render(children) {
109
- if (children.length) {
110
- children.push(this.makeFormBtn());
111
+ if (children.slotLen() && !this.$handle.preview) {
112
+ children.setSlot(undefined, () => this.makeFormBtn());
111
113
  }
112
- return this.$r(this.rule, isFalse(this.options.row.show) ? children : [this.makeRow(children)]);
114
+ return this.$r(this.rule, isFalse(this.options.row.show) ? children.getSlots() : [this.makeRow(children)]);
113
115
  },
114
116
  makeWrap(ctx, children) {
115
117
  const rule = ctx.prop;
116
118
  const uni = `${this.key}${ctx.key}`;
117
119
  const col = rule.col;
118
- const isTitle = this.isTitle(rule);
120
+ const isTitle = this.isTitle(rule) && rule.wrap.title !== false;
119
121
  const labelWidth = (!col.labelWidth && !isTitle) ? 0 : col.labelWidth;
120
122
  const {inline, col: _col} = this.rule.props;
123
+ delete rule.wrap.title;
121
124
  const item = isFalse(rule.wrap.show) ? children : this.$r(mergeProps([rule.wrap, {
122
125
  props: {
123
126
  labelWidth,
124
- ...(rule.wrap || {}),
127
+ ...tidyRule(rule.wrap || {}),
125
128
  prop: ctx.id,
126
129
  rules: ctx.injectValidate(),
127
130
  },
128
- class: rule.className,
131
+ class: this.$render.mergeClass(rule.className, 'fc-form-item'),
129
132
  key: `${uni}fi`,
130
133
  ref: ctx.wrapRef,
131
134
  type: 'formItem',
132
- }]), [children, isTitle ? this.makeInfo(rule, uni, ctx) : null]);
135
+ }]), {default: () => children, ...(isTitle ? {label: () => this.makeInfo(rule, uni, ctx)} : {})});
133
136
  return (inline === true || isFalse(_col) || isFalse(col.show)) ? item : this.makeCol(rule, uni, [item]);
134
137
  },
135
138
  isTitle(rule) {
@@ -138,21 +141,33 @@ export default {
138
141
  return !((!title.title && !title.native) || isFalse(title.show))
139
142
  },
140
143
  makeInfo(rule, uni, ctx) {
141
- const titleProp = rule.title;
142
- const infoProp = rule.info;
144
+ const titleProp = {...rule.title};
145
+ const infoProp = {...rule.info};
146
+ const form = this.options.form;
143
147
  const titleSlot = this.getSlot('title');
144
- const children = [titleSlot ? titleSlot({title: titleProp.title || '', rule: ctx.rule, options: this.options}) : titleProp.title];
148
+ const children = [titleSlot ? titleSlot({
149
+ title: ctx.refRule.__$title.value,
150
+ rule: ctx.rule,
151
+ options: this.options
152
+ }) : ((ctx.refRule.__$title.value) + (form.labelSuffix || form['label-suffix'] || ''))];
145
153
 
146
154
  if (!isFalse(infoProp.show) && (infoProp.info || infoProp.native) && !isFalse(infoProp.icon)) {
147
155
  const prop = {
148
156
  type: infoProp.type || 'poptip',
149
- props: {...infoProp},
157
+ props: tidyRule(infoProp),
150
158
  key: `${uni}pop`,
151
159
  slot: 'label'
152
160
  };
161
+
162
+ delete prop.props.icon;
163
+ delete prop.props.show;
164
+ delete prop.props.info;
165
+ delete prop.props.align;
166
+ delete prop.props.native;
167
+
153
168
  const field = 'content';
154
169
  if (infoProp.info && !hasProperty(prop.props, field)) {
155
- prop.props[field] = infoProp.info;
170
+ prop.props[field] = ctx.refRule.__$info.value;
156
171
  }
157
172
  children[infoProp.align !== 'left' ? 'unshift' : 'push'](this.$r(mergeProps([infoProp, prop]), [
158
173
  this.$r({
@@ -163,17 +178,24 @@ export default {
163
178
  })
164
179
  ]));
165
180
  }
166
- return this.$r(mergeProps([titleProp, {
167
- props: titleProp,
168
- slot: titleProp.slot || 'label',
181
+ const _prop = mergeProps([titleProp, {
182
+ props: tidyRule(titleProp),
169
183
  key: `${uni}tit`,
184
+ class: 'fc-form-title',
185
+ slot: titleProp.slot || 'label',
170
186
  type: titleProp.type || 'span',
171
- }]), children);
187
+ }]);
188
+
189
+ delete _prop.props.show;
190
+ delete _prop.props.title;
191
+ delete _prop.props.native;
192
+
193
+ return this.$r(_prop, children);
172
194
  },
173
195
  makeCol(rule, uni, children) {
174
196
  const col = rule.col;
175
197
  return this.$r({
176
- class: col.class,
198
+ class: this.$render.mergeClass(col.class, 'fc-form-col'),
177
199
  type: 'col',
178
200
  props: col || {span: 24},
179
201
  key: `${uni}col`
@@ -184,7 +206,7 @@ export default {
184
206
  return this.$r({
185
207
  type: 'row',
186
208
  props: row,
187
- class: row.class,
209
+ class: this.$render.mergeClass(row.class, 'fc-form-row'),
188
210
  key: `${this.key}row`
189
211
  }, children)
190
212
  },
@@ -201,6 +223,7 @@ export default {
201
223
  }
202
224
  const item = this.$r({
203
225
  type: 'formItem',
226
+ class: 'fc-form-item',
204
227
  key: `${this.key}fb`
205
228
  }, vn);
206
229
 
@@ -208,44 +231,57 @@ export default {
208
231
  ? item
209
232
  : this.$r({
210
233
  type: 'col',
234
+ class: 'fc-form-col fc-form-footer',
211
235
  props: {span: 24},
212
236
  key: `${this.key}fc`
213
237
  }, [item]);
214
238
  },
239
+
215
240
  makeResetBtn() {
216
- const resetBtn = this.options.resetBtn;
217
- this.setSize(resetBtn);
241
+ const resetBtn = {...this.options.resetBtn};
242
+ const innerText = resetBtn.innerText;
243
+ delete resetBtn.innerText;
244
+ delete resetBtn.click;
245
+ delete resetBtn.col;
246
+ delete resetBtn.show;
218
247
  return this.$r({
219
248
  type: 'button',
220
249
  props: resetBtn,
221
- style: {width: resetBtn.width, marginLeft: '15px'},
250
+ class: 'fc-reset-btn',
251
+ style: {width: resetBtn.width},
222
252
  on: {
223
253
  click: () => {
224
254
  const fApi = this.$handle.api;
225
- resetBtn.click
226
- ? resetBtn.click(fApi)
255
+ this.options.resetBtn.click
256
+ ? this.options.resetBtn.click(fApi)
227
257
  : fApi.resetFields();
228
258
  }
229
259
  },
230
260
  key: `${this.key}b2`,
231
- }, [resetBtn.innerText]);
261
+ }, [innerText]);
232
262
  },
233
263
  makeSubmitBtn() {
234
- const submitBtn = this.options.submitBtn;
235
- this.setSize(submitBtn);
264
+ const submitBtn = {...this.options.submitBtn};
265
+ const innerText = submitBtn.innerText;
266
+ delete submitBtn.innerText;
267
+ delete submitBtn.click;
268
+ delete submitBtn.col;
269
+ delete submitBtn.show;
236
270
  return this.$r({
237
271
  type: 'button',
238
272
  props: submitBtn,
273
+ class: 'fc-submit-btn',
239
274
  style: {width: submitBtn.width},
240
275
  on: {
241
276
  click: () => {
242
277
  const fApi = this.$handle.api;
243
- submitBtn.click
244
- ? submitBtn.click(fApi)
245
- : fApi.submit();
278
+ this.options.submitBtn.click
279
+ ? this.options.submitBtn.click(fApi)
280
+ : fApi.submit().catch(() => {
281
+ });
246
282
  }
247
283
  },
248
284
  key: `${this.key}b1`,
249
- }, [submitBtn.innerText]);
285
+ }, [innerText]);
250
286
  }
251
287
  }
@@ -0,0 +1,52 @@
1
+ import is from '@form-create/utils/lib/type';
2
+
3
+ const required = {
4
+ name: 'required',
5
+ load(inject, rule, api) {
6
+ const val = parseVal(inject.getValue());
7
+ if (val.required === false) {
8
+ inject.clearProp();
9
+ api.clearValidateState([rule.field]);
10
+ } else {
11
+ const validate = {
12
+ required: true,
13
+ validator(_, v, call) {
14
+ is.empty(v) ? call(validate.message) : call();
15
+ },
16
+ ...val,
17
+ };
18
+ if (!validate.message) {
19
+ validate.message = rule.__fc__.refRule.__$title.value + (api.getLocale() === 'en' ? ' is required' : '不能为空');
20
+ } else {
21
+ const match = validate.message.match(/^\{\{\s*\$t\.(.+)\s*\}\}$/);
22
+ if (match) {
23
+ validate.message = api.t(match[1], {title: rule.__fc__.refRule.__$title.value});
24
+ }
25
+ }
26
+ inject.getProp().validate = [validate];
27
+ }
28
+ api.sync(rule);
29
+ },
30
+ watch(...args) {
31
+ required.load(...args);
32
+ }
33
+ }
34
+
35
+ function parseVal(val) {
36
+ if (is.Boolean(val)) {
37
+ return {required: val}
38
+ } else if (is.String(val)) {
39
+ return {message: val};
40
+ } else if (is.Undef(val)) {
41
+ return {required: false};
42
+ } else if (is.Function(val)) {
43
+ return {validator: val};
44
+ } else if (!is.Object(val)) {
45
+ return {};
46
+ } else {
47
+ return val;
48
+ }
49
+ }
50
+
51
+
52
+ export default required