@blueking/bkui-form 1.0.1-beta.1 → 1.0.1-beta.3
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/esm/adapter/common/FieldGroupWrap.tsx.js +199 -0
- package/esm/adapter/field/ArrayField.tsx.js +55 -0
- package/esm/adapter/field/BooleanField.tsx.js +41 -0
- package/esm/adapter/field/CompositionField.tsx.js +113 -0
- package/esm/adapter/field/FieldProps.ts.js +85 -0
- package/esm/adapter/field/NumberField.tsx.js +19 -0
- package/esm/adapter/field/ObjectField.tsx.js +58 -0
- package/esm/adapter/field/SchemaField.tsx.js +28 -0
- package/esm/adapter/field/StringField.tsx.js +4 -0
- package/esm/adapter/widget/ArrayWidget.ts.js +196 -0
- package/esm/adapter/widget/ButtonWidget.ts.js +76 -0
- package/esm/adapter/widget/CheckboxWidget.ts.js +101 -0
- package/esm/adapter/widget/CollapseGroupWidget.ts.js +140 -0
- package/esm/adapter/widget/ColorPickerWidget.ts.js +71 -0
- package/esm/adapter/widget/InputWidget.ts.js +79 -0
- package/esm/adapter/widget/KeyValueArrayWidget.tsx.js +156 -0
- package/esm/adapter/widget/RadioWidget.ts.js +98 -0
- package/esm/adapter/widget/SelectWidget.ts.js +98 -0
- package/esm/adapter/widget/SwitchWidget.ts.js +73 -0
- package/esm/adapter/widget/TabGroupWidget.ts.js +145 -0
- package/esm/adapter/widget/TableWidget.ts.js +205 -0
- package/esm/adapter/widget/Widget.ts.js +497 -0
- package/esm/assets/fonts/iconcool.eot +0 -0
- package/esm/assets/fonts/iconcool.ttf +0 -0
- package/esm/assets/fonts/iconcool.woff +0 -0
- package/esm/assets/style.css +43 -0
- package/esm/controller/form.css +164 -0
- package/esm/controller/form.tsx.js +159 -0
- package/esm/controller/props.ts.js +75 -0
- package/esm/core/events.ts.js +74 -0
- package/esm/core/expression.ts.js +67 -0
- package/esm/core/lang.ts.js +25 -0
- package/esm/core/layout.ts.js +149 -0
- package/esm/core/path.ts.js +60 -0
- package/esm/core/proxy.ts.js +10 -0
- package/esm/core/reaction.ts.js +116 -0
- package/esm/core/register.ts.js +74 -0
- package/esm/core/schema.ts.js +302 -0
- package/esm/core/validator.ts.js +248 -0
- package/esm/core/widgetTree.ts.js +163 -0
- package/esm/index.ts.js +6 -0
- package/esm/node_modules/@blueking/cli-service/dist/lib/path.js +35 -0
- package/esm/node_modules/@blueking/cli-service/dist/lib/util.js +233 -0
- package/esm/node_modules/@blueking/cli-service/dist/tools/rust/helper/path.js +172 -0
- package/esm/node_modules/ajv/dist/ajv.js +50 -0
- package/esm/node_modules/ajv/dist/compile/codegen/code.js +156 -0
- package/esm/node_modules/ajv/dist/compile/codegen/index.js +697 -0
- package/esm/node_modules/ajv/dist/compile/codegen/scope.js +143 -0
- package/esm/node_modules/ajv/dist/compile/errors.js +123 -0
- package/esm/node_modules/ajv/dist/compile/index.js +242 -0
- package/esm/node_modules/ajv/dist/compile/names.js +28 -0
- package/esm/node_modules/ajv/dist/compile/ref_error.js +12 -0
- package/esm/node_modules/ajv/dist/compile/resolve.js +155 -0
- package/esm/node_modules/ajv/dist/compile/rules.js +26 -0
- package/esm/node_modules/ajv/dist/compile/util.js +178 -0
- package/esm/node_modules/ajv/dist/compile/validate/applicability.js +19 -0
- package/esm/node_modules/ajv/dist/compile/validate/boolSchema.js +50 -0
- package/esm/node_modules/ajv/dist/compile/validate/dataType.js +203 -0
- package/esm/node_modules/ajv/dist/compile/validate/defaults.js +35 -0
- package/esm/node_modules/ajv/dist/compile/validate/index.js +520 -0
- package/esm/node_modules/ajv/dist/compile/validate/keyword.js +124 -0
- package/esm/node_modules/ajv/dist/compile/validate/subschema.js +81 -0
- package/esm/node_modules/ajv/dist/core.js +618 -0
- package/esm/node_modules/ajv/dist/refs/data.json +13 -0
- package/esm/node_modules/ajv/dist/refs/json-schema-draft-07.json +151 -0
- package/esm/node_modules/ajv/dist/runtime/equal.js +7 -0
- package/esm/node_modules/ajv/dist/runtime/ucs2length.js +24 -0
- package/esm/node_modules/ajv/dist/runtime/uri.js +6 -0
- package/esm/node_modules/ajv/dist/runtime/validation_error.js +11 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/additionalItems.js +49 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/additionalProperties.js +106 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/allOf.js +23 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/anyOf.js +12 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/contains.js +95 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/dependencies.js +85 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/if.js +66 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/index.js +44 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/items.js +52 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/items2020.js +30 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/not.js +26 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/oneOf.js +60 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/patternProperties.js +75 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/prefixItems.js +12 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/properties.js +54 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/propertyNames.js +38 -0
- package/esm/node_modules/ajv/dist/vocabularies/applicator/thenElse.js +13 -0
- package/esm/node_modules/ajv/dist/vocabularies/code.js +131 -0
- package/esm/node_modules/ajv/dist/vocabularies/core/id.js +10 -0
- package/esm/node_modules/ajv/dist/vocabularies/core/index.js +16 -0
- package/esm/node_modules/ajv/dist/vocabularies/core/ref.js +122 -0
- package/esm/node_modules/ajv/dist/vocabularies/discriminator/index.js +104 -0
- package/esm/node_modules/ajv/dist/vocabularies/discriminator/types.js +9 -0
- package/esm/node_modules/ajv/dist/vocabularies/draft7.js +17 -0
- package/esm/node_modules/ajv/dist/vocabularies/format/format.js +92 -0
- package/esm/node_modules/ajv/dist/vocabularies/format/index.js +6 -0
- package/esm/node_modules/ajv/dist/vocabularies/metadata.js +18 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/const.js +25 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/enum.js +48 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/index.js +33 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/limitItems.js +24 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/limitLength.js +27 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/limitNumber.js +27 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/limitProperties.js +24 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/multipleOf.js +26 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/pattern.js +24 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/required.js +79 -0
- package/esm/node_modules/ajv/dist/vocabularies/validation/uniqueItems.js +64 -0
- package/esm/node_modules/ajv-formats/dist/formats.js +173 -0
- package/esm/node_modules/ajv-formats/dist/index.js +37 -0
- package/esm/node_modules/ajv-formats/dist/limit.js +69 -0
- package/esm/node_modules/ajv-i18n/localize/zh/index.js +154 -0
- package/esm/node_modules/fast-deep-equal/index.js +46 -0
- package/esm/node_modules/fast-uri/index.js +303 -0
- package/esm/node_modules/fast-uri/lib/schemes.js +188 -0
- package/esm/node_modules/fast-uri/lib/scopedChars.js +30 -0
- package/esm/node_modules/fast-uri/lib/utils.js +244 -0
- package/esm/node_modules/json-schema-traverse/index.js +89 -0
- package/esm/node_modules/semver/classes/comparator.js +143 -0
- package/esm/node_modules/semver/classes/range.js +557 -0
- package/esm/node_modules/semver/classes/semver.js +333 -0
- package/esm/node_modules/semver/functions/clean.js +8 -0
- package/esm/node_modules/semver/functions/cmp.js +54 -0
- package/esm/node_modules/semver/functions/coerce.js +62 -0
- package/esm/node_modules/semver/functions/compare-build.js +9 -0
- package/esm/node_modules/semver/functions/compare-loose.js +5 -0
- package/esm/node_modules/semver/functions/compare.js +7 -0
- package/esm/node_modules/semver/functions/diff.js +60 -0
- package/esm/node_modules/semver/functions/eq.js +5 -0
- package/esm/node_modules/semver/functions/gt.js +5 -0
- package/esm/node_modules/semver/functions/gte.js +5 -0
- package/esm/node_modules/semver/functions/inc.js +21 -0
- package/esm/node_modules/semver/functions/lt.js +5 -0
- package/esm/node_modules/semver/functions/lte.js +5 -0
- package/esm/node_modules/semver/functions/major.js +5 -0
- package/esm/node_modules/semver/functions/minor.js +5 -0
- package/esm/node_modules/semver/functions/neq.js +5 -0
- package/esm/node_modules/semver/functions/parse.js +18 -0
- package/esm/node_modules/semver/functions/patch.js +5 -0
- package/esm/node_modules/semver/functions/prerelease.js +8 -0
- package/esm/node_modules/semver/functions/rcompare.js +5 -0
- package/esm/node_modules/semver/functions/rsort.js +5 -0
- package/esm/node_modules/semver/functions/satisfies.js +12 -0
- package/esm/node_modules/semver/functions/sort.js +5 -0
- package/esm/node_modules/semver/functions/valid.js +8 -0
- package/esm/node_modules/semver/index.js +91 -0
- package/esm/node_modules/semver/internal/constants.js +37 -0
- package/esm/node_modules/semver/internal/debug.js +11 -0
- package/esm/node_modules/semver/internal/identifiers.js +29 -0
- package/esm/node_modules/semver/internal/lrucache.js +42 -0
- package/esm/node_modules/semver/internal/parse-options.js +17 -0
- package/esm/node_modules/semver/internal/re.js +223 -0
- package/esm/node_modules/semver/ranges/gtr.js +6 -0
- package/esm/node_modules/semver/ranges/intersects.js +9 -0
- package/esm/node_modules/semver/ranges/ltr.js +6 -0
- package/esm/node_modules/semver/ranges/max-satisfying.js +27 -0
- package/esm/node_modules/semver/ranges/min-satisfying.js +26 -0
- package/esm/node_modules/semver/ranges/min-version.js +63 -0
- package/esm/node_modules/semver/ranges/outside.js +82 -0
- package/esm/node_modules/semver/ranges/simplify.js +49 -0
- package/esm/node_modules/semver/ranges/subset.js +249 -0
- package/esm/node_modules/semver/ranges/to-comparators.js +10 -0
- package/esm/node_modules/semver/ranges/valid.js +13 -0
- package/esm/util/fetch.ts.js +190 -0
- package/esm/util/index.ts.js +276 -0
- package/package.json +3 -4
- package/dist/main.css +0 -208
- package/dist/main.js +0 -17353
- /package/{dist/svg/iconcool.eb237688.svg → esm/assets/fonts/iconcool.svg} +0 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function _object_spread(target) {
|
|
15
|
+
for(var i = 1; i < arguments.length; i++){
|
|
16
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
17
|
+
var ownKeys = Object.keys(source);
|
|
18
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
19
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
20
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
ownKeys.forEach(function(key) {
|
|
24
|
+
_define_property(target, key, source[key]);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
29
|
+
function ownKeys(object, enumerableOnly) {
|
|
30
|
+
var keys = Object.keys(object);
|
|
31
|
+
if (Object.getOwnPropertySymbols) {
|
|
32
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
33
|
+
if (enumerableOnly) {
|
|
34
|
+
symbols = symbols.filter(function(sym) {
|
|
35
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
keys.push.apply(keys, symbols);
|
|
39
|
+
}
|
|
40
|
+
return keys;
|
|
41
|
+
}
|
|
42
|
+
function _object_spread_props(target, source) {
|
|
43
|
+
source = source != null ? source : {};
|
|
44
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
45
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
46
|
+
} else {
|
|
47
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
48
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return target;
|
|
52
|
+
}
|
|
53
|
+
function _object_without_properties(source, excluded) {
|
|
54
|
+
if (source == null) return {};
|
|
55
|
+
var target = _object_without_properties_loose(source, excluded);
|
|
56
|
+
var key, i;
|
|
57
|
+
if (Object.getOwnPropertySymbols) {
|
|
58
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
59
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
60
|
+
key = sourceSymbolKeys[i];
|
|
61
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
62
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
63
|
+
target[key] = source[key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return target;
|
|
67
|
+
}
|
|
68
|
+
function _object_without_properties_loose(source, excluded) {
|
|
69
|
+
if (source == null) return {};
|
|
70
|
+
var target = {};
|
|
71
|
+
var sourceKeys = Object.keys(source);
|
|
72
|
+
var key, i;
|
|
73
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
74
|
+
key = sourceKeys[i];
|
|
75
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
76
|
+
target[key] = source[key];
|
|
77
|
+
}
|
|
78
|
+
return target;
|
|
79
|
+
}
|
|
80
|
+
import { isObj, hasOwnProperty, valueType, initializationValue } from '../util/index.ts.js';
|
|
81
|
+
import registry from './register.ts.js';
|
|
82
|
+
import { resolveComponent } from 'vue';
|
|
83
|
+
class Schema {
|
|
84
|
+
static resolveRef() {}
|
|
85
|
+
static resolveDependencies() {}
|
|
86
|
+
static resolveAdditionalProperties() {}
|
|
87
|
+
static resolveSchema(schema) {
|
|
88
|
+
if (!isObj(schema)) return {};
|
|
89
|
+
if (hasOwnProperty(schema, 'allOf')) {
|
|
90
|
+
// schema = this.resolveAllOf(schema);
|
|
91
|
+
}
|
|
92
|
+
if (hasOwnProperty(schema, '$ref')) {
|
|
93
|
+
// schema = this.resolveRef(schema)
|
|
94
|
+
}
|
|
95
|
+
return schema;
|
|
96
|
+
}
|
|
97
|
+
// 获取Schema字段默认值
|
|
98
|
+
static getSchemaDefaultValue(_schema) {
|
|
99
|
+
const schema = isObj(_schema) ? _schema : {};
|
|
100
|
+
switch(this.getSchemaType(schema)){
|
|
101
|
+
case 'null':
|
|
102
|
+
return null;
|
|
103
|
+
case 'object':
|
|
104
|
+
return Object.keys(schema.properties || {}).reduce((pre, key)=>{
|
|
105
|
+
var _schema_properties;
|
|
106
|
+
const defaultValue = this.getSchemaDefaultValue((_schema_properties = schema.properties) === null || _schema_properties === void 0 ? void 0 : _schema_properties[key]);
|
|
107
|
+
pre[key] = defaultValue;
|
|
108
|
+
return pre;
|
|
109
|
+
}, {});
|
|
110
|
+
case 'array':
|
|
111
|
+
if (schema.default && Array.isArray(schema.default)) {
|
|
112
|
+
return schema.default;
|
|
113
|
+
}
|
|
114
|
+
return Array.isArray(schema.items) ? schema.items.map((item)=>this.getSchemaDefaultValue(item)) : [];
|
|
115
|
+
}
|
|
116
|
+
return schema.default !== undefined ? schema.default : initializationValue(schema.type || 'any');
|
|
117
|
+
}
|
|
118
|
+
static getSchemaType(schema) {
|
|
119
|
+
const { type } = schema;
|
|
120
|
+
if (!type && schema.const) {
|
|
121
|
+
return valueType(schema.const);
|
|
122
|
+
}
|
|
123
|
+
if (!type && schema.enum) {
|
|
124
|
+
return 'string';
|
|
125
|
+
}
|
|
126
|
+
if (!type && schema.items) {
|
|
127
|
+
return 'array';
|
|
128
|
+
}
|
|
129
|
+
return type;
|
|
130
|
+
}
|
|
131
|
+
static getUiProps(props, schema) {
|
|
132
|
+
if (!props || !props.startsWith('ui:')) {
|
|
133
|
+
console.warn('ui props must start with ui:');
|
|
134
|
+
}
|
|
135
|
+
return schema[props] || {};
|
|
136
|
+
}
|
|
137
|
+
static getSchemaField(schema) {
|
|
138
|
+
var _schema_oneOf, _schema_anyOf;
|
|
139
|
+
// 自定义Field组件
|
|
140
|
+
const field = schema['ui:field'];
|
|
141
|
+
if (field) {
|
|
142
|
+
return field;
|
|
143
|
+
}
|
|
144
|
+
// oneOf field
|
|
145
|
+
if ((_schema_oneOf = schema.oneOf) === null || _schema_oneOf === void 0 ? void 0 : _schema_oneOf.length) {
|
|
146
|
+
return {
|
|
147
|
+
name: registry.getField('composition'),
|
|
148
|
+
composition: 'oneOf'
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// anyOf field
|
|
152
|
+
if ((_schema_anyOf = schema.anyOf) === null || _schema_anyOf === void 0 ? void 0 : _schema_anyOf.length) {
|
|
153
|
+
return {
|
|
154
|
+
name: registry.getField('composition'),
|
|
155
|
+
composition: 'anyOf'
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// default field
|
|
159
|
+
const defaultField = registry.getField(this.getSchemaType(schema));
|
|
160
|
+
if (defaultField) {
|
|
161
|
+
return {
|
|
162
|
+
name: defaultField
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
name: null
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
static isMultiSelect(schema) {
|
|
170
|
+
if (!schema.uniqueItems || !schema.items) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
return Array.isArray(schema.items.enum);
|
|
174
|
+
}
|
|
175
|
+
static isTupleArray(schema) {
|
|
176
|
+
var _schema_items;
|
|
177
|
+
return Array.isArray(schema.items) && ((_schema_items = schema.items) === null || _schema_items === void 0 ? void 0 : _schema_items.length) > 0 && schema.items.every((item)=>isObj(item));
|
|
178
|
+
}
|
|
179
|
+
// 是否是自定义数组类型控件(如:label)
|
|
180
|
+
static isCustomArrayWidget(schema) {
|
|
181
|
+
var _schema_uicomponent;
|
|
182
|
+
const com = (_schema_uicomponent = schema['ui:component']) === null || _schema_uicomponent === void 0 ? void 0 : _schema_uicomponent.name;
|
|
183
|
+
return schema.type === 'array' && com;
|
|
184
|
+
}
|
|
185
|
+
static resolveFormComponent(com) {
|
|
186
|
+
if (typeof com === 'string' && com) {
|
|
187
|
+
return resolveComponent(com);
|
|
188
|
+
}
|
|
189
|
+
return com;
|
|
190
|
+
}
|
|
191
|
+
static getGroupWrap(schema) {
|
|
192
|
+
const _ref = schema['ui:group'] || {}, { name } = _ref, groupVnode = _object_without_properties(_ref, [
|
|
193
|
+
"name"
|
|
194
|
+
]);
|
|
195
|
+
return _object_spread_props(_object_spread({}, groupVnode), {
|
|
196
|
+
name: this.resolveFormComponent(registry.getComponent(name) || name || registry.getComponent('group'))
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
static getUiComponent(schema) {
|
|
200
|
+
const _ref = schema['ui:component'] || {}, { name } = _ref, vnodeData = _object_without_properties(_ref, [
|
|
201
|
+
"name"
|
|
202
|
+
]);
|
|
203
|
+
return _object_spread({
|
|
204
|
+
name: this.resolveFormComponent(registry.getComponent(name) || name)
|
|
205
|
+
}, vnodeData);
|
|
206
|
+
}
|
|
207
|
+
static getDefaultWidget(schema) {
|
|
208
|
+
var _schema_oneOf, _schema_oneOf1, _schema_anyOf, _schema_anyOf1;
|
|
209
|
+
const type = this.getSchemaType(schema);
|
|
210
|
+
// 默认转换策略
|
|
211
|
+
let defaultComponent = null;
|
|
212
|
+
if (type === 'string' && Array.isArray(schema.enum)) {
|
|
213
|
+
// string类型的枚举数据默认用select组件
|
|
214
|
+
defaultComponent = registry.getComponent('select');
|
|
215
|
+
}
|
|
216
|
+
if (type === 'array' && Schema.isMultiSelect(schema)) {
|
|
217
|
+
// array类型多选默认用checkbox组件
|
|
218
|
+
defaultComponent = registry.getComponent('checkbox');
|
|
219
|
+
}
|
|
220
|
+
if (typeof type !== 'object' && (((_schema_oneOf = schema.oneOf) === null || _schema_oneOf === void 0 ? void 0 : _schema_oneOf.length) && ((_schema_oneOf1 = schema.oneOf) === null || _schema_oneOf1 === void 0 ? void 0 : _schema_oneOf1.every((item)=>item.const)) || ((_schema_anyOf = schema.anyOf) === null || _schema_anyOf === void 0 ? void 0 : _schema_anyOf.length) && ((_schema_anyOf1 = schema.anyOf) === null || _schema_anyOf1 === void 0 ? void 0 : _schema_anyOf1.every((item)=>item.const)))) {
|
|
221
|
+
defaultComponent = registry.getComponent('select');
|
|
222
|
+
}
|
|
223
|
+
if (defaultComponent) {
|
|
224
|
+
return defaultComponent;
|
|
225
|
+
}
|
|
226
|
+
// 找不到对应组件就使用默认type对应的widget
|
|
227
|
+
const typeComponentMap = {
|
|
228
|
+
string: 'input',
|
|
229
|
+
number: 'input',
|
|
230
|
+
integer: 'input',
|
|
231
|
+
boolean: 'switcher',
|
|
232
|
+
null: ''
|
|
233
|
+
};
|
|
234
|
+
const name = typeComponentMap[type];
|
|
235
|
+
const defaultWidget = registry.getComponent(name) || registry.getBaseWidget(name);
|
|
236
|
+
if (defaultWidget) {
|
|
237
|
+
return defaultWidget;
|
|
238
|
+
}
|
|
239
|
+
console.warn(`未注册类型${type}对应的默认表单项`);
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
static isRequired(schema, name) {
|
|
243
|
+
return Array.isArray(schema.required) && schema.required.includes(name);
|
|
244
|
+
}
|
|
245
|
+
static getUiOptions(schema) {
|
|
246
|
+
const options = _object_spread({
|
|
247
|
+
showTitle: true,
|
|
248
|
+
label: schema.title,
|
|
249
|
+
desc: schema.description,
|
|
250
|
+
minLength: schema.minLength,
|
|
251
|
+
maxLength: schema.maxLength
|
|
252
|
+
}, schema['ui:props'] || {});
|
|
253
|
+
return _object_spread_props(_object_spread({}, options), {
|
|
254
|
+
description: options.desc || options.description,
|
|
255
|
+
// 0.1 兼容formItem设置 labelWidth 0 不生效问题
|
|
256
|
+
labelWidth: options.showTitle ? options.labelWidth : 0.1
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// 当前属性是否被依赖
|
|
260
|
+
static getDependencies(schema, name) {
|
|
261
|
+
return Object.entries(schema.dependencies || {}).find((data)=>Array.isArray(data[1]) && data[1].includes(name));
|
|
262
|
+
}
|
|
263
|
+
static resolveDefaultDatasource(schema) {
|
|
264
|
+
var _schema_uicomponent, _schema_items;
|
|
265
|
+
if ((_schema_uicomponent = schema['ui:component']) === null || _schema_uicomponent === void 0 ? void 0 : _schema_uicomponent.datasource) {
|
|
266
|
+
return schema['ui:component'].datasource;
|
|
267
|
+
}
|
|
268
|
+
if (Array.isArray(schema.enum)) {
|
|
269
|
+
const data = schema.enum;
|
|
270
|
+
return data.map((value)=>({
|
|
271
|
+
value,
|
|
272
|
+
label: value
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
if (Array.isArray((_schema_items = schema.items) === null || _schema_items === void 0 ? void 0 : _schema_items.enum)) {
|
|
276
|
+
const data = schema.items.enum || [];
|
|
277
|
+
return data.map((value)=>({
|
|
278
|
+
value,
|
|
279
|
+
label: value
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
if (typeof schema.type !== 'object' && schema.oneOf && schema.oneOf.every((item)=>item.const)) {
|
|
283
|
+
const data = schema.oneOf.map((item)=>({
|
|
284
|
+
value: item.const,
|
|
285
|
+
label: item.title || item.const
|
|
286
|
+
}));
|
|
287
|
+
return data;
|
|
288
|
+
}
|
|
289
|
+
if (typeof schema.type !== 'object' && schema.anyOf && schema.anyOf.every((item)=>item.const)) {
|
|
290
|
+
const data = schema.anyOf.map((item)=>({
|
|
291
|
+
value: item.const,
|
|
292
|
+
label: item.title || item.const
|
|
293
|
+
}));
|
|
294
|
+
return data;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
constructor(rootSchema){
|
|
298
|
+
Schema.rootSchema = rootSchema;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
_define_property(Schema, "rootSchema", void 0);
|
|
302
|
+
export { Schema as default };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _async_to_generator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
import { hasOwnProperty, initializationValue, isRegExp } from '../util/index.ts.js';
|
|
31
|
+
import { executeExpression, isExpression } from './expression.ts.js';
|
|
32
|
+
import Ajv from '../node_modules/ajv/dist/ajv.js';
|
|
33
|
+
import addFormats from '../node_modules/ajv-formats/dist/index.js';
|
|
34
|
+
import localizeZh from '../node_modules/ajv-i18n/localize/zh/index.js';
|
|
35
|
+
import widgetTree from './widgetTree.ts.js';
|
|
36
|
+
export const ajv = new Ajv({
|
|
37
|
+
strict: false
|
|
38
|
+
});
|
|
39
|
+
addFormats(ajv);
|
|
40
|
+
// 校验器 key
|
|
41
|
+
const VALIDATOR = 'validator';
|
|
42
|
+
const OWN_RULE_PROPERTY = 'ui:rules';
|
|
43
|
+
const DEBUG_PREFIX = '[bk-schema-form-validator]';
|
|
44
|
+
// 默认规则
|
|
45
|
+
export const DEFAULT_RULES = {
|
|
46
|
+
required: {
|
|
47
|
+
message: '必填项',
|
|
48
|
+
validator: '{{ $self.value !== \'\' }}'
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const throwErr = (err)=>{
|
|
52
|
+
throw new Error(`${DEBUG_PREFIX} ${err}`);
|
|
53
|
+
};
|
|
54
|
+
const globalRules = new Map();
|
|
55
|
+
/**
|
|
56
|
+
* 注册全局校验规则
|
|
57
|
+
* @param rules
|
|
58
|
+
*/ export const registryGlobalRules = (rules)=>{
|
|
59
|
+
try {
|
|
60
|
+
if (!rules) return;
|
|
61
|
+
if ((rules === null || rules === void 0 ? void 0 : rules.constructor) !== Object) throwErr('global rules must be an object');
|
|
62
|
+
Object.keys(rules).forEach((ruleName)=>{
|
|
63
|
+
const rule = rules[ruleName];
|
|
64
|
+
if (!hasOwnProperty(rule, VALIDATOR)) throwErr(`'${ruleName}' rule must have a validator property`);
|
|
65
|
+
const validator = rule[VALIDATOR];
|
|
66
|
+
if (!(isExpression(validator) || isRegExp(validator) || (validator === null || validator === void 0 ? void 0 : validator.constructor) === Function)) throwErr(`'${ruleName}' must be one of expression or regexp or function`);
|
|
67
|
+
globalRules.set(ruleName, rule);
|
|
68
|
+
});
|
|
69
|
+
} catch (error) {
|
|
70
|
+
throwErr(error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* 校验规则
|
|
75
|
+
*/ const validate = (rule, instance)=>{
|
|
76
|
+
let theRule = rule;
|
|
77
|
+
if (typeof rule === 'string') {
|
|
78
|
+
theRule = globalRules.get(rule);
|
|
79
|
+
if (!theRule) throwErr(`'${rule} is not a valid global rule, you can registry it to global rules node or use form item own custom rules`);
|
|
80
|
+
}
|
|
81
|
+
const { validator, message } = theRule;
|
|
82
|
+
let valid = true;
|
|
83
|
+
if (isExpression(validator)) {
|
|
84
|
+
valid = executeExpression(validator, instance);
|
|
85
|
+
} else if (typeof validator === 'function') {
|
|
86
|
+
valid = validator(instance);
|
|
87
|
+
// eslint-disable-next-line no-eval
|
|
88
|
+
} else if (eval(validator) instanceof RegExp) {
|
|
89
|
+
// eslint-disable-next-line no-eval
|
|
90
|
+
valid = new RegExp(eval(validator)).test(instance.value);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
valid,
|
|
94
|
+
message
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
// 设置表单项校验状态
|
|
98
|
+
export const setWidgetErrorTips = (path, isError, errorMsg)=>{
|
|
99
|
+
const formItem = widgetTree.widgetMap[path];
|
|
100
|
+
const { instance } = formItem;
|
|
101
|
+
if (formItem.type === 'node') {
|
|
102
|
+
instance === null || instance === void 0 ? void 0 : instance.setState('error', true);
|
|
103
|
+
instance === null || instance === void 0 ? void 0 : instance.setErrorTips(errorMsg);
|
|
104
|
+
}
|
|
105
|
+
// 如果实例属于某个组,则沿着 path 向上查找,如果找到组且组的 verifiable 设置为 true,则通过实例的校验状态改变组的校验状态
|
|
106
|
+
// submit 时校验,change 时去掉错误
|
|
107
|
+
const widgetPaths = path.split('.');
|
|
108
|
+
const fieldTitleBreadcrumb = [];
|
|
109
|
+
widgetPaths.forEach((thePath, thePathIndex)=>{
|
|
110
|
+
var _theWidget_instance;
|
|
111
|
+
const currentPaths = widgetPaths.slice(0, widgetPaths.length - thePathIndex);
|
|
112
|
+
const widgetPath = currentPaths.join('.');
|
|
113
|
+
const theWidget = widgetTree.widgetMap[widgetPath];
|
|
114
|
+
// 判断path是否是数组索引
|
|
115
|
+
const isIndex = theWidget.instance.schema.type === 'array' || !theWidget.instance.schema.title;
|
|
116
|
+
fieldTitleBreadcrumb.push(isIndex ? currentPaths.pop() : theWidget.instance.schema.title);
|
|
117
|
+
if (((_theWidget_instance = theWidget.instance) === null || _theWidget_instance === void 0 ? void 0 : _theWidget_instance.verifiable) && theWidget.type === 'group') {
|
|
118
|
+
if (isError) {
|
|
119
|
+
const groupErrors = `${fieldTitleBreadcrumb.reverse().join('-')}: ${errorMsg}`;
|
|
120
|
+
theWidget.instance.setGroupErrorTips(path, groupErrors);
|
|
121
|
+
} else {
|
|
122
|
+
theWidget.instance.removeGroupErrorTips(path);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* 校验单个表单项
|
|
129
|
+
* @param path 字段路径
|
|
130
|
+
*/ export const validateFormItem = (path)=>{
|
|
131
|
+
var _instance_schema;
|
|
132
|
+
const formItem = widgetTree.widgetMap[path];
|
|
133
|
+
if (!formItem) return {
|
|
134
|
+
result: true,
|
|
135
|
+
message: '',
|
|
136
|
+
path: ''
|
|
137
|
+
};
|
|
138
|
+
const { instance } = formItem;
|
|
139
|
+
// 清空上一次状态
|
|
140
|
+
if (formItem.type === 'group') {
|
|
141
|
+
instance === null || instance === void 0 ? void 0 : instance.clearGroupErrorTips();
|
|
142
|
+
} else {
|
|
143
|
+
instance === null || instance === void 0 ? void 0 : instance.setState('error', false);
|
|
144
|
+
instance === null || instance === void 0 ? void 0 : instance.setErrorTips('');
|
|
145
|
+
// 清空所在组错误信息
|
|
146
|
+
const widgetPaths = path.split('.');
|
|
147
|
+
widgetPaths.forEach((thePath, thePathIndex)=>{
|
|
148
|
+
var _theWidget_instance;
|
|
149
|
+
const currentPaths = widgetPaths.slice(0, widgetPaths.length - thePathIndex).join('.');
|
|
150
|
+
const theWidget = widgetTree.widgetMap[currentPaths];
|
|
151
|
+
if (((_theWidget_instance = theWidget.instance) === null || _theWidget_instance === void 0 ? void 0 : _theWidget_instance.verifiable) && theWidget.type === 'group') {
|
|
152
|
+
theWidget.instance.removeGroupErrorTips(path);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// json schema 规则校验
|
|
157
|
+
const ownSchema = instance.schema;
|
|
158
|
+
const schemaValidate = ajv.compile(ownSchema);
|
|
159
|
+
const value = instance.value === undefined ? initializationValue(ownSchema.type, {
|
|
160
|
+
integer: 0,
|
|
161
|
+
number: 0
|
|
162
|
+
}) : instance.value;
|
|
163
|
+
const schemaValid = schemaValidate(value);
|
|
164
|
+
if (!schemaValid) {
|
|
165
|
+
localizeZh(schemaValidate.errors);
|
|
166
|
+
const errorsText = ajv.errorsText(schemaValidate.errors, {
|
|
167
|
+
separator: '\n'
|
|
168
|
+
});
|
|
169
|
+
setWidgetErrorTips(path, true, errorsText);
|
|
170
|
+
return {
|
|
171
|
+
result: false,
|
|
172
|
+
message: errorsText,
|
|
173
|
+
path
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
// 自定义规则校验
|
|
177
|
+
const customRules = ((_instance_schema = instance.schema) === null || _instance_schema === void 0 ? void 0 : _instance_schema[OWN_RULE_PROPERTY]) || [];
|
|
178
|
+
let isError = false;
|
|
179
|
+
let errorMsg = '';
|
|
180
|
+
for (const rule of customRules){
|
|
181
|
+
const result = validate(rule, instance);
|
|
182
|
+
if (!result.valid) {
|
|
183
|
+
isError = true;
|
|
184
|
+
errorMsg = result.message;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (isError) {
|
|
189
|
+
setWidgetErrorTips(path, true, errorMsg);
|
|
190
|
+
return {
|
|
191
|
+
result: false,
|
|
192
|
+
message: errorMsg,
|
|
193
|
+
path
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
result: true,
|
|
198
|
+
message: errorMsg,
|
|
199
|
+
path
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* 校验整个表单
|
|
204
|
+
*/ export const validateForm = ()=>{
|
|
205
|
+
let isValid = true;
|
|
206
|
+
Object.keys(widgetTree.widgetMap).forEach((path)=>{
|
|
207
|
+
var _validateFormItem;
|
|
208
|
+
if (!((_validateFormItem = validateFormItem(path)) === null || _validateFormItem === void 0 ? void 0 : _validateFormItem.result)) isValid = false;
|
|
209
|
+
});
|
|
210
|
+
return isValid;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* 校验表单(抛出具体错误信息)
|
|
214
|
+
*/ export const validateFormWithResult = ()=>new Promise((resolve, reject)=>{
|
|
215
|
+
const result = Object.keys(widgetTree.widgetMap).filter((path)=>{
|
|
216
|
+
var _widgetTree_widgetMap_path;
|
|
217
|
+
return ((_widgetTree_widgetMap_path = widgetTree.widgetMap[path]) === null || _widgetTree_widgetMap_path === void 0 ? void 0 : _widgetTree_widgetMap_path.type) === 'node';
|
|
218
|
+
}).reduce((pre, path)=>{
|
|
219
|
+
const validateResult = validateFormItem(path);
|
|
220
|
+
if (!(validateResult === null || validateResult === void 0 ? void 0 : validateResult.result)) {
|
|
221
|
+
pre.push(validateResult);
|
|
222
|
+
}
|
|
223
|
+
return pre;
|
|
224
|
+
}, []);
|
|
225
|
+
if (result.length) {
|
|
226
|
+
reject(result);
|
|
227
|
+
} else {
|
|
228
|
+
resolve([]);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
// 校验schema是否正常
|
|
232
|
+
export const validateSchema = function() {
|
|
233
|
+
var _ref = _async_to_generator(function*(schema) {
|
|
234
|
+
try {
|
|
235
|
+
return yield ajv.validateSchema(schema, true);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
console.error('Schema不符合规范, 请检查', err);
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return function validateSchema1(schema) {
|
|
242
|
+
return _ref.apply(this, arguments);
|
|
243
|
+
};
|
|
244
|
+
}();
|
|
245
|
+
/**
|
|
246
|
+
* 触发校验
|
|
247
|
+
* @param path 字段路径
|
|
248
|
+
*/ export const dispatchValidate = (path)=>validateFormItem(path);
|