@xiaohaih/condition-core
Advanced tools
Comparing version 0.3.2 to 0.4.0
# @xiaohaih/condition-core | ||
## 0.4.0 | ||
### Minor Changes | ||
- core 支持所有数据类型, 修复重置报错, 去除 cascader 内置的 emitPath, 修复 ts 声明报层级过深 | ||
## 0.3.2 | ||
@@ -4,0 +10,0 @@ |
@@ -153,9 +153,45 @@ 'use strict'; | ||
} | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
function shallowDeep(val) { | ||
return Array.isArray(val) ? [...val] : val; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
function isEqualExcludeEmptyValue(x, y) { | ||
return isEmptyValue(x) && isEmptyValue(y) || isEqual(x, y); | ||
} | ||
/** 判断是否是空值(null, undefined, '') */ | ||
function isEmptyValue(val) { | ||
return val === undefined || val === '' || val === null; | ||
} | ||
/** 判断两个值是否一致 */ | ||
function isEqual(x, y) { | ||
if (Object.is(x, y)) return true; | ||
if (x instanceof Date && y instanceof Date) { | ||
return x.getTime() === y.getTime(); | ||
} | ||
if (x instanceof RegExp && y instanceof RegExp) { | ||
return x.toString() === y.toString(); | ||
} | ||
if (typeof x !== 'object' || x === null || typeof y !== 'object' || y === null) { | ||
return false; | ||
} | ||
const keysX = Reflect.ownKeys(x); | ||
const keysY = Reflect.ownKeys(y); | ||
if (keysX.length !== keysY.length) return false; | ||
for (let i = 0; i < keysX.length; i++) { | ||
if (!Reflect.has(y, keysX[i])) return false; | ||
if (!isEqual(x[keysX[i]], y[keysX[i]])) return false; | ||
} | ||
return true; | ||
} | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
function isPrimitive(value) { | ||
return value === undefined || value === null || typeof value !== 'object' && typeof value !== 'function'; | ||
} | ||
/** 拷贝 */ | ||
function clone(obj, deep) { | ||
if (isPrimitive(obj)) return obj; | ||
if (typeof obj === 'function') return obj.bind({}); | ||
const newObj = new obj.constructor(); | ||
Object.getOwnPropertyNames(obj).forEach(prop => { | ||
newObj[prop] = deep ? clone(obj[prop]) : obj[prop]; | ||
}); | ||
return newObj; | ||
} | ||
/** | ||
@@ -185,3 +221,4 @@ * 获取指定层级的父级(包括自身) | ||
function getNode(node, ...args) { | ||
if (!node) return; | ||
// 直接抛出 null, template 中会报错 | ||
if (!node) return null; | ||
return typeof node === 'function' ? node(...args) : typeof node === 'string' ? node : vueDemi.markRaw(node); | ||
@@ -269,6 +306,2 @@ } | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str$1(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** 封装扁平组件必备的信息 */ | ||
@@ -285,5 +318,3 @@ function usePlain(props) { | ||
/** 当前选中值 */ | ||
const checked = vueDemi.ref(initialBackfillValue || | ||
// 防止数组引用导致默认值发生改变 | ||
shallowDeep(props.defaultValue !== undefined ? initialValue.value : props.multiple ? [] : '')); | ||
const checked = vueDemi.shallowRef(initialBackfillValue || props.defaultValue !== undefined ? clone(initialValue.value) : undefined); | ||
/** 远程获取的数据源 */ | ||
@@ -295,4 +326,5 @@ const remoteOption = vueDemi.ref([]); | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(checked.value?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(shallowDeep(vueDemi.toRaw(checked.value)), props.emptyValue) | ||
const _checked = clone(checked.value); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(_checked?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(_checked, props.emptyValue) | ||
}; | ||
@@ -348,6 +380,6 @@ }; | ||
([_field, val], [__field]) => { | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
// 仅在值发生变化时同步 忽视空值不一致的问题 | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (!realtimeFlag.value) return; | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateCheckedValue(_val); | ||
@@ -359,3 +391,3 @@ })); | ||
const _val = props.backfillToValue(val, _field, props.backfill); | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateBackfillFlag(); | ||
@@ -365,3 +397,3 @@ updateCheckedValue(_val); | ||
// 存在依赖项 | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
@@ -371,4 +403,4 @@ if (val === oldVal) return; | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (checked.value === undefined || checked.value.toString() === '') return; | ||
if (_depend !== __depend || !isEqualExcludeEmptyValue(_dependFields, __dependFields)) return; | ||
if (isEmptyValue(checked.value)) return; | ||
updateCheckedValue(props.multiple ? [] : ''); | ||
@@ -402,3 +434,3 @@ })); | ||
* 更新选中值(父级也同步更改) | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -412,3 +444,3 @@ function updateCheckedValue(value) { | ||
* 更新选中值并触发内部搜索事件 | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -440,2 +472,7 @@ function change(value) { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -501,7 +538,2 @@ query: { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 数据源 */ | ||
@@ -518,6 +550,5 @@ options: { | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
/** 封装 tree 组件必备的信息 */ | ||
@@ -530,3 +561,3 @@ function useTree(props) { | ||
/** 当前选中值 */ | ||
const checked = vueDemi.ref([]); | ||
const checked = vueDemi.ref(); | ||
/** 远程获取的数据源 */ | ||
@@ -536,2 +567,6 @@ const remoteOption = vueDemi.ref([]); | ||
const finalOption = vueDemi.computed(() => remoteOption.value.length ? remoteOption.value : props.options); | ||
/** 字段集合 */ | ||
const fieldObj = vueDemi.computed(() => props.fields ? props.fields.reduce((p, v) => (p[v] = props.emptyValue, p), {}) : { | ||
[props.field]: props.emptyValue | ||
}); | ||
/** 获取当前条件所拥有的值 */ | ||
@@ -541,8 +576,19 @@ const getQuery = () => { | ||
if (!sourceIsInit.value && !initialValue.value) return {}; | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.fields?.length ? props.fields.reduce((p, v, i) => Object.assign(p, { | ||
[v]: emptyToValue(checked.value[i], props.emptyValue) | ||
}), {}) : { | ||
[props.field]: emptyToValue(props.emitPath ? [...checked.value] : checked.value.slice(-1)[0], props.emptyValue) | ||
}; | ||
let _checked = clone(checked.value); | ||
const { | ||
...result | ||
} = fieldObj.value; | ||
if (props.customGetQuery) return props.customGetQuery(_checked, emptyToValue, props); | ||
if (!_checked) return result; | ||
if (!props.emitPath) { | ||
_checked = props.multiple ? _checked.map(v => v?.slice(-1)[0]) : _checked.slice(-1)[0]; | ||
} | ||
if (Array.isArray(_checked)) { | ||
props.fields ? _checked.forEach((o, i) => { | ||
result[props.fields[i]] = emptyToValue(o, props.emptyValue); | ||
}) : result[props.field] = _checked; | ||
} else { | ||
result[props.fields?.[0] || props.field] = emptyToValue(_checked, props.emptyValue); | ||
} | ||
return result; | ||
}; | ||
@@ -616,4 +662,6 @@ // 防止触发搜索时, query 产生变化内部重复赋值 | ||
if (initialValue.value?.length) { | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// 此种情况比较多 可封装成一个函数处理 | ||
checked.value = typeof initialValue.value === 'string' ? insideGetChained(initialValue.value) : initialValue.value.slice(); | ||
typeof initialValue.value === 'string' && (initialValue.value = checked.value.slice()); | ||
typeof initialValue.value === 'string' && (initialValue.value = clone(checked.value)); | ||
option.updateWrapperQuery(); | ||
@@ -630,5 +678,6 @@ } | ||
// 仅在值发生变化时同步 | ||
if (_field !== __field || emptyValue2Str(val) === emptyValue2Str(oldVal)) return; | ||
if (!realtimeFlag.value) return; | ||
checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
if (_field !== __field || isEqualExcludeEmptyValue(val, oldVal)) return; | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
})); | ||
@@ -641,23 +690,16 @@ // 回填值发生变化时触发更新 | ||
if (!sourceIsInit.value) return; | ||
if (emptyValue2Str(value) === emptyValue2Str(oldVal)) return; | ||
if (isEqualExcludeEmptyValue(value, oldVal)) return; | ||
updateBackfillFlag(); | ||
if (Array.isArray(value)) { | ||
updateCheckedValue(value); | ||
} else { | ||
if (!value && value !== 0) { | ||
checked.value.length && (checked.value = []); | ||
return; | ||
} | ||
updateCheckedValue(insideGetChained(value)); | ||
} | ||
// TODO 判断最后一个值是否与选中项最后一个一致(多选需遍历) | ||
// 如果一致则不更新, 不一致, 则通过 insideGetChained 获取到树形再赋值 | ||
// updateCheckedValue(value); | ||
})); | ||
// 存在依赖项 | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
if (val === oldVal) return; | ||
if (isEqualExcludeEmptyValue(val, oldVal)) return; | ||
getOption('depend'); | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (!checked.value.length) return; | ||
updateCheckedValue(typeof checked.value === 'string' ? '' : []); | ||
updateCheckedValue(undefined); | ||
})); | ||
@@ -690,5 +732,4 @@ unwatchs.push(vueDemi.watch(() => props.getOptions, getOption.bind(null, 'initial'), { | ||
function updateCheckedValue(values) { | ||
const _checked = Array.isArray(values) ? values : insideGetChained(values); | ||
if (_checked.join('') === checked.value.join('')) return; | ||
checked.value = _checked; | ||
if (isEqualExcludeEmptyValue(checked.value, values)) return; | ||
checked.value = values; | ||
option.updateWrapperQuery(); | ||
@@ -701,3 +742,3 @@ } | ||
function change(values) { | ||
updateCheckedValue(values || []); | ||
updateCheckedValue(values); | ||
wrapper?.insetSearch(); | ||
@@ -709,3 +750,3 @@ } | ||
function insideGetChained(val) { | ||
if (!val && val !== 0) return []; | ||
if (isEmptyValue(val)) return []; | ||
const { | ||
@@ -746,3 +787,3 @@ valueKey, | ||
}, | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
emitPath: { | ||
@@ -764,2 +805,3 @@ type: [Boolean], | ||
exports.IS_COMPOSITION_VERSION = IS_COMPOSITION_VERSION; | ||
exports.clone = clone; | ||
exports.commonProps = commonProps; | ||
@@ -771,5 +813,8 @@ exports.defineCommonMethod = defineCommonMethod; | ||
exports.getNode = getNode; | ||
exports.isEmptyValue = isEmptyValue; | ||
exports.isEqual = isEqual; | ||
exports.isEqualExcludeEmptyValue = isEqualExcludeEmptyValue; | ||
exports.isPrimitive = isPrimitive; | ||
exports.plainProps = plainProps; | ||
exports.provideKey = provideKey; | ||
exports.shallowDeep = shallowDeep; | ||
exports.treeProps = treeProps; | ||
@@ -776,0 +821,0 @@ exports.usePlain = usePlain; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("vue-demi");const t="2.7"===e.version.slice(0,3),a="condition-wrapper";function l(e){return e}function r(e){return e}const i={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function n(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function u(e){return Array.isArray(e)?[...e]:e}function o(e,t,a="children"){for(const l of e){if(t(l))return[l];if(l[a]?.length){const e=o(l[a],t);if(e.length)return e.unshift(l),e}}return[]}function d(t){const a=e.ref();return e.computed({set(e){a.value=e},get:()=>void 0===a.value?void 0!==t.defaultValue?"function"==typeof t.defaultValue?t.defaultValue(t.query,t.backfill):t.defaultValue:void 0:a.value})}function s(t,a){const l=e.ref("boolean"==typeof t.disabled&&t.disabled),r=e.ref("boolean"==typeof t.hide&&t.hide),i=()=>({query:t.query,backfill:t.backfill,option:a}),n=()=>{if("function"==typeof t.hide){r.value!==t.hide(i())&&(r.value=t.hide(i()))}else if("function"==typeof t.disabled){l.value!==t.disabled(i())&&(l.value=t.disabled(i()))}};let u=[e.watch((()=>t.query),n,{immediate:!0,deep:!0}),e.watch((()=>[t.disabled,t.hide]),((e,t)=>{e[0]!==t[0]&&(l.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(r.value="boolean"==typeof e[1]&&e[1],e[1]),n()}))];return e.onBeforeUnmount((()=>(u.forEach((e=>e())),u=[]))),{insetDisabled:l,insetHide:r}}function p(t=!0){const a=e.ref(t);return{flag:a,updateFlag:()=>{a.value=!t,e.nextTick((()=>{a.value=t}))}}}function c(e){return e?.toString()??""}const f={field:{type:String,required:!0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},y={...f,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},multiple:{type:Boolean,default:void 0},options:{type:Array,default:()=>[]},getOptions:{type:Function}};function v(e){return e?.toString()??""}const h={...f,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};exports.IS_COMPOSITION_VERSION=t,exports.commonProps=f,exports.defineCommonMethod=r,exports.defineProvideValue=l,exports.emptyToValue=n,exports.getChained=o,exports.getNode=function(t,...a){if(t)return"function"==typeof t?t(...a):"string"==typeof t?t:e.markRaw(t)},exports.plainProps=y,exports.provideKey=a,exports.shallowDeep=u,exports.treeProps=h,exports.usePlain=function(t){const l=e.inject(a),r=d(t),i=t.backfill&&(t.fields?.length?t.fields.map((e=>t.backfill[e])).filter(Boolean):t.backfill[t.field]),o=e.ref(i||u(void 0!==t.defaultValue?r.value:t.multiple?[]:"")),f=e.ref([]),y=e.computed((()=>f.value.length?f.value:t.options)),v=()=>t.customGetQuery?t.customGetQuery(o.value,n,t):t.multiple&&t.fields?t.fields.reduce(((e,a,l)=>(e[a]=n(o.value?.[l],t.emptyValue),e)),{}):{[t.field]:n(u(e.toRaw(o.value)),t.emptyValue)},{flag:h,updateFlag:g}=p(),{flag:m,updateFlag:b}=p(),Q={reset(){const{multiple:e}=t;g(),b(),o.value=t.resetToInitialValue&&r.value?.slice()||(e?[]:"")},updateWrapperQuery(){g(),l&&Object.entries(v()).forEach((([e,t])=>l.updateQueryValue(e,t)))},get validator(){return t.validator},getQuery:v};l?.register(Q);const{insetDisabled:S,insetHide:k}=s(t,Q);!i&&t.defaultValue&&Q.updateWrapperQuery();const V=[];function w(e){t.getOptions?.((e=>{const t=o.value;o.value=void 0,f.value=e||[],o.value=t}),t.query||{},{trigger:e,change:(e,t)=>{t&&(r.value=e),O(e)},search:(e,t)=>{t&&(r.value=e),F(e),l?.search()}})}function F(e){e!==o.value&&(o.value=e,Q.updateWrapperQuery())}function O(e){F(e),l?.insetSearch()}return e.onBeforeUnmount((()=>V.forEach((e=>e())))),V.push(e.watch((()=>t.field),((e,t)=>{e!==t&&l?.removeUnreferencedField(t),Q.updateWrapperQuery()}))),V.push(e.watch((()=>[t.fields||t.field,t.fields?t.fields.map((e=>t.query[e])).filter(Boolean):t.query[t.field]]),(([e,a],[l])=>{const r=t.backfillToValue(a,e,t.query);e.toString()===l.toString()&&c(r)!==c(o.value)&&h.value&&F(r)}))),V.push(e.watch((()=>[t.fields||t.field,t.fields?t.fields.map((e=>t.backfill?.[e])).filter(Boolean):t.backfill?.[t.field]]),(([e,a],[l])=>{const r=t.backfillToValue(a,e,t.backfill);e.toString()===l.toString()&&c(r)!==c(o.value)&&(b(),F(r))}))),V.push(e.watch((()=>[t.depend,t.dependFields,t.dependFields&&[].concat(t.dependFields).map((e=>t.query?.[e])).join(",")||""]),(([e,a,l],[r,i,n])=>{m.value&&l!==n&&(w("depend"),e===r&&a?.toString()===i?.toString()&&void 0!==o.value&&""!==o.value.toString()&&F(t.multiple?[]:""))}))),V.push(e.watch((()=>t.getOptions),w.bind(null,"initial"),{immediate:!0})),{wrapper:l,option:Q,checked:o,getQuery:v,insetDisabled:S,insetHide:k,finalOption:y,updateCheckedValue:F,change:O,reset:Q.reset}},exports.useTree=function(t){const l=e.inject(a),r=d(t),i=e.ref([]),u=e.ref([]),c=e.computed((()=>u.value.length?u.value:t.options)),f=()=>V.value||r.value?t.customGetQuery?t.customGetQuery(i.value,n,t):t.fields?.length?t.fields.reduce(((e,a,l)=>Object.assign(e,{[a]:n(i.value[l],t.emptyValue)})),{}):{[t.field]:n(t.emitPath?[...i.value]:i.value.slice(-1)[0],t.emptyValue)}:{},{flag:y,updateFlag:h}=p(),{flag:g,updateFlag:m}=p(),b={reset(){return h(),m(),i.value=t.resetToInitialValue&&r.value?.slice()||[],this},get validator(){return t.validator},updateWrapperQuery(){h(),l&&Object.entries(f()).forEach((([e,t])=>l.updateQueryValue(e,t)))},getQuery:f};l?.register(b);const{insetDisabled:Q,insetHide:S}=s(t,b),k=[];e.onBeforeUnmount((()=>k.forEach((e=>e()))));const V=e.ref("function"!=typeof t.getOptions||!!t.fields?.length);function w(e){t.getOptions?.((e=>{u.value=e||[],V.value=!0}),t.query||{},{trigger:e,change:(e,t)=>{t&&(r.value=e),O(e)},search:(e,t)=>{t&&(r.value=e),F(e),l?.search()}})}function F(e){const t=Array.isArray(e)?e:B(e);t.join("")!==i.value.join("")&&(i.value=t,b.updateWrapperQuery())}function O(e){F(e||[]),l?.insetSearch()}function B(e){if(!e&&0!==e)return[];const{valueKey:a,childrenKey:l}=t;return o(c.value,(t=>t[a]===e)).map((e=>e[a]),l).filter(Boolean)}return e.watch(V,(e=>e&&function(){const{backfill:e,field:a,fields:l}=t;if(e)if(l){const t=l.reduce(((t,a)=>(e[a]&&t.push(e[a]),t)),[]);if(t.length)return i.value=t,void b.updateWrapperQuery()}else if(e[a])return i.value=B(e[a]),void b.updateWrapperQuery();r.value?.length&&(i.value="string"==typeof r.value?B(r.value):r.value.slice(),"string"==typeof r.value&&(r.value=i.value.slice()),b.updateWrapperQuery())}()),{immediate:!0}),k.push(e.watch((()=>t.fields||[t.field]),((e,t)=>{e.toString()!==t.toString()&&l&&t.forEach((t=>e.includes(t)||l.removeUnreferencedField(t))),b.updateWrapperQuery()}))),k.push(e.watch((()=>[t.fields?.toString()||t.field,t.fields?.map((e=>t.query[e])).filter(Boolean)||t.query[t.field]]),(([e,t],[a,l])=>{e===a&&v(t)!==v(l)&&y.value&&(i.value="string"==typeof t?B(t):t)}))),k.push(e.watch((()=>t.fields?.length?t.fields.reduce(((e,a)=>(t.backfill?.[a]&&e.push(t.backfill[a]),e)),[]):t.backfill?.[t.field]),((e,t)=>{if(V.value&&v(e)!==v(t))if(m(),Array.isArray(e))F(e);else{if(!e&&0!==e)return void(i.value.length&&(i.value=[]));F(B(e))}}))),k.push(e.watch((()=>[t.depend,t.dependFields,t.dependFields&&[].concat(t.dependFields).map((e=>t.query?.[e])).join(",")||""]),(([e,t,a],[l,r,n])=>{g.value&&a!==n&&(w("depend"),e===l&&t?.toString()===r?.toString()&&i.value.length&&F("string"==typeof i.value?"":[]))}))),k.push(e.watch((()=>t.getOptions),w.bind(null,"initial"),{immediate:!0})),{wrapper:l,option:b,checked:i,getQuery:f,finalOption:c,insetDisabled:Q,insetHide:S,change:O,reset:b.reset}},exports.useWrapper=function(l,r){const i=[];e.onBeforeUnmount((()=>i.splice(0)));const n={};let u=!1,o=[];const d={realtime:e.ref(l.realtime),register(a){i.push(a);const r=()=>{u=!0,a.reset(),a.updateWrapperQuery();const t=i.indexOf(a);-1!==t&&i.splice(t,1),l.searchAtDatumChanged&&c(),u=!1,o.forEach((t=>{e.del(s.value,t),delete n[t]})),o=[]},d=e.getCurrentInstance();return d&&e.onBeforeUnmount(r,t?d.proxy:d),r},updateQueryValue(t,a){u&&o.push(t),e.set(s.value,t,a),n[t]=a},insetSearch(){l.realtime&&c()},search:c,removeUnreferencedField(t){let a=0;i.some((e=>(e.getQuery().hasOwnProperty(t)&&(a+=1),a))),a||(e.del(s.value,t),delete n[t])}};e.provide(a,d);const s=e.ref({...l.backfill}),p=()=>({...s.value,...l.backfill,...n});async function c(){const e=await f();e?l.toast?.(e):r?.search?.(p())}async function f(){return(await Promise.all(i.map((e=>e.validator?.(s.value))))).find((e=>e&&"string"==typeof e))}return{child:i,wrapperInstance:d,query:s,getQuery:p,search:c,reset:function(){i.forEach((e=>{e.reset(),e.updateWrapperQuery()})),r?.reset?.(p())},validate:f}},exports.wrapperProps=i; | ||
"use strict";var e=require("vue-demi");const t="2.7"===e.version.slice(0,3),r="condition-wrapper";function n(e){return e}function l(e){return e}const i={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function a(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function u(e,t){return o(e)&&o(t)||s(e,t)}function o(e){return void 0===e||""===e||null===e}function s(e,t){if(Object.is(e,t))return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.toString()===t.toString();if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const r=Reflect.ownKeys(e),n=Reflect.ownKeys(t);if(r.length!==n.length)return!1;for(let n=0;n<r.length;n++){if(!Reflect.has(t,r[n]))return!1;if(!s(e[r[n]],t[r[n]]))return!1}return!0}function d(e){return null==e||"object"!=typeof e&&"function"!=typeof e}function f(e,t){if(d(e))return e;if("function"==typeof e)return e.bind({});const r=new e.constructor;return Object.getOwnPropertyNames(e).forEach((n=>{r[n]=t?f(e[n]):e[n]})),r}function p(e,t,r="children"){for(const n of e){if(t(n))return[n];if(n[r]?.length){const e=p(n[r],t);if(e.length)return e.unshift(n),e}}return[]}function c(t){const r=e.ref();return e.computed({set(e){r.value=e},get:()=>void 0===r.value?void 0!==t.defaultValue?"function"==typeof t.defaultValue?t.defaultValue(t.query,t.backfill):t.defaultValue:void 0:r.value})}function y(t,r){const n=e.ref("boolean"==typeof t.disabled&&t.disabled),l=e.ref("boolean"==typeof t.hide&&t.hide),i=()=>({query:t.query,backfill:t.backfill,option:r}),a=()=>{if("function"==typeof t.hide){l.value!==t.hide(i())&&(l.value=t.hide(i()))}else if("function"==typeof t.disabled){n.value!==t.disabled(i())&&(n.value=t.disabled(i()))}};let u=[e.watch((()=>t.query),a,{immediate:!0,deep:!0}),e.watch((()=>[t.disabled,t.hide]),((e,t)=>{e[0]!==t[0]&&(n.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(l.value="boolean"==typeof e[1]&&e[1],e[1]),a()}))];return e.onBeforeUnmount((()=>(u.forEach((e=>e())),u=[]))),{insetDisabled:n,insetHide:l}}function v(t=!0){const r=e.ref(t);return{flag:r,updateFlag:()=>{r.value=!t,e.nextTick((()=>{r.value=t}))}}}const h={field:{type:String,required:!0},multiple:{type:Boolean,default:void 0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},g={...h,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},options:{type:Array,default:()=>[]},getOptions:{type:Function}};const m={...h,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};exports.IS_COMPOSITION_VERSION=t,exports.clone=f,exports.commonProps=h,exports.defineCommonMethod=l,exports.defineProvideValue=n,exports.emptyToValue=a,exports.getChained=p,exports.getNode=function(t,...r){return t?"function"==typeof t?t(...r):"string"==typeof t?t:e.markRaw(t):null},exports.isEmptyValue=o,exports.isEqual=s,exports.isEqualExcludeEmptyValue=u,exports.isPrimitive=d,exports.plainProps=g,exports.provideKey=r,exports.treeProps=m,exports.usePlain=function(t){const n=e.inject(r),l=c(t),i=t.backfill&&(t.fields?.length?t.fields.map((e=>t.backfill[e])).filter(Boolean):t.backfill[t.field]),s=e.shallowRef(i||void 0!==t.defaultValue?f(l.value):void 0),d=e.ref([]),p=e.computed((()=>d.value.length?d.value:t.options)),h=()=>{if(t.customGetQuery)return t.customGetQuery(s.value,a,t);const e=f(s.value);return t.multiple&&t.fields?t.fields.reduce(((r,n,l)=>(r[n]=a(e?.[l],t.emptyValue),r)),{}):{[t.field]:a(e,t.emptyValue)}},{flag:g,updateFlag:m}=v(),{flag:b,updateFlag:V}=v(),w={reset(){const{multiple:e}=t;m(),V(),s.value=t.resetToInitialValue&&l.value?.slice()||(e?[]:"")},updateWrapperQuery(){m(),n&&Object.entries(h()).forEach((([e,t])=>n.updateQueryValue(e,t)))},get validator(){return t.validator},getQuery:h};n?.register(w);const{insetDisabled:Q,insetHide:k}=y(t,w);!i&&t.defaultValue&&w.updateWrapperQuery();const x=[];function F(e){t.getOptions?.((e=>{const t=s.value;s.value=void 0,d.value=e||[],s.value=t}),t.query||{},{trigger:e,change:(e,t)=>{t&&(l.value=e),S(e)},search:(e,t)=>{t&&(l.value=e),O(e),n?.search()}})}function O(e){e!==s.value&&(s.value=e,w.updateWrapperQuery())}function S(e){O(e),n?.insetSearch()}return e.onBeforeUnmount((()=>x.forEach((e=>e())))),x.push(e.watch((()=>t.field),((e,t)=>{e!==t&&n?.removeUnreferencedField(t),w.updateWrapperQuery()}))),x.push(e.watch((()=>[t.fields||t.field,t.fields?t.fields.map((e=>t.query[e])).filter(Boolean):t.query[t.field]]),(([e,r],[n])=>{if(!g.value)return;const l=t.backfillToValue(r,e,t.query);e.toString()!==n.toString()||u(l,s.value)||O(l)}))),x.push(e.watch((()=>[t.fields||t.field,t.fields?t.fields.map((e=>t.backfill?.[e])).filter(Boolean):t.backfill?.[t.field]]),(([e,r],[n])=>{const l=t.backfillToValue(r,e,t.backfill);e.toString()!==n.toString()||u(l,s.value)||(V(),O(l))}))),x.push(e.watch((()=>[t.depend,t.dependFields,t.dependFields&&[].concat(t.dependFields).map((e=>t.query?.[e]))]),(([e,r,n],[l,i,a])=>{b.value&&n!==a&&(F("depend"),e===l&&u(r,i)&&(o(s.value)||O(t.multiple?[]:"")))}))),x.push(e.watch((()=>t.getOptions),F.bind(null,"initial"),{immediate:!0})),{wrapper:n,option:w,checked:s,getQuery:h,insetDisabled:Q,insetHide:k,finalOption:p,updateCheckedValue:O,change:S,reset:w.reset}},exports.useTree=function(t){const n=e.inject(r),l=c(t),i=e.ref(),s=e.ref([]),d=e.computed((()=>s.value.length?s.value:t.options)),h=e.computed((()=>t.fields?t.fields.reduce(((e,r)=>(e[r]=t.emptyValue,e)),{}):{[t.field]:t.emptyValue})),g=()=>{if(!O.value&&!l.value)return{};let e=f(i.value);const{...r}=h.value;return t.customGetQuery?t.customGetQuery(e,a,t):e?(t.emitPath||(e=t.multiple?e.map((e=>e?.slice(-1)[0])):e.slice(-1)[0]),Array.isArray(e)?t.fields?e.forEach(((e,n)=>{r[t.fields[n]]=a(e,t.emptyValue)})):r[t.field]=e:r[t.fields?.[0]||t.field]=a(e,t.emptyValue),r):r},{flag:m,updateFlag:b}=v(),{flag:V,updateFlag:w}=v(),Q={reset(){return b(),w(),i.value=t.resetToInitialValue&&l.value?.slice()||[],this},get validator(){return t.validator},updateWrapperQuery(){b(),n&&Object.entries(g()).forEach((([e,t])=>n.updateQueryValue(e,t)))},getQuery:g};n?.register(Q);const{insetDisabled:k,insetHide:x}=y(t,Q),F=[];e.onBeforeUnmount((()=>F.forEach((e=>e()))));const O=e.ref("function"!=typeof t.getOptions||!!t.fields?.length);function S(e){t.getOptions?.((e=>{s.value=e||[],O.value=!0}),t.query||{},{trigger:e,change:(e,t)=>{t&&(l.value=e),B(e)},search:(e,t)=>{t&&(l.value=e),q(e),n?.search()}})}function q(e){u(i.value,e)||(i.value=e,Q.updateWrapperQuery())}function B(e){q(e),n?.insetSearch()}function E(e){if(o(e))return[];const{valueKey:r,childrenKey:n}=t;return p(d.value,(t=>t[r]===e)).map((e=>e[r]),n).filter(Boolean)}return e.watch(O,(e=>e&&function(){const{backfill:e,field:r,fields:n}=t;if(e)if(n){const t=n.reduce(((t,r)=>(e[r]&&t.push(e[r]),t)),[]);if(t.length)return i.value=t,void Q.updateWrapperQuery()}else if(e[r])return i.value=E(e[r]),void Q.updateWrapperQuery();l.value?.length&&(i.value="string"==typeof l.value?E(l.value):l.value.slice(),"string"==typeof l.value&&(l.value=f(i.value)),Q.updateWrapperQuery())}()),{immediate:!0}),F.push(e.watch((()=>t.fields||[t.field]),((e,t)=>{e.toString()!==t.toString()&&n&&t.forEach((t=>e.includes(t)||n.removeUnreferencedField(t))),Q.updateWrapperQuery()}))),F.push(e.watch((()=>[t.fields?.toString()||t.field,t.fields?.map((e=>t.query[e])).filter(Boolean)||t.query[t.field]]),(([e,t],[r,n])=>{m.value&&e===r&&u(t,n)}))),F.push(e.watch((()=>t.fields?.length?t.fields.reduce(((e,r)=>(t.backfill?.[r]&&e.push(t.backfill[r]),e)),[]):t.backfill?.[t.field]),((e,t)=>{O.value&&(u(e,t)||w())}))),F.push(e.watch((()=>[t.depend,t.dependFields,t.dependFields&&[].concat(t.dependFields).map((e=>t.query?.[e]))]),(([e,t,r],[n,l,i])=>{V.value&&(u(r,i)||(S("depend"),e===n&&t?.toString()===l?.toString()&&q(void 0)))}))),F.push(e.watch((()=>t.getOptions),S.bind(null,"initial"),{immediate:!0})),{wrapper:n,option:Q,checked:i,getQuery:g,finalOption:d,insetDisabled:k,insetHide:x,change:B,reset:Q.reset}},exports.useWrapper=function(n,l){const i=[];e.onBeforeUnmount((()=>i.splice(0)));const a={};let u=!1,o=[];const s={realtime:e.ref(n.realtime),register(r){i.push(r);const l=()=>{u=!0,r.reset(),r.updateWrapperQuery();const t=i.indexOf(r);-1!==t&&i.splice(t,1),n.searchAtDatumChanged&&p(),u=!1,o.forEach((t=>{e.del(d.value,t),delete a[t]})),o=[]},s=e.getCurrentInstance();return s&&e.onBeforeUnmount(l,t?s.proxy:s),l},updateQueryValue(t,r){u&&o.push(t),e.set(d.value,t,r),a[t]=r},insetSearch(){n.realtime&&p()},search:p,removeUnreferencedField(t){let r=0;i.some((e=>(e.getQuery().hasOwnProperty(t)&&(r+=1),r))),r||(e.del(d.value,t),delete a[t])}};e.provide(r,s);const d=e.ref({...n.backfill}),f=()=>({...d.value,...n.backfill,...a});async function p(){const e=await c();e?n.toast?.(e):l?.search?.(f())}async function c(){return(await Promise.all(i.map((e=>e.validator?.(d.value))))).find((e=>e&&"string"==typeof e))}return{child:i,wrapperInstance:s,query:d,getQuery:f,search:p,reset:function(){i.forEach((e=>{e.reset(),e.updateWrapperQuery()})),l?.reset?.(f())},validate:c}},exports.wrapperProps=i; | ||
//# sourceMappingURL=index.cjs.min.js.map |
@@ -1,2 +0,2 @@ | ||
import { version, onBeforeUnmount, ref, getCurrentInstance, set, del, provide, markRaw, computed, watch, nextTick, inject, toRaw } from 'vue-demi'; | ||
import { version, onBeforeUnmount, ref, getCurrentInstance, set, del, provide, markRaw, computed, watch, nextTick, inject, shallowRef } from 'vue-demi'; | ||
@@ -151,9 +151,45 @@ /** 判断是否是 2.7.* 版本, 监听生命周期需对该版本做处理 */ | ||
} | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
function shallowDeep(val) { | ||
return Array.isArray(val) ? [...val] : val; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
function isEqualExcludeEmptyValue(x, y) { | ||
return isEmptyValue(x) && isEmptyValue(y) || isEqual(x, y); | ||
} | ||
/** 判断是否是空值(null, undefined, '') */ | ||
function isEmptyValue(val) { | ||
return val === undefined || val === '' || val === null; | ||
} | ||
/** 判断两个值是否一致 */ | ||
function isEqual(x, y) { | ||
if (Object.is(x, y)) return true; | ||
if (x instanceof Date && y instanceof Date) { | ||
return x.getTime() === y.getTime(); | ||
} | ||
if (x instanceof RegExp && y instanceof RegExp) { | ||
return x.toString() === y.toString(); | ||
} | ||
if (typeof x !== 'object' || x === null || typeof y !== 'object' || y === null) { | ||
return false; | ||
} | ||
const keysX = Reflect.ownKeys(x); | ||
const keysY = Reflect.ownKeys(y); | ||
if (keysX.length !== keysY.length) return false; | ||
for (let i = 0; i < keysX.length; i++) { | ||
if (!Reflect.has(y, keysX[i])) return false; | ||
if (!isEqual(x[keysX[i]], y[keysX[i]])) return false; | ||
} | ||
return true; | ||
} | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
function isPrimitive(value) { | ||
return value === undefined || value === null || typeof value !== 'object' && typeof value !== 'function'; | ||
} | ||
/** 拷贝 */ | ||
function clone(obj, deep) { | ||
if (isPrimitive(obj)) return obj; | ||
if (typeof obj === 'function') return obj.bind({}); | ||
const newObj = new obj.constructor(); | ||
Object.getOwnPropertyNames(obj).forEach(prop => { | ||
newObj[prop] = deep ? clone(obj[prop]) : obj[prop]; | ||
}); | ||
return newObj; | ||
} | ||
/** | ||
@@ -183,3 +219,4 @@ * 获取指定层级的父级(包括自身) | ||
function getNode(node, ...args) { | ||
if (!node) return; | ||
// 直接抛出 null, template 中会报错 | ||
if (!node) return null; | ||
return typeof node === 'function' ? node(...args) : typeof node === 'string' ? node : markRaw(node); | ||
@@ -267,6 +304,2 @@ } | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str$1(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** 封装扁平组件必备的信息 */ | ||
@@ -283,5 +316,3 @@ function usePlain(props) { | ||
/** 当前选中值 */ | ||
const checked = ref(initialBackfillValue || | ||
// 防止数组引用导致默认值发生改变 | ||
shallowDeep(props.defaultValue !== undefined ? initialValue.value : props.multiple ? [] : '')); | ||
const checked = shallowRef(initialBackfillValue || props.defaultValue !== undefined ? clone(initialValue.value) : undefined); | ||
/** 远程获取的数据源 */ | ||
@@ -293,4 +324,5 @@ const remoteOption = ref([]); | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(checked.value?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(shallowDeep(toRaw(checked.value)), props.emptyValue) | ||
const _checked = clone(checked.value); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(_checked?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(_checked, props.emptyValue) | ||
}; | ||
@@ -346,6 +378,6 @@ }; | ||
([_field, val], [__field]) => { | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
// 仅在值发生变化时同步 忽视空值不一致的问题 | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (!realtimeFlag.value) return; | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateCheckedValue(_val); | ||
@@ -357,3 +389,3 @@ })); | ||
const _val = props.backfillToValue(val, _field, props.backfill); | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateBackfillFlag(); | ||
@@ -363,3 +395,3 @@ updateCheckedValue(_val); | ||
// 存在依赖项 | ||
unwatchs.push(watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
@@ -369,4 +401,4 @@ if (val === oldVal) return; | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (checked.value === undefined || checked.value.toString() === '') return; | ||
if (_depend !== __depend || !isEqualExcludeEmptyValue(_dependFields, __dependFields)) return; | ||
if (isEmptyValue(checked.value)) return; | ||
updateCheckedValue(props.multiple ? [] : ''); | ||
@@ -400,3 +432,3 @@ })); | ||
* 更新选中值(父级也同步更改) | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -410,3 +442,3 @@ function updateCheckedValue(value) { | ||
* 更新选中值并触发内部搜索事件 | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -438,2 +470,7 @@ function change(value) { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -499,7 +536,2 @@ query: { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 数据源 */ | ||
@@ -516,6 +548,5 @@ options: { | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
/** 封装 tree 组件必备的信息 */ | ||
@@ -528,3 +559,3 @@ function useTree(props) { | ||
/** 当前选中值 */ | ||
const checked = ref([]); | ||
const checked = ref(); | ||
/** 远程获取的数据源 */ | ||
@@ -534,2 +565,6 @@ const remoteOption = ref([]); | ||
const finalOption = computed(() => remoteOption.value.length ? remoteOption.value : props.options); | ||
/** 字段集合 */ | ||
const fieldObj = computed(() => props.fields ? props.fields.reduce((p, v) => (p[v] = props.emptyValue, p), {}) : { | ||
[props.field]: props.emptyValue | ||
}); | ||
/** 获取当前条件所拥有的值 */ | ||
@@ -539,8 +574,19 @@ const getQuery = () => { | ||
if (!sourceIsInit.value && !initialValue.value) return {}; | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.fields?.length ? props.fields.reduce((p, v, i) => Object.assign(p, { | ||
[v]: emptyToValue(checked.value[i], props.emptyValue) | ||
}), {}) : { | ||
[props.field]: emptyToValue(props.emitPath ? [...checked.value] : checked.value.slice(-1)[0], props.emptyValue) | ||
}; | ||
let _checked = clone(checked.value); | ||
const { | ||
...result | ||
} = fieldObj.value; | ||
if (props.customGetQuery) return props.customGetQuery(_checked, emptyToValue, props); | ||
if (!_checked) return result; | ||
if (!props.emitPath) { | ||
_checked = props.multiple ? _checked.map(v => v?.slice(-1)[0]) : _checked.slice(-1)[0]; | ||
} | ||
if (Array.isArray(_checked)) { | ||
props.fields ? _checked.forEach((o, i) => { | ||
result[props.fields[i]] = emptyToValue(o, props.emptyValue); | ||
}) : result[props.field] = _checked; | ||
} else { | ||
result[props.fields?.[0] || props.field] = emptyToValue(_checked, props.emptyValue); | ||
} | ||
return result; | ||
}; | ||
@@ -614,4 +660,6 @@ // 防止触发搜索时, query 产生变化内部重复赋值 | ||
if (initialValue.value?.length) { | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// 此种情况比较多 可封装成一个函数处理 | ||
checked.value = typeof initialValue.value === 'string' ? insideGetChained(initialValue.value) : initialValue.value.slice(); | ||
typeof initialValue.value === 'string' && (initialValue.value = checked.value.slice()); | ||
typeof initialValue.value === 'string' && (initialValue.value = clone(checked.value)); | ||
option.updateWrapperQuery(); | ||
@@ -628,5 +676,6 @@ } | ||
// 仅在值发生变化时同步 | ||
if (_field !== __field || emptyValue2Str(val) === emptyValue2Str(oldVal)) return; | ||
if (!realtimeFlag.value) return; | ||
checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
if (_field !== __field || isEqualExcludeEmptyValue(val, oldVal)) return; | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
})); | ||
@@ -639,23 +688,16 @@ // 回填值发生变化时触发更新 | ||
if (!sourceIsInit.value) return; | ||
if (emptyValue2Str(value) === emptyValue2Str(oldVal)) return; | ||
if (isEqualExcludeEmptyValue(value, oldVal)) return; | ||
updateBackfillFlag(); | ||
if (Array.isArray(value)) { | ||
updateCheckedValue(value); | ||
} else { | ||
if (!value && value !== 0) { | ||
checked.value.length && (checked.value = []); | ||
return; | ||
} | ||
updateCheckedValue(insideGetChained(value)); | ||
} | ||
// TODO 判断最后一个值是否与选中项最后一个一致(多选需遍历) | ||
// 如果一致则不更新, 不一致, 则通过 insideGetChained 获取到树形再赋值 | ||
// updateCheckedValue(value); | ||
})); | ||
// 存在依赖项 | ||
unwatchs.push(watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
if (val === oldVal) return; | ||
if (isEqualExcludeEmptyValue(val, oldVal)) return; | ||
getOption('depend'); | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (!checked.value.length) return; | ||
updateCheckedValue(typeof checked.value === 'string' ? '' : []); | ||
updateCheckedValue(undefined); | ||
})); | ||
@@ -688,5 +730,4 @@ unwatchs.push(watch(() => props.getOptions, getOption.bind(null, 'initial'), { | ||
function updateCheckedValue(values) { | ||
const _checked = Array.isArray(values) ? values : insideGetChained(values); | ||
if (_checked.join('') === checked.value.join('')) return; | ||
checked.value = _checked; | ||
if (isEqualExcludeEmptyValue(checked.value, values)) return; | ||
checked.value = values; | ||
option.updateWrapperQuery(); | ||
@@ -699,3 +740,3 @@ } | ||
function change(values) { | ||
updateCheckedValue(values || []); | ||
updateCheckedValue(values); | ||
wrapper?.insetSearch(); | ||
@@ -707,3 +748,3 @@ } | ||
function insideGetChained(val) { | ||
if (!val && val !== 0) return []; | ||
if (isEmptyValue(val)) return []; | ||
const { | ||
@@ -744,3 +785,3 @@ valueKey, | ||
}, | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
emitPath: { | ||
@@ -761,3 +802,3 @@ type: [Boolean], | ||
export { IS_COMPOSITION_VERSION, commonProps, defineCommonMethod, defineProvideValue, emptyToValue, getChained, getNode, plainProps, provideKey, shallowDeep, treeProps, usePlain, useTree, useWrapper, wrapperProps }; | ||
export { IS_COMPOSITION_VERSION, clone, commonProps, defineCommonMethod, defineProvideValue, emptyToValue, getChained, getNode, isEmptyValue, isEqual, isEqualExcludeEmptyValue, isPrimitive, plainProps, provideKey, treeProps, usePlain, useTree, useWrapper, wrapperProps }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,2 @@ | ||
import{version as e,onBeforeUnmount as t,ref as l,getCurrentInstance as a,set as i,del as u,provide as n,markRaw as r,computed as o,watch as d,nextTick as s,inject as p,toRaw as f}from"vue-demi";const c="2.7"===e.slice(0,3),y="condition-wrapper";function v(e){return e}function g(e){return e}function h(e,r){const o=[];t((()=>o.splice(0)));const d={};let s=!1,p=[];const f={realtime:l(e.realtime),register(l){o.push(l);const i=()=>{s=!0,l.reset(),l.updateWrapperQuery();const t=o.indexOf(l);-1!==t&&o.splice(t,1),e.searchAtDatumChanged&&h(),s=!1,p.forEach((e=>{u(v.value,e),delete d[e]})),p=[]},n=a();return n&&t(i,c?n.proxy:n),i},updateQueryValue(e,t){s&&p.push(e),i(v.value,e,t),d[e]=t},insetSearch(){e.realtime&&h()},search:h,removeUnreferencedField(e){let t=0;o.some((l=>(l.getQuery().hasOwnProperty(e)&&(t+=1),t))),t||(u(v.value,e),delete d[e])}};n(y,f);const v=l({...e.backfill}),g=()=>({...v.value,...e.backfill,...d});async function h(){const t=await m();t?e.toast?.(t):r?.search?.(g())}async function m(){return(await Promise.all(o.map((e=>e.validator?.(v.value))))).find((e=>e&&"string"==typeof e))}return{child:o,wrapperInstance:f,query:v,getQuery:g,search:h,reset:function(){o.forEach((e=>{e.reset(),e.updateWrapperQuery()})),r?.reset?.(g())},validate:m}}const m={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function b(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function Q(e){return Array.isArray(e)?[...e]:e}function F(e,t,l="children"){for(const a of e){if(t(a))return[a];if(a[l]?.length){const e=F(a[l],t);if(e.length)return e.unshift(a),e}}return[]}function k(e,...t){if(e)return"function"==typeof e?e(...t):"string"==typeof e?e:r(e)}function S(e){const t=l();return o({set(e){t.value=e},get:()=>void 0===t.value?void 0!==e.defaultValue?"function"==typeof e.defaultValue?e.defaultValue(e.query,e.backfill):e.defaultValue:void 0:t.value})}function V(e,a){const i=l("boolean"==typeof e.disabled&&e.disabled),u=l("boolean"==typeof e.hide&&e.hide),n=()=>({query:e.query,backfill:e.backfill,option:a}),r=()=>{if("function"==typeof e.hide){u.value!==e.hide(n())&&(u.value=e.hide(n()))}else if("function"==typeof e.disabled){i.value!==e.disabled(n())&&(i.value=e.disabled(n()))}};let o=[d((()=>e.query),r,{immediate:!0,deep:!0}),d((()=>[e.disabled,e.hide]),((e,t)=>{e[0]!==t[0]&&(i.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(u.value="boolean"==typeof e[1]&&e[1],e[1]),r()}))];return t((()=>(o.forEach((e=>e())),o=[]))),{insetDisabled:i,insetHide:u}}function q(e=!0){const t=l(e);return{flag:t,updateFlag:()=>{t.value=!e,s((()=>{t.value=e}))}}}function O(e){return e?.toString()??""}function A(e){const a=p(y),i=S(e),u=e.backfill&&(e.fields?.length?e.fields.map((t=>e.backfill[t])).filter(Boolean):e.backfill[e.field]),n=l(u||Q(void 0!==e.defaultValue?i.value:e.multiple?[]:"")),r=l([]),s=o((()=>r.value.length?r.value:e.options)),c=()=>e.customGetQuery?e.customGetQuery(n.value,b,e):e.multiple&&e.fields?e.fields.reduce(((t,l,a)=>(t[l]=b(n.value?.[a],e.emptyValue),t)),{}):{[e.field]:b(Q(f(n.value)),e.emptyValue)},{flag:v,updateFlag:g}=q(),{flag:h,updateFlag:m}=q(),F={reset(){const{multiple:t}=e;g(),m(),n.value=e.resetToInitialValue&&i.value?.slice()||(t?[]:"")},updateWrapperQuery(){g(),a&&Object.entries(c()).forEach((([e,t])=>a.updateQueryValue(e,t)))},get validator(){return e.validator},getQuery:c};a?.register(F);const{insetDisabled:k,insetHide:A}=V(e,F);!u&&e.defaultValue&&F.updateWrapperQuery();const B=[];function W(t){e.getOptions?.((e=>{const t=n.value;n.value=void 0,r.value=e||[],n.value=t}),e.query||{},{trigger:t,change:(e,t)=>{t&&(i.value=e),E(e)},search:(e,t)=>{t&&(i.value=e),j(e),a?.search()}})}function j(e){e!==n.value&&(n.value=e,F.updateWrapperQuery())}function E(e){j(e),a?.insetSearch()}return t((()=>B.forEach((e=>e())))),B.push(d((()=>e.field),((e,t)=>{e!==t&&a?.removeUnreferencedField(t),F.updateWrapperQuery()}))),B.push(d((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.query[t])).filter(Boolean):e.query[e.field]]),(([t,l],[a])=>{const i=e.backfillToValue(l,t,e.query);t.toString()===a.toString()&&O(i)!==O(n.value)&&v.value&&j(i)}))),B.push(d((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.backfill?.[t])).filter(Boolean):e.backfill?.[e.field]]),(([t,l],[a])=>{const i=e.backfillToValue(l,t,e.backfill);t.toString()===a.toString()&&O(i)!==O(n.value)&&(m(),j(i))}))),B.push(d((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t])).join(",")||""]),(([t,l,a],[i,u,r])=>{h.value&&a!==r&&(W("depend"),t===i&&l?.toString()===u?.toString()&&void 0!==n.value&&""!==n.value.toString()&&j(e.multiple?[]:""))}))),B.push(d((()=>e.getOptions),W.bind(null,"initial"),{immediate:!0})),{wrapper:a,option:F,checked:n,getQuery:c,insetDisabled:k,insetHide:A,finalOption:s,updateCheckedValue:j,change:E,reset:F.reset}}const B={field:{type:String,required:!0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},W={...B,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},multiple:{type:Boolean,default:void 0},options:{type:Array,default:()=>[]},getOptions:{type:Function}};function j(e){return e?.toString()??""}function E(e){const a=p(y),i=S(e),u=l([]),n=l([]),r=o((()=>n.value.length?n.value:e.options)),s=()=>O.value||i.value?e.customGetQuery?e.customGetQuery(u.value,b,e):e.fields?.length?e.fields.reduce(((t,l,a)=>Object.assign(t,{[l]:b(u.value[a],e.emptyValue)})),{}):{[e.field]:b(e.emitPath?[...u.value]:u.value.slice(-1)[0],e.emptyValue)}:{},{flag:f,updateFlag:c}=q(),{flag:v,updateFlag:g}=q(),h={reset(){return c(),g(),u.value=e.resetToInitialValue&&i.value?.slice()||[],this},get validator(){return e.validator},updateWrapperQuery(){c(),a&&Object.entries(s()).forEach((([e,t])=>a.updateQueryValue(e,t)))},getQuery:s};a?.register(h);const{insetDisabled:m,insetHide:Q}=V(e,h),k=[];t((()=>k.forEach((e=>e()))));const O=l("function"!=typeof e.getOptions||!!e.fields?.length);function A(t){e.getOptions?.((e=>{n.value=e||[],O.value=!0}),e.query||{},{trigger:t,change:(e,t)=>{t&&(i.value=e),W(e)},search:(e,t)=>{t&&(i.value=e),B(e),a?.search()}})}function B(e){const t=Array.isArray(e)?e:E(e);t.join("")!==u.value.join("")&&(u.value=t,h.updateWrapperQuery())}function W(e){B(e||[]),a?.insetSearch()}function E(t){if(!t&&0!==t)return[];const{valueKey:l,childrenKey:a}=e;return F(r.value,(e=>e[l]===t)).map((e=>e[l]),a).filter(Boolean)}return d(O,(t=>t&&function(){const{backfill:t,field:l,fields:a}=e;if(t)if(a){const e=a.reduce(((e,l)=>(t[l]&&e.push(t[l]),e)),[]);if(e.length)return u.value=e,void h.updateWrapperQuery()}else if(t[l])return u.value=E(t[l]),void h.updateWrapperQuery();i.value?.length&&(u.value="string"==typeof i.value?E(i.value):i.value.slice(),"string"==typeof i.value&&(i.value=u.value.slice()),h.updateWrapperQuery())}()),{immediate:!0}),k.push(d((()=>e.fields||[e.field]),((e,t)=>{e.toString()!==t.toString()&&a&&t.forEach((t=>e.includes(t)||a.removeUnreferencedField(t))),h.updateWrapperQuery()}))),k.push(d((()=>[e.fields?.toString()||e.field,e.fields?.map((t=>e.query[t])).filter(Boolean)||e.query[e.field]]),(([e,t],[l,a])=>{e===l&&j(t)!==j(a)&&f.value&&(u.value="string"==typeof t?E(t):t)}))),k.push(d((()=>e.fields?.length?e.fields.reduce(((t,l)=>(e.backfill?.[l]&&t.push(e.backfill[l]),t)),[]):e.backfill?.[e.field]),((e,t)=>{if(O.value&&j(e)!==j(t))if(g(),Array.isArray(e))B(e);else{if(!e&&0!==e)return void(u.value.length&&(u.value=[]));B(E(e))}}))),k.push(d((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t])).join(",")||""]),(([e,t,l],[a,i,n])=>{v.value&&l!==n&&(A("depend"),e===a&&t?.toString()===i?.toString()&&u.value.length&&B("string"==typeof u.value?"":[]))}))),k.push(d((()=>e.getOptions),A.bind(null,"initial"),{immediate:!0})),{wrapper:a,option:h,checked:u,getQuery:s,finalOption:r,insetDisabled:m,insetHide:Q,change:W,reset:h.reset}}const w={...B,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};export{c as IS_COMPOSITION_VERSION,B as commonProps,g as defineCommonMethod,v as defineProvideValue,b as emptyToValue,F as getChained,k as getNode,W as plainProps,y as provideKey,Q as shallowDeep,w as treeProps,A as usePlain,E as useTree,h as useWrapper,m as wrapperProps}; | ||
import{version as e,onBeforeUnmount as t,ref as l,getCurrentInstance as i,set as n,del as a,provide as u,markRaw as r,computed as o,watch as d,nextTick as f,inject as s,shallowRef as c}from"vue-demi";const p="2.7"===e.slice(0,3),y="condition-wrapper";function v(e){return e}function g(e){return e}function h(e,r){const o=[];t((()=>o.splice(0)));const d={};let f=!1,s=[];const c={realtime:l(e.realtime),register(l){o.push(l);const n=()=>{f=!0,l.reset(),l.updateWrapperQuery();const t=o.indexOf(l);-1!==t&&o.splice(t,1),e.searchAtDatumChanged&&h(),f=!1,s.forEach((e=>{a(v.value,e),delete d[e]})),s=[]},u=i();return u&&t(n,p?u.proxy:u),n},updateQueryValue(e,t){f&&s.push(e),n(v.value,e,t),d[e]=t},insetSearch(){e.realtime&&h()},search:h,removeUnreferencedField(e){let t=0;o.some((l=>(l.getQuery().hasOwnProperty(e)&&(t+=1),t))),t||(a(v.value,e),delete d[e])}};u(y,c);const v=l({...e.backfill}),g=()=>({...v.value,...e.backfill,...d});async function h(){const t=await m();t?e.toast?.(t):r?.search?.(g())}async function m(){return(await Promise.all(o.map((e=>e.validator?.(v.value))))).find((e=>e&&"string"==typeof e))}return{child:o,wrapperInstance:c,query:v,getQuery:g,search:h,reset:function(){o.forEach((e=>{e.reset(),e.updateWrapperQuery()})),r?.reset?.(g())},validate:m}}const m={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function b(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function Q(e,t){return F(e)&&F(t)||V(e,t)}function F(e){return void 0===e||""===e||null===e}function V(e,t){if(Object.is(e,t))return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.toString()===t.toString();if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const l=Reflect.ownKeys(e),i=Reflect.ownKeys(t);if(l.length!==i.length)return!1;for(let i=0;i<l.length;i++){if(!Reflect.has(t,l[i]))return!1;if(!V(e[l[i]],t[l[i]]))return!1}return!0}function k(e){return null==e||"object"!=typeof e&&"function"!=typeof e}function S(e,t){if(k(e))return e;if("function"==typeof e)return e.bind({});const l=new e.constructor;return Object.getOwnPropertyNames(e).forEach((i=>{l[i]=t?S(e[i]):e[i]})),l}function O(e,t,l="children"){for(const i of e){if(t(i))return[i];if(i[l]?.length){const e=O(i[l],t);if(e.length)return e.unshift(i),e}}return[]}function q(e,...t){return e?"function"==typeof e?e(...t):"string"==typeof e?e:r(e):null}function B(e){const t=l();return o({set(e){t.value=e},get:()=>void 0===t.value?void 0!==e.defaultValue?"function"==typeof e.defaultValue?e.defaultValue(e.query,e.backfill):e.defaultValue:void 0:t.value})}function A(e,i){const n=l("boolean"==typeof e.disabled&&e.disabled),a=l("boolean"==typeof e.hide&&e.hide),u=()=>({query:e.query,backfill:e.backfill,option:i}),r=()=>{if("function"==typeof e.hide){a.value!==e.hide(u())&&(a.value=e.hide(u()))}else if("function"==typeof e.disabled){n.value!==e.disabled(u())&&(n.value=e.disabled(u()))}};let o=[d((()=>e.query),r,{immediate:!0,deep:!0}),d((()=>[e.disabled,e.hide]),((e,t)=>{e[0]!==t[0]&&(n.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(a.value="boolean"==typeof e[1]&&e[1],e[1]),r()}))];return t((()=>(o.forEach((e=>e())),o=[]))),{insetDisabled:n,insetHide:a}}function E(e=!0){const t=l(e);return{flag:t,updateFlag:()=>{t.value=!e,f((()=>{t.value=e}))}}}function W(e){const i=s(y),n=B(e),a=e.backfill&&(e.fields?.length?e.fields.map((t=>e.backfill[t])).filter(Boolean):e.backfill[e.field]),u=c(a||void 0!==e.defaultValue?S(n.value):void 0),r=l([]),f=o((()=>r.value.length?r.value:e.options)),p=()=>{if(e.customGetQuery)return e.customGetQuery(u.value,b,e);const t=S(u.value);return e.multiple&&e.fields?e.fields.reduce(((l,i,n)=>(l[i]=b(t?.[n],e.emptyValue),l)),{}):{[e.field]:b(t,e.emptyValue)}},{flag:v,updateFlag:g}=E(),{flag:h,updateFlag:m}=E(),V={reset(){const{multiple:t}=e;g(),m(),u.value=e.resetToInitialValue&&n.value?.slice()||(t?[]:"")},updateWrapperQuery(){g(),i&&Object.entries(p()).forEach((([e,t])=>i.updateQueryValue(e,t)))},get validator(){return e.validator},getQuery:p};i?.register(V);const{insetDisabled:k,insetHide:O}=A(e,V);!a&&e.defaultValue&&V.updateWrapperQuery();const q=[];function W(t){e.getOptions?.((e=>{const t=u.value;u.value=void 0,r.value=e||[],u.value=t}),e.query||{},{trigger:t,change:(e,t)=>{t&&(n.value=e),j(e)},search:(e,t)=>{t&&(n.value=e),w(e),i?.search()}})}function w(e){e!==u.value&&(u.value=e,V.updateWrapperQuery())}function j(e){w(e),i?.insetSearch()}return t((()=>q.forEach((e=>e())))),q.push(d((()=>e.field),((e,t)=>{e!==t&&i?.removeUnreferencedField(t),V.updateWrapperQuery()}))),q.push(d((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.query[t])).filter(Boolean):e.query[e.field]]),(([t,l],[i])=>{if(!v.value)return;const n=e.backfillToValue(l,t,e.query);t.toString()!==i.toString()||Q(n,u.value)||w(n)}))),q.push(d((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.backfill?.[t])).filter(Boolean):e.backfill?.[e.field]]),(([t,l],[i])=>{const n=e.backfillToValue(l,t,e.backfill);t.toString()!==i.toString()||Q(n,u.value)||(m(),w(n))}))),q.push(d((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t]))]),(([t,l,i],[n,a,r])=>{h.value&&i!==r&&(W("depend"),t===n&&Q(l,a)&&(F(u.value)||w(e.multiple?[]:"")))}))),q.push(d((()=>e.getOptions),W.bind(null,"initial"),{immediate:!0})),{wrapper:i,option:V,checked:u,getQuery:p,insetDisabled:k,insetHide:O,finalOption:f,updateCheckedValue:w,change:j,reset:V.reset}}const w={field:{type:String,required:!0},multiple:{type:Boolean,default:void 0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},j={...w,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},options:{type:Array,default:()=>[]},getOptions:{type:Function}};function D(e){const i=s(y),n=B(e),a=l(),u=l([]),r=o((()=>u.value.length?u.value:e.options)),f=o((()=>e.fields?e.fields.reduce(((t,l)=>(t[l]=e.emptyValue,t)),{}):{[e.field]:e.emptyValue})),c=()=>{if(!W.value&&!n.value)return{};let t=S(a.value);const{...l}=f.value;return e.customGetQuery?e.customGetQuery(t,b,e):t?(e.emitPath||(t=e.multiple?t.map((e=>e?.slice(-1)[0])):t.slice(-1)[0]),Array.isArray(t)?e.fields?t.forEach(((t,i)=>{l[e.fields[i]]=b(t,e.emptyValue)})):l[e.field]=t:l[e.fields?.[0]||e.field]=b(t,e.emptyValue),l):l},{flag:p,updateFlag:v}=E(),{flag:g,updateFlag:h}=E(),m={reset(){return v(),h(),a.value=e.resetToInitialValue&&n.value?.slice()||[],this},get validator(){return e.validator},updateWrapperQuery(){v(),i&&Object.entries(c()).forEach((([e,t])=>i.updateQueryValue(e,t)))},getQuery:c};i?.register(m);const{insetDisabled:V,insetHide:k}=A(e,m),q=[];t((()=>q.forEach((e=>e()))));const W=l("function"!=typeof e.getOptions||!!e.fields?.length);function w(t){e.getOptions?.((e=>{u.value=e||[],W.value=!0}),e.query||{},{trigger:t,change:(e,t)=>{t&&(n.value=e),D(e)},search:(e,t)=>{t&&(n.value=e),j(e),i?.search()}})}function j(e){Q(a.value,e)||(a.value=e,m.updateWrapperQuery())}function D(e){j(e),i?.insetSearch()}function T(t){if(F(t))return[];const{valueKey:l,childrenKey:i}=e;return O(r.value,(e=>e[l]===t)).map((e=>e[l]),i).filter(Boolean)}return d(W,(t=>t&&function(){const{backfill:t,field:l,fields:i}=e;if(t)if(i){const e=i.reduce(((e,l)=>(t[l]&&e.push(t[l]),e)),[]);if(e.length)return a.value=e,void m.updateWrapperQuery()}else if(t[l])return a.value=T(t[l]),void m.updateWrapperQuery();n.value?.length&&(a.value="string"==typeof n.value?T(n.value):n.value.slice(),"string"==typeof n.value&&(n.value=S(a.value)),m.updateWrapperQuery())}()),{immediate:!0}),q.push(d((()=>e.fields||[e.field]),((e,t)=>{e.toString()!==t.toString()&&i&&t.forEach((t=>e.includes(t)||i.removeUnreferencedField(t))),m.updateWrapperQuery()}))),q.push(d((()=>[e.fields?.toString()||e.field,e.fields?.map((t=>e.query[t])).filter(Boolean)||e.query[e.field]]),(([e,t],[l,i])=>{p.value&&e===l&&Q(t,i)}))),q.push(d((()=>e.fields?.length?e.fields.reduce(((t,l)=>(e.backfill?.[l]&&t.push(e.backfill[l]),t)),[]):e.backfill?.[e.field]),((e,t)=>{W.value&&(Q(e,t)||h())}))),q.push(d((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t]))]),(([e,t,l],[i,n,a])=>{g.value&&(Q(l,a)||(w("depend"),e===i&&t?.toString()===n?.toString()&&j(void 0)))}))),q.push(d((()=>e.getOptions),w.bind(null,"initial"),{immediate:!0})),{wrapper:i,option:m,checked:a,getQuery:c,finalOption:r,insetDisabled:V,insetHide:k,change:D,reset:m.reset}}const T={...w,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};export{p as IS_COMPOSITION_VERSION,S as clone,w as commonProps,g as defineCommonMethod,v as defineProvideValue,b as emptyToValue,O as getChained,q as getNode,F as isEmptyValue,V as isEqual,Q as isEqualExcludeEmptyValue,k as isPrimitive,j as plainProps,y as provideKey,T as treeProps,W as usePlain,D as useTree,h as useWrapper,m as wrapperProps}; | ||
//# sourceMappingURL=index.esm.min.js.map |
@@ -271,9 +271,45 @@ var VueDemi = (function (VueDemi, Vue, VueCompositionAPI) { | ||
} | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
function shallowDeep(val) { | ||
return Array.isArray(val) ? [...val] : val; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
function isEqualExcludeEmptyValue(x, y) { | ||
return isEmptyValue(x) && isEmptyValue(y) || isEqual(x, y); | ||
} | ||
/** 判断是否是空值(null, undefined, '') */ | ||
function isEmptyValue(val) { | ||
return val === undefined || val === '' || val === null; | ||
} | ||
/** 判断两个值是否一致 */ | ||
function isEqual(x, y) { | ||
if (Object.is(x, y)) return true; | ||
if (x instanceof Date && y instanceof Date) { | ||
return x.getTime() === y.getTime(); | ||
} | ||
if (x instanceof RegExp && y instanceof RegExp) { | ||
return x.toString() === y.toString(); | ||
} | ||
if (typeof x !== 'object' || x === null || typeof y !== 'object' || y === null) { | ||
return false; | ||
} | ||
const keysX = Reflect.ownKeys(x); | ||
const keysY = Reflect.ownKeys(y); | ||
if (keysX.length !== keysY.length) return false; | ||
for (let i = 0; i < keysX.length; i++) { | ||
if (!Reflect.has(y, keysX[i])) return false; | ||
if (!isEqual(x[keysX[i]], y[keysX[i]])) return false; | ||
} | ||
return true; | ||
} | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
function isPrimitive(value) { | ||
return value === undefined || value === null || typeof value !== 'object' && typeof value !== 'function'; | ||
} | ||
/** 拷贝 */ | ||
function clone(obj, deep) { | ||
if (isPrimitive(obj)) return obj; | ||
if (typeof obj === 'function') return obj.bind({}); | ||
const newObj = new obj.constructor(); | ||
Object.getOwnPropertyNames(obj).forEach(prop => { | ||
newObj[prop] = deep ? clone(obj[prop]) : obj[prop]; | ||
}); | ||
return newObj; | ||
} | ||
/** | ||
@@ -303,3 +339,4 @@ * 获取指定层级的父级(包括自身) | ||
function getNode(node, ...args) { | ||
if (!node) return; | ||
// 直接抛出 null, template 中会报错 | ||
if (!node) return null; | ||
return typeof node === 'function' ? node(...args) : typeof node === 'string' ? node : vueDemi.markRaw(node); | ||
@@ -387,6 +424,2 @@ } | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str$1(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** 封装扁平组件必备的信息 */ | ||
@@ -403,5 +436,3 @@ function usePlain(props) { | ||
/** 当前选中值 */ | ||
const checked = vueDemi.ref(initialBackfillValue || | ||
// 防止数组引用导致默认值发生改变 | ||
shallowDeep(props.defaultValue !== undefined ? initialValue.value : props.multiple ? [] : '')); | ||
const checked = vueDemi.shallowRef(initialBackfillValue || props.defaultValue !== undefined ? clone(initialValue.value) : undefined); | ||
/** 远程获取的数据源 */ | ||
@@ -413,4 +444,5 @@ const remoteOption = vueDemi.ref([]); | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(checked.value?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(shallowDeep(vueDemi.toRaw(checked.value)), props.emptyValue) | ||
const _checked = clone(checked.value); | ||
return props.multiple && props.fields ? props.fields.reduce((p, k, i) => (p[k] = emptyToValue(_checked?.[i], props.emptyValue), p), {}) : { | ||
[props.field]: emptyToValue(_checked, props.emptyValue) | ||
}; | ||
@@ -466,6 +498,6 @@ }; | ||
([_field, val], [__field]) => { | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
// 仅在值发生变化时同步 忽视空值不一致的问题 | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (!realtimeFlag.value) return; | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateCheckedValue(_val); | ||
@@ -477,3 +509,3 @@ })); | ||
const _val = props.backfillToValue(val, _field, props.backfill); | ||
if (_field.toString() !== __field.toString() || emptyValue2Str$1(_val) === emptyValue2Str$1(checked.value)) return; | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateBackfillFlag(); | ||
@@ -483,3 +515,3 @@ updateCheckedValue(_val); | ||
// 存在依赖项 | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
@@ -489,4 +521,4 @@ if (val === oldVal) return; | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (checked.value === undefined || checked.value.toString() === '') return; | ||
if (_depend !== __depend || !isEqualExcludeEmptyValue(_dependFields, __dependFields)) return; | ||
if (isEmptyValue(checked.value)) return; | ||
updateCheckedValue(props.multiple ? [] : ''); | ||
@@ -520,3 +552,3 @@ })); | ||
* 更新选中值(父级也同步更改) | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -530,3 +562,3 @@ function updateCheckedValue(value) { | ||
* 更新选中值并触发内部搜索事件 | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
@@ -558,2 +590,7 @@ function change(value) { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -619,7 +656,2 @@ query: { | ||
}, | ||
/** 是否多选 */ | ||
multiple: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
/** 数据源 */ | ||
@@ -636,6 +668,5 @@ options: { | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str(val) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
/** 封装 tree 组件必备的信息 */ | ||
@@ -648,3 +679,3 @@ function useTree(props) { | ||
/** 当前选中值 */ | ||
const checked = vueDemi.ref([]); | ||
const checked = vueDemi.ref(); | ||
/** 远程获取的数据源 */ | ||
@@ -654,2 +685,6 @@ const remoteOption = vueDemi.ref([]); | ||
const finalOption = vueDemi.computed(() => remoteOption.value.length ? remoteOption.value : props.options); | ||
/** 字段集合 */ | ||
const fieldObj = vueDemi.computed(() => props.fields ? props.fields.reduce((p, v) => (p[v] = props.emptyValue, p), {}) : { | ||
[props.field]: props.emptyValue | ||
}); | ||
/** 获取当前条件所拥有的值 */ | ||
@@ -659,8 +694,19 @@ const getQuery = () => { | ||
if (!sourceIsInit.value && !initialValue.value) return {}; | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.fields?.length ? props.fields.reduce((p, v, i) => Object.assign(p, { | ||
[v]: emptyToValue(checked.value[i], props.emptyValue) | ||
}), {}) : { | ||
[props.field]: emptyToValue(props.emitPath ? [...checked.value] : checked.value.slice(-1)[0], props.emptyValue) | ||
}; | ||
let _checked = clone(checked.value); | ||
const { | ||
...result | ||
} = fieldObj.value; | ||
if (props.customGetQuery) return props.customGetQuery(_checked, emptyToValue, props); | ||
if (!_checked) return result; | ||
if (!props.emitPath) { | ||
_checked = props.multiple ? _checked.map(v => v?.slice(-1)[0]) : _checked.slice(-1)[0]; | ||
} | ||
if (Array.isArray(_checked)) { | ||
props.fields ? _checked.forEach((o, i) => { | ||
result[props.fields[i]] = emptyToValue(o, props.emptyValue); | ||
}) : result[props.field] = _checked; | ||
} else { | ||
result[props.fields?.[0] || props.field] = emptyToValue(_checked, props.emptyValue); | ||
} | ||
return result; | ||
}; | ||
@@ -734,4 +780,6 @@ // 防止触发搜索时, query 产生变化内部重复赋值 | ||
if (initialValue.value?.length) { | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// 此种情况比较多 可封装成一个函数处理 | ||
checked.value = typeof initialValue.value === 'string' ? insideGetChained(initialValue.value) : initialValue.value.slice(); | ||
typeof initialValue.value === 'string' && (initialValue.value = checked.value.slice()); | ||
typeof initialValue.value === 'string' && (initialValue.value = clone(checked.value)); | ||
option.updateWrapperQuery(); | ||
@@ -748,5 +796,6 @@ } | ||
// 仅在值发生变化时同步 | ||
if (_field !== __field || emptyValue2Str(val) === emptyValue2Str(oldVal)) return; | ||
if (!realtimeFlag.value) return; | ||
checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
if (_field !== __field || isEqualExcludeEmptyValue(val, oldVal)) return; | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
})); | ||
@@ -759,23 +808,16 @@ // 回填值发生变化时触发更新 | ||
if (!sourceIsInit.value) return; | ||
if (emptyValue2Str(value) === emptyValue2Str(oldVal)) return; | ||
if (isEqualExcludeEmptyValue(value, oldVal)) return; | ||
updateBackfillFlag(); | ||
if (Array.isArray(value)) { | ||
updateCheckedValue(value); | ||
} else { | ||
if (!value && value !== 0) { | ||
checked.value.length && (checked.value = []); | ||
return; | ||
} | ||
updateCheckedValue(insideGetChained(value)); | ||
} | ||
// TODO 判断最后一个值是否与选中项最后一个一致(多选需遍历) | ||
// 如果一致则不更新, 不一致, 则通过 insideGetChained 获取到树形再赋值 | ||
// updateCheckedValue(value); | ||
})); | ||
// 存在依赖项 | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k]).join(',') || ''], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
unwatchs.push(vueDemi.watch(() => [props.depend, props.dependFields, props.dependFields && [].concat(props.dependFields).map(k => props.query?.[k])], ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
if (val === oldVal) return; | ||
if (isEqualExcludeEmptyValue(val, oldVal)) return; | ||
getOption('depend'); | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (!checked.value.length) return; | ||
updateCheckedValue(typeof checked.value === 'string' ? '' : []); | ||
updateCheckedValue(undefined); | ||
})); | ||
@@ -808,5 +850,4 @@ unwatchs.push(vueDemi.watch(() => props.getOptions, getOption.bind(null, 'initial'), { | ||
function updateCheckedValue(values) { | ||
const _checked = Array.isArray(values) ? values : insideGetChained(values); | ||
if (_checked.join('') === checked.value.join('')) return; | ||
checked.value = _checked; | ||
if (isEqualExcludeEmptyValue(checked.value, values)) return; | ||
checked.value = values; | ||
option.updateWrapperQuery(); | ||
@@ -819,3 +860,3 @@ } | ||
function change(values) { | ||
updateCheckedValue(values || []); | ||
updateCheckedValue(values); | ||
wrapper?.insetSearch(); | ||
@@ -827,3 +868,3 @@ } | ||
function insideGetChained(val) { | ||
if (!val && val !== 0) return []; | ||
if (isEmptyValue(val)) return []; | ||
const { | ||
@@ -864,3 +905,3 @@ valueKey, | ||
}, | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
emitPath: { | ||
@@ -882,2 +923,3 @@ type: [Boolean], | ||
exports.IS_COMPOSITION_VERSION = IS_COMPOSITION_VERSION; | ||
exports.clone = clone; | ||
exports.commonProps = commonProps; | ||
@@ -889,5 +931,8 @@ exports.defineCommonMethod = defineCommonMethod; | ||
exports.getNode = getNode; | ||
exports.isEmptyValue = isEmptyValue; | ||
exports.isEqual = isEqual; | ||
exports.isEqualExcludeEmptyValue = isEqualExcludeEmptyValue; | ||
exports.isPrimitive = isPrimitive; | ||
exports.plainProps = plainProps; | ||
exports.provideKey = provideKey; | ||
exports.shallowDeep = shallowDeep; | ||
exports.treeProps = treeProps; | ||
@@ -894,0 +939,0 @@ exports.usePlain = usePlain; |
@@ -1,2 +0,2 @@ | ||
var VueDemi=function(e,t,i){if(e.install)return e;if(!t)return console.error("[vue-demi] no Vue instance found, please be sure to import `vue` before `vue-demi`."),e;if("2.7."===t.version.slice(0,4)){for(var n in t)e[n]=t[n];e.isVue2=!0,e.isVue3=!1,e.install=function(){},e.Vue=t,e.Vue2=t,e.version=t.version,e.warn=t.util.warn,e.hasInjectionContext=()=>!!e.getCurrentInstance(),e.createApp=function(e,i){var n,r={},u={config:t.config,use:t.use.bind(t),mixin:t.mixin.bind(t),component:t.component.bind(t),provide:function(e,t){return r[e]=t,this},directive:function(e,i){return i?(t.directive(e,i),u):t.directive(e)},mount:function(u,a){return n||((n=new t(Object.assign({propsData:i},e,{provide:Object.assign(r,e.provide)}))).$mount(u,a),n)},unmount:function(){n&&(n.$destroy(),n=void 0)}};return u}}else if("2."===t.version.slice(0,2))if(i){for(var n in i)e[n]=i[n];e.isVue2=!0,e.isVue3=!1,e.install=function(){},e.Vue=t,e.Vue2=t,e.version=t.version,e.hasInjectionContext=()=>!!e.getCurrentInstance()}else console.error("[vue-demi] no VueCompositionAPI instance found, please be sure to import `@vue/composition-api` before `vue-demi`.");else if("3."===t.version.slice(0,2)){for(var n in t)e[n]=t[n];e.isVue2=!1,e.isVue3=!0,e.install=function(){},e.Vue=t,e.Vue2=void 0,e.version=t.version,e.set=function(e,t,i){return Array.isArray(e)?(e.length=Math.max(e.length,t),e.splice(t,1,i),i):(e[t]=i,i)},e.del=function(e,t){Array.isArray(e)?e.splice(t,1):delete e[t]}}else console.error("[vue-demi] Vue version "+t.version+" is unsupported.");return e}(this.VueDemi=this.VueDemi||(void 0!==VueDemi?VueDemi:{}),this.Vue||("undefined"!=typeof Vue?Vue:void 0),this.VueCompositionAPI||("undefined"!=typeof VueCompositionAPI?VueCompositionAPI:void 0));!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue-demi")):"function"==typeof define&&define.amd?define(["exports","vue-demi"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CoreCondition={},e.VueDemi)}(this,(function(e,t){"use strict";const i="2.7"===t.version.slice(0,3),n="condition-wrapper";function r(e){return e}function u(e){return e}const a={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function l(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function o(e){return Array.isArray(e)?[...e]:e}function s(e,t,i="children"){for(const n of e){if(t(n))return[n];if(n[i]?.length){const e=s(n[i],t);if(e.length)return e.unshift(n),e}}return[]}function d(e){const i=t.ref();return t.computed({set(e){i.value=e},get:()=>void 0===i.value?void 0!==e.defaultValue?"function"==typeof e.defaultValue?e.defaultValue(e.query,e.backfill):e.defaultValue:void 0:i.value})}function f(e,i){const n=t.ref("boolean"==typeof e.disabled&&e.disabled),r=t.ref("boolean"==typeof e.hide&&e.hide),u=()=>({query:e.query,backfill:e.backfill,option:i}),a=()=>{if("function"==typeof e.hide){r.value!==e.hide(u())&&(r.value=e.hide(u()))}else if("function"==typeof e.disabled){n.value!==e.disabled(u())&&(n.value=e.disabled(u()))}};let l=[t.watch((()=>e.query),a,{immediate:!0,deep:!0}),t.watch((()=>[e.disabled,e.hide]),((e,t)=>{e[0]!==t[0]&&(n.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(r.value="boolean"==typeof e[1]&&e[1],e[1]),a()}))];return t.onBeforeUnmount((()=>(l.forEach((e=>e())),l=[]))),{insetDisabled:n,insetHide:r}}function c(e=!0){const i=t.ref(e);return{flag:i,updateFlag:()=>{i.value=!e,t.nextTick((()=>{i.value=e}))}}}function p(e){return e?.toString()??""}const v={field:{type:String,required:!0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},y={...v,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},multiple:{type:Boolean,default:void 0},options:{type:Array,default:()=>[]},getOptions:{type:Function}};function h(e){return e?.toString()??""}const g={...v,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};e.IS_COMPOSITION_VERSION=i,e.commonProps=v,e.defineCommonMethod=u,e.defineProvideValue=r,e.emptyToValue=l,e.getChained=s,e.getNode=function(e,...i){if(e)return"function"==typeof e?e(...i):"string"==typeof e?e:t.markRaw(e)},e.plainProps=y,e.provideKey=n,e.shallowDeep=o,e.treeProps=g,e.usePlain=function(e){const i=t.inject(n),r=d(e),u=e.backfill&&(e.fields?.length?e.fields.map((t=>e.backfill[t])).filter(Boolean):e.backfill[e.field]),a=t.ref(u||o(void 0!==e.defaultValue?r.value:e.multiple?[]:"")),s=t.ref([]),v=t.computed((()=>s.value.length?s.value:e.options)),y=()=>e.customGetQuery?e.customGetQuery(a.value,l,e):e.multiple&&e.fields?e.fields.reduce(((t,i,n)=>(t[i]=l(a.value?.[n],e.emptyValue),t)),{}):{[e.field]:l(o(t.toRaw(a.value)),e.emptyValue)},{flag:h,updateFlag:g}=c(),{flag:m,updateFlag:b}=c(),V={reset(){const{multiple:t}=e;g(),b(),a.value=e.resetToInitialValue&&r.value?.slice()||(t?[]:"")},updateWrapperQuery(){g(),i&&Object.entries(y()).forEach((([e,t])=>i.updateQueryValue(e,t)))},get validator(){return e.validator},getQuery:y};i?.register(V);const{insetDisabled:w,insetHide:Q}=f(e,V);!u&&e.defaultValue&&V.updateWrapperQuery();const S=[];function k(t){e.getOptions?.((e=>{const t=a.value;a.value=void 0,s.value=e||[],a.value=t}),e.query||{},{trigger:t,change:(e,t)=>{t&&(r.value=e),F(e)},search:(e,t)=>{t&&(r.value=e),A(e),i?.search()}})}function A(e){e!==a.value&&(a.value=e,V.updateWrapperQuery())}function F(e){A(e),i?.insetSearch()}return t.onBeforeUnmount((()=>S.forEach((e=>e())))),S.push(t.watch((()=>e.field),((e,t)=>{e!==t&&i?.removeUnreferencedField(t),V.updateWrapperQuery()}))),S.push(t.watch((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.query[t])).filter(Boolean):e.query[e.field]]),(([t,i],[n])=>{const r=e.backfillToValue(i,t,e.query);t.toString()===n.toString()&&p(r)!==p(a.value)&&h.value&&A(r)}))),S.push(t.watch((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.backfill?.[t])).filter(Boolean):e.backfill?.[e.field]]),(([t,i],[n])=>{const r=e.backfillToValue(i,t,e.backfill);t.toString()===n.toString()&&p(r)!==p(a.value)&&(b(),A(r))}))),S.push(t.watch((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t])).join(",")||""]),(([t,i,n],[r,u,l])=>{m.value&&n!==l&&(k("depend"),t===r&&i?.toString()===u?.toString()&&void 0!==a.value&&""!==a.value.toString()&&A(e.multiple?[]:""))}))),S.push(t.watch((()=>e.getOptions),k.bind(null,"initial"),{immediate:!0})),{wrapper:i,option:V,checked:a,getQuery:y,insetDisabled:w,insetHide:Q,finalOption:v,updateCheckedValue:A,change:F,reset:V.reset}},e.useTree=function(e){const i=t.inject(n),r=d(e),u=t.ref([]),a=t.ref([]),o=t.computed((()=>a.value.length?a.value:e.options)),p=()=>S.value||r.value?e.customGetQuery?e.customGetQuery(u.value,l,e):e.fields?.length?e.fields.reduce(((t,i,n)=>Object.assign(t,{[i]:l(u.value[n],e.emptyValue)})),{}):{[e.field]:l(e.emitPath?[...u.value]:u.value.slice(-1)[0],e.emptyValue)}:{},{flag:v,updateFlag:y}=c(),{flag:g,updateFlag:m}=c(),b={reset(){return y(),m(),u.value=e.resetToInitialValue&&r.value?.slice()||[],this},get validator(){return e.validator},updateWrapperQuery(){y(),i&&Object.entries(p()).forEach((([e,t])=>i.updateQueryValue(e,t)))},getQuery:p};i?.register(b);const{insetDisabled:V,insetHide:w}=f(e,b),Q=[];t.onBeforeUnmount((()=>Q.forEach((e=>e()))));const S=t.ref("function"!=typeof e.getOptions||!!e.fields?.length);function k(t){e.getOptions?.((e=>{a.value=e||[],S.value=!0}),e.query||{},{trigger:t,change:(e,t)=>{t&&(r.value=e),F(e)},search:(e,t)=>{t&&(r.value=e),A(e),i?.search()}})}function A(e){const t=Array.isArray(e)?e:O(e);t.join("")!==u.value.join("")&&(u.value=t,b.updateWrapperQuery())}function F(e){A(e||[]),i?.insetSearch()}function O(t){if(!t&&0!==t)return[];const{valueKey:i,childrenKey:n}=e;return s(o.value,(e=>e[i]===t)).map((e=>e[i]),n).filter(Boolean)}return t.watch(S,(t=>t&&function(){const{backfill:t,field:i,fields:n}=e;if(t)if(n){const e=n.reduce(((e,i)=>(t[i]&&e.push(t[i]),e)),[]);if(e.length)return u.value=e,void b.updateWrapperQuery()}else if(t[i])return u.value=O(t[i]),void b.updateWrapperQuery();r.value?.length&&(u.value="string"==typeof r.value?O(r.value):r.value.slice(),"string"==typeof r.value&&(r.value=u.value.slice()),b.updateWrapperQuery())}()),{immediate:!0}),Q.push(t.watch((()=>e.fields||[e.field]),((e,t)=>{e.toString()!==t.toString()&&i&&t.forEach((t=>e.includes(t)||i.removeUnreferencedField(t))),b.updateWrapperQuery()}))),Q.push(t.watch((()=>[e.fields?.toString()||e.field,e.fields?.map((t=>e.query[t])).filter(Boolean)||e.query[e.field]]),(([e,t],[i,n])=>{e===i&&h(t)!==h(n)&&v.value&&(u.value="string"==typeof t?O(t):t)}))),Q.push(t.watch((()=>e.fields?.length?e.fields.reduce(((t,i)=>(e.backfill?.[i]&&t.push(e.backfill[i]),t)),[]):e.backfill?.[e.field]),((e,t)=>{if(S.value&&h(e)!==h(t))if(m(),Array.isArray(e))A(e);else{if(!e&&0!==e)return void(u.value.length&&(u.value=[]));A(O(e))}}))),Q.push(t.watch((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t])).join(",")||""]),(([e,t,i],[n,r,a])=>{g.value&&i!==a&&(k("depend"),e===n&&t?.toString()===r?.toString()&&u.value.length&&A("string"==typeof u.value?"":[]))}))),Q.push(t.watch((()=>e.getOptions),k.bind(null,"initial"),{immediate:!0})),{wrapper:i,option:b,checked:u,getQuery:p,finalOption:o,insetDisabled:V,insetHide:w,change:F,reset:b.reset}},e.useWrapper=function(e,r){const u=[];t.onBeforeUnmount((()=>u.splice(0)));const a={};let l=!1,o=[];const s={realtime:t.ref(e.realtime),register(n){u.push(n);const r=()=>{l=!0,n.reset(),n.updateWrapperQuery();const i=u.indexOf(n);-1!==i&&u.splice(i,1),e.searchAtDatumChanged&&c(),l=!1,o.forEach((e=>{t.del(d.value,e),delete a[e]})),o=[]},s=t.getCurrentInstance();return s&&t.onBeforeUnmount(r,i?s.proxy:s),r},updateQueryValue(e,i){l&&o.push(e),t.set(d.value,e,i),a[e]=i},insetSearch(){e.realtime&&c()},search:c,removeUnreferencedField(e){let i=0;u.some((t=>(t.getQuery().hasOwnProperty(e)&&(i+=1),i))),i||(t.del(d.value,e),delete a[e])}};t.provide(n,s);const d=t.ref({...e.backfill}),f=()=>({...d.value,...e.backfill,...a});async function c(){const t=await p();t?e.toast?.(t):r?.search?.(f())}async function p(){return(await Promise.all(u.map((e=>e.validator?.(d.value))))).find((e=>e&&"string"==typeof e))}return{child:u,wrapperInstance:s,query:d,getQuery:f,search:c,reset:function(){u.forEach((e=>{e.reset(),e.updateWrapperQuery()})),r?.reset?.(f())},validate:p}},e.wrapperProps=a})); | ||
var VueDemi=function(e,t,n){if(e.install)return e;if(!t)return console.error("[vue-demi] no Vue instance found, please be sure to import `vue` before `vue-demi`."),e;if("2.7."===t.version.slice(0,4)){for(var i in t)e[i]=t[i];e.isVue2=!0,e.isVue3=!1,e.install=function(){},e.Vue=t,e.Vue2=t,e.version=t.version,e.warn=t.util.warn,e.hasInjectionContext=()=>!!e.getCurrentInstance(),e.createApp=function(e,n){var i,r={},u={config:t.config,use:t.use.bind(t),mixin:t.mixin.bind(t),component:t.component.bind(t),provide:function(e,t){return r[e]=t,this},directive:function(e,n){return n?(t.directive(e,n),u):t.directive(e)},mount:function(u,o){return i||((i=new t(Object.assign({propsData:n},e,{provide:Object.assign(r,e.provide)}))).$mount(u,o),i)},unmount:function(){i&&(i.$destroy(),i=void 0)}};return u}}else if("2."===t.version.slice(0,2))if(n){for(var i in n)e[i]=n[i];e.isVue2=!0,e.isVue3=!1,e.install=function(){},e.Vue=t,e.Vue2=t,e.version=t.version,e.hasInjectionContext=()=>!!e.getCurrentInstance()}else console.error("[vue-demi] no VueCompositionAPI instance found, please be sure to import `@vue/composition-api` before `vue-demi`.");else if("3."===t.version.slice(0,2)){for(var i in t)e[i]=t[i];e.isVue2=!1,e.isVue3=!0,e.install=function(){},e.Vue=t,e.Vue2=void 0,e.version=t.version,e.set=function(e,t,n){return Array.isArray(e)?(e.length=Math.max(e.length,t),e.splice(t,1,n),n):(e[t]=n,n)},e.del=function(e,t){Array.isArray(e)?e.splice(t,1):delete e[t]}}else console.error("[vue-demi] Vue version "+t.version+" is unsupported.");return e}(this.VueDemi=this.VueDemi||(void 0!==VueDemi?VueDemi:{}),this.Vue||("undefined"!=typeof Vue?Vue:void 0),this.VueCompositionAPI||("undefined"!=typeof VueCompositionAPI?VueCompositionAPI:void 0));!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue-demi")):"function"==typeof define&&define.amd?define(["exports","vue-demi"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CoreCondition={},e.VueDemi)}(this,(function(e,t){"use strict";const n="2.7"===t.version.slice(0,3),i="condition-wrapper";function r(e){return e}function u(e){return e}const o={realtime:{type:Boolean,default:void 0},searchAtDatumChanged:{type:Boolean,default:void 0},backfill:{type:Object},toast:{type:Function,default:void 0}};function l(e,t){return Array.isArray(e)?e.filter(Boolean).length?e:t:"number"==typeof e?e:e||t}function a(e,t){return s(e)&&s(t)||d(e,t)}function s(e){return void 0===e||""===e||null===e}function d(e,t){if(Object.is(e,t))return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.toString()===t.toString();if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const n=Reflect.ownKeys(e),i=Reflect.ownKeys(t);if(n.length!==i.length)return!1;for(let i=0;i<n.length;i++){if(!Reflect.has(t,n[i]))return!1;if(!d(e[n[i]],t[n[i]]))return!1}return!0}function f(e){return null==e||"object"!=typeof e&&"function"!=typeof e}function c(e,t){if(f(e))return e;if("function"==typeof e)return e.bind({});const n=new e.constructor;return Object.getOwnPropertyNames(e).forEach((i=>{n[i]=t?c(e[i]):e[i]})),n}function p(e,t,n="children"){for(const i of e){if(t(i))return[i];if(i[n]?.length){const e=p(i[n],t);if(e.length)return e.unshift(i),e}}return[]}function v(e){const n=t.ref();return t.computed({set(e){n.value=e},get:()=>void 0===n.value?void 0!==e.defaultValue?"function"==typeof e.defaultValue?e.defaultValue(e.query,e.backfill):e.defaultValue:void 0:n.value})}function y(e,n){const i=t.ref("boolean"==typeof e.disabled&&e.disabled),r=t.ref("boolean"==typeof e.hide&&e.hide),u=()=>({query:e.query,backfill:e.backfill,option:n}),o=()=>{if("function"==typeof e.hide){r.value!==e.hide(u())&&(r.value=e.hide(u()))}else if("function"==typeof e.disabled){i.value!==e.disabled(u())&&(i.value=e.disabled(u()))}};let l=[t.watch((()=>e.query),o,{immediate:!0,deep:!0}),t.watch((()=>[e.disabled,e.hide]),((e,t)=>{e[0]!==t[0]&&(i.value="boolean"==typeof e[0]&&e[0],e[0]),e[1]!==t[1]&&(r.value="boolean"==typeof e[1]&&e[1],e[1]),o()}))];return t.onBeforeUnmount((()=>(l.forEach((e=>e())),l=[]))),{insetDisabled:i,insetHide:r}}function h(e=!0){const n=t.ref(e);return{flag:n,updateFlag:()=>{n.value=!e,t.nextTick((()=>{n.value=e}))}}}const m={field:{type:String,required:!0},multiple:{type:Boolean,default:void 0},query:{type:Object,required:!0},backfill:{type:Object},disabled:{type:[Boolean,Function]},hide:{type:[Boolean,Function]},depend:{type:Boolean},dependFields:{type:[String,Array]},resetToInitialValue:{type:[Boolean]},emptyValue:{type:[String,Number,Boolean,null,void 0],default:void 0},validator:{type:[Function]},customGetQuery:{type:Function},defaultValue:{type:[String,Number,Array,Function]}},g={...m,fields:{type:[Array]},backfillToValue:{type:Function,default:e=>e},options:{type:Array,default:()=>[]},getOptions:{type:Function}};const b={...m,fields:{type:[Array]},valueKey:{type:String,required:!0},childrenKey:{type:String},emitPath:{type:[Boolean],default:!1},options:{type:Array,default:()=>[]},getOptions:{type:Function}};e.IS_COMPOSITION_VERSION=n,e.clone=c,e.commonProps=m,e.defineCommonMethod=u,e.defineProvideValue=r,e.emptyToValue=l,e.getChained=p,e.getNode=function(e,...n){return e?"function"==typeof e?e(...n):"string"==typeof e?e:t.markRaw(e):null},e.isEmptyValue=s,e.isEqual=d,e.isEqualExcludeEmptyValue=a,e.isPrimitive=f,e.plainProps=g,e.provideKey=i,e.treeProps=b,e.usePlain=function(e){const n=t.inject(i),r=v(e),u=e.backfill&&(e.fields?.length?e.fields.map((t=>e.backfill[t])).filter(Boolean):e.backfill[e.field]),o=t.shallowRef(u||void 0!==e.defaultValue?c(r.value):void 0),d=t.ref([]),f=t.computed((()=>d.value.length?d.value:e.options)),p=()=>{if(e.customGetQuery)return e.customGetQuery(o.value,l,e);const t=c(o.value);return e.multiple&&e.fields?e.fields.reduce(((n,i,r)=>(n[i]=l(t?.[r],e.emptyValue),n)),{}):{[e.field]:l(t,e.emptyValue)}},{flag:m,updateFlag:g}=h(),{flag:b,updateFlag:V}=h(),w={reset(){const{multiple:t}=e;g(),V(),o.value=e.resetToInitialValue&&r.value?.slice()||(t?[]:"")},updateWrapperQuery(){g(),n&&Object.entries(p()).forEach((([e,t])=>n.updateQueryValue(e,t)))},get validator(){return e.validator},getQuery:p};n?.register(w);const{insetDisabled:Q,insetHide:k}=y(e,w);!u&&e.defaultValue&&w.updateWrapperQuery();const O=[];function F(t){e.getOptions?.((e=>{const t=o.value;o.value=void 0,d.value=e||[],o.value=t}),e.query||{},{trigger:t,change:(e,t)=>{t&&(r.value=e),q(e)},search:(e,t)=>{t&&(r.value=e),S(e),n?.search()}})}function S(e){e!==o.value&&(o.value=e,w.updateWrapperQuery())}function q(e){S(e),n?.insetSearch()}return t.onBeforeUnmount((()=>O.forEach((e=>e())))),O.push(t.watch((()=>e.field),((e,t)=>{e!==t&&n?.removeUnreferencedField(t),w.updateWrapperQuery()}))),O.push(t.watch((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.query[t])).filter(Boolean):e.query[e.field]]),(([t,n],[i])=>{if(!m.value)return;const r=e.backfillToValue(n,t,e.query);t.toString()!==i.toString()||a(r,o.value)||S(r)}))),O.push(t.watch((()=>[e.fields||e.field,e.fields?e.fields.map((t=>e.backfill?.[t])).filter(Boolean):e.backfill?.[e.field]]),(([t,n],[i])=>{const r=e.backfillToValue(n,t,e.backfill);t.toString()!==i.toString()||a(r,o.value)||(V(),S(r))}))),O.push(t.watch((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t]))]),(([t,n,i],[r,u,l])=>{b.value&&i!==l&&(F("depend"),t===r&&a(n,u)&&(s(o.value)||S(e.multiple?[]:"")))}))),O.push(t.watch((()=>e.getOptions),F.bind(null,"initial"),{immediate:!0})),{wrapper:n,option:w,checked:o,getQuery:p,insetDisabled:Q,insetHide:k,finalOption:f,updateCheckedValue:S,change:q,reset:w.reset}},e.useTree=function(e){const n=t.inject(i),r=v(e),u=t.ref(),o=t.ref([]),d=t.computed((()=>o.value.length?o.value:e.options)),f=t.computed((()=>e.fields?e.fields.reduce(((t,n)=>(t[n]=e.emptyValue,t)),{}):{[e.field]:e.emptyValue})),m=()=>{if(!S.value&&!r.value)return{};let t=c(u.value);const{...n}=f.value;return e.customGetQuery?e.customGetQuery(t,l,e):t?(e.emitPath||(t=e.multiple?t.map((e=>e?.slice(-1)[0])):t.slice(-1)[0]),Array.isArray(t)?e.fields?t.forEach(((t,i)=>{n[e.fields[i]]=l(t,e.emptyValue)})):n[e.field]=t:n[e.fields?.[0]||e.field]=l(t,e.emptyValue),n):n},{flag:g,updateFlag:b}=h(),{flag:V,updateFlag:w}=h(),Q={reset(){return b(),w(),u.value=e.resetToInitialValue&&r.value?.slice()||[],this},get validator(){return e.validator},updateWrapperQuery(){b(),n&&Object.entries(m()).forEach((([e,t])=>n.updateQueryValue(e,t)))},getQuery:m};n?.register(Q);const{insetDisabled:k,insetHide:O}=y(e,Q),F=[];t.onBeforeUnmount((()=>F.forEach((e=>e()))));const S=t.ref("function"!=typeof e.getOptions||!!e.fields?.length);function q(t){e.getOptions?.((e=>{o.value=e||[],S.value=!0}),e.query||{},{trigger:t,change:(e,t)=>{t&&(r.value=e),B(e)},search:(e,t)=>{t&&(r.value=e),A(e),n?.search()}})}function A(e){a(u.value,e)||(u.value=e,Q.updateWrapperQuery())}function B(e){A(e),n?.insetSearch()}function E(t){if(s(t))return[];const{valueKey:n,childrenKey:i}=e;return p(d.value,(e=>e[n]===t)).map((e=>e[n]),i).filter(Boolean)}return t.watch(S,(t=>t&&function(){const{backfill:t,field:n,fields:i}=e;if(t)if(i){const e=i.reduce(((e,n)=>(t[n]&&e.push(t[n]),e)),[]);if(e.length)return u.value=e,void Q.updateWrapperQuery()}else if(t[n])return u.value=E(t[n]),void Q.updateWrapperQuery();r.value?.length&&(u.value="string"==typeof r.value?E(r.value):r.value.slice(),"string"==typeof r.value&&(r.value=c(u.value)),Q.updateWrapperQuery())}()),{immediate:!0}),F.push(t.watch((()=>e.fields||[e.field]),((e,t)=>{e.toString()!==t.toString()&&n&&t.forEach((t=>e.includes(t)||n.removeUnreferencedField(t))),Q.updateWrapperQuery()}))),F.push(t.watch((()=>[e.fields?.toString()||e.field,e.fields?.map((t=>e.query[t])).filter(Boolean)||e.query[e.field]]),(([e,t],[n,i])=>{g.value&&e===n&&a(t,i)}))),F.push(t.watch((()=>e.fields?.length?e.fields.reduce(((t,n)=>(e.backfill?.[n]&&t.push(e.backfill[n]),t)),[]):e.backfill?.[e.field]),((e,t)=>{S.value&&(a(e,t)||w())}))),F.push(t.watch((()=>[e.depend,e.dependFields,e.dependFields&&[].concat(e.dependFields).map((t=>e.query?.[t]))]),(([e,t,n],[i,r,u])=>{V.value&&(a(n,u)||(q("depend"),e===i&&t?.toString()===r?.toString()&&A(void 0)))}))),F.push(t.watch((()=>e.getOptions),q.bind(null,"initial"),{immediate:!0})),{wrapper:n,option:Q,checked:u,getQuery:m,finalOption:d,insetDisabled:k,insetHide:O,change:B,reset:Q.reset}},e.useWrapper=function(e,r){const u=[];t.onBeforeUnmount((()=>u.splice(0)));const o={};let l=!1,a=[];const s={realtime:t.ref(e.realtime),register(i){u.push(i);const r=()=>{l=!0,i.reset(),i.updateWrapperQuery();const n=u.indexOf(i);-1!==n&&u.splice(n,1),e.searchAtDatumChanged&&c(),l=!1,a.forEach((e=>{t.del(d.value,e),delete o[e]})),a=[]},s=t.getCurrentInstance();return s&&t.onBeforeUnmount(r,n?s.proxy:s),r},updateQueryValue(e,n){l&&a.push(e),t.set(d.value,e,n),o[e]=n},insetSearch(){e.realtime&&c()},search:c,removeUnreferencedField(e){let n=0;u.some((t=>(t.getQuery().hasOwnProperty(e)&&(n+=1),n))),n||(t.del(d.value,e),delete o[e])}};t.provide(i,s);const d=t.ref({...e.backfill}),f=()=>({...d.value,...e.backfill,...o});async function c(){const t=await p();t?e.toast?.(t):r?.search?.(f())}async function p(){return(await Promise.all(u.map((e=>e.validator?.(d.value))))).find((e=>e&&"string"==typeof e))}return{child:u,wrapperInstance:s,query:d,getQuery:f,search:c,reset:function(){u.forEach((e=>{e.reset(),e.updateWrapperQuery()})),r?.reset?.(f())},validate:p}},e.wrapperProps=o})); | ||
//# sourceMappingURL=index.umd.min.js.map |
@@ -1,2 +0,2 @@ | ||
import * as _vue_composition_api from '@vue/composition-api'; | ||
import * as vue_demi from 'vue-demi'; | ||
import { Ref, PropType, ExtractPropTypes, VNode, UnwrapRef } from 'vue-demi'; | ||
@@ -88,3 +88,3 @@ | ||
wrapperInstance: { | ||
realtime: _vue_composition_api.Ref<boolean>; | ||
realtime: vue_demi.Ref<boolean | undefined>; | ||
register(compOption: CommonMethod): () => void; | ||
@@ -96,5 +96,3 @@ updateQueryValue(field: string, value: any): void; | ||
}; | ||
query: _vue_composition_api.Ref<{ | ||
[x: string]: string; | ||
}>; | ||
query: vue_demi.Ref<Record<string, string>>; | ||
getQuery: () => { | ||
@@ -114,8 +112,13 @@ [x: string]: any; | ||
declare function emptyToValue<T extends unknown>(val: any, defaultVal: T): any; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
declare function isEqualExcludeEmptyValue(x: any, y: any): boolean; | ||
/** 判断是否是空值(null, undefined, '') */ | ||
declare function isEmptyValue(val: any): boolean; | ||
/** 判断两个值是否一致 */ | ||
declare function isEqual(x: any, y: any): boolean; | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
declare function isPrimitive(value: any): boolean; | ||
/** 拷贝 */ | ||
declare function clone<T>(obj: T, deep?: boolean): T; | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
declare function shallowDeep<T extends unknown>(val: T): T; | ||
/** | ||
* 获取指定层级的父级(包括自身) | ||
@@ -130,3 +133,3 @@ * @param {Record<string, any>[]} data 数据源 | ||
*/ | ||
declare function getNode(node: string | ((...args: any[]) => VNode) | VNode | undefined | null, ...args: any[]): string | VNode | undefined; | ||
declare function getNode(node: string | ((...args: any[]) => VNode) | VNode | undefined | null, ...args: any[]): {}; | ||
@@ -153,3 +156,3 @@ /** 隐藏元素 */ | ||
/** 条件值可能的类型 */ | ||
type ValueType$1 = number | string | string[]; | ||
type ValueType$2 = number | string | string[]; | ||
/** 改变当前条件值触发方式 */ | ||
@@ -184,2 +187,7 @@ interface TriggerOption { | ||
}; | ||
/** 是否多选 */ | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -229,3 +237,3 @@ readonly query: { | ||
readonly defaultValue: { | ||
readonly type: PropType<ValueType$1 | ((query: Record<string, any>, backfill?: Record<string, any>) => ValueType$1)>; | ||
readonly type: PropType<ValueType$2 | ((query: Record<string, any>, backfill?: Record<string, any>) => ValueType$2)>; | ||
}; | ||
@@ -245,7 +253,2 @@ }; | ||
}; | ||
/** 是否多选 */ | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
/** 数据源 */ | ||
@@ -264,2 +267,6 @@ readonly options: { | ||
}; | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
readonly query: { | ||
@@ -298,3 +305,3 @@ readonly type: PropType<Record<string, any>>; | ||
readonly defaultValue: { | ||
readonly type: PropType<ValueType$1 | ((query: Record<string, any>, backfill?: Record<string, any> | undefined) => ValueType$1)>; | ||
readonly type: PropType<ValueType$2 | ((query: Record<string, any>, backfill?: Record<string, any> | undefined) => ValueType$2)>; | ||
}; | ||
@@ -305,2 +312,3 @@ }; | ||
type PlainProps = ExtractPropTypes<typeof plainProps>; | ||
type ValueType$1 = string | number | boolean | null | undefined | Record<string, any>; | ||
/** 封装扁平组件必备的信息 */ | ||
@@ -315,9 +323,9 @@ declare function usePlain(props: PlainProps): { | ||
}; | ||
checked: _vue_composition_api.Ref<string | string[]>; | ||
checked: vue_demi.ShallowRef<ValueType$1 | ValueType$1[]>; | ||
getQuery: () => Record<string, any>; | ||
insetDisabled: _vue_composition_api.Ref<boolean>; | ||
insetHide: _vue_composition_api.Ref<boolean>; | ||
finalOption: _vue_composition_api.ComputedRef<Record<string, any>[]>; | ||
updateCheckedValue: (value: string | string[]) => void; | ||
change: (value: string | string[]) => void; | ||
insetDisabled: vue_demi.Ref<boolean>; | ||
insetHide: vue_demi.Ref<boolean>; | ||
finalOption: vue_demi.ComputedRef<Record<string, any>[]>; | ||
updateCheckedValue: (value: ValueType$1 | ValueType$1[]) => void; | ||
change: (value: ValueType$1 | ValueType$1[]) => void; | ||
reset: () => void; | ||
@@ -341,3 +349,3 @@ }; | ||
}; | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
readonly emitPath: { | ||
@@ -360,2 +368,6 @@ readonly type: PropType<boolean>; | ||
}; | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
readonly query: { | ||
@@ -394,3 +406,3 @@ readonly type: PropType<Record<string, any>>; | ||
readonly defaultValue: { | ||
readonly type: PropType<ValueType$1 | ((query: Record<string, any>, backfill?: Record<string, any> | undefined) => ValueType$1)>; | ||
readonly type: PropType<ValueType$2 | ((query: Record<string, any>, backfill?: Record<string, any> | undefined) => ValueType$2)>; | ||
}; | ||
@@ -400,5 +412,8 @@ }; | ||
/** 选中值类型 */ | ||
type ValueType = string | number | null | undefined; | ||
type ValueType = string | number | boolean | Record<string, any>; | ||
/** 外部需传递的 props */ | ||
type TreeProps = ExtractPropTypes<typeof treeProps>; | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
/** 封装 tree 组件必备的信息 */ | ||
@@ -413,8 +428,8 @@ declare function useTree(props: TreeProps): { | ||
}; | ||
checked: _vue_composition_api.Ref<ValueType[]>; | ||
checked: vue_demi.Ref<ValueType[] | ValueType[][] | undefined>; | ||
getQuery: () => Record<string, any>; | ||
finalOption: _vue_composition_api.ComputedRef<Record<string, any>[]>; | ||
insetDisabled: _vue_composition_api.Ref<boolean>; | ||
insetHide: _vue_composition_api.Ref<boolean>; | ||
change: (values: ValueType[] | ValueType) => void; | ||
finalOption: vue_demi.ComputedRef<Record<string, any>[]>; | ||
insetDisabled: vue_demi.Ref<boolean>; | ||
insetHide: vue_demi.Ref<boolean>; | ||
change: (values?: ValueType[] | ValueType[][]) => void; | ||
reset: () => CommonMethod; | ||
@@ -446,2 +461,2 @@ }; | ||
export { CommonMethod, CoreCondition, GetOptions, GetQuery, HideOption, IS_COMPOSITION_VERSION, PlainProps, ProvideValue, TreeProps, TriggerOption, ValueType$1 as ValueType, WrapperOption, WrapperProps, commonProps, defineCommonMethod, defineProvideValue, emptyToValue, getChained, getNode, plainProps, provideKey, shallowDeep, treeProps, usePlain, useTree, useWrapper, wrapperProps }; | ||
export { CommonMethod, CoreCondition, GetOptions, GetQuery, HideOption, IS_COMPOSITION_VERSION, PlainProps, ProvideValue, TreeProps, TriggerOption, ValueType$2 as ValueType, WrapperOption, WrapperProps, clone, commonProps, defineCommonMethod, defineProvideValue, emptyToValue, getChained, getNode, isEmptyValue, isEqual, isEqualExcludeEmptyValue, isPrimitive, plainProps, provideKey, treeProps, usePlain, useTree, useWrapper, wrapperProps }; |
@@ -5,3 +5,3 @@ import { ExtractPropTypes } from 'vue-demi'; | ||
/** 获取条件的初始值 */ | ||
export declare function useInitialValue<T extends ExtractPropTypes<Readonly<typeof commonProps>>>(props: T): import("@vue/composition-api").WritableComputedRef<any>; | ||
export declare function useInitialValue<T extends ExtractPropTypes<Readonly<typeof commonProps>>>(props: T): import("vue-demi").WritableComputedRef<any>; | ||
/** | ||
@@ -11,4 +11,4 @@ * 获取当前组件的显示和隐藏状态 | ||
export declare function useDisplay<T extends ExtractPropTypes<Readonly<typeof commonProps>>>(props: T, option: CommonMethod): { | ||
insetDisabled: import("@vue/composition-api").Ref<boolean>; | ||
insetHide: import("@vue/composition-api").Ref<boolean>; | ||
insetDisabled: import("vue-demi").Ref<boolean>; | ||
insetHide: import("vue-demi").Ref<boolean>; | ||
}; | ||
@@ -20,4 +20,4 @@ /** | ||
export declare function useDisableInCurrentCycle(initialValue?: boolean): { | ||
flag: import("@vue/composition-api").Ref<boolean>; | ||
flag: import("vue-demi").Ref<boolean>; | ||
updateFlag: () => void; | ||
}; |
@@ -6,2 +6,3 @@ import { ExtractPropTypes } from 'vue-demi'; | ||
export type PlainProps = ExtractPropTypes<typeof plainProps>; | ||
type ValueType = string | number | boolean | null | undefined | Record<string, any>; | ||
/** 封装扁平组件必备的信息 */ | ||
@@ -16,10 +17,11 @@ export declare function usePlain(props: PlainProps): { | ||
}; | ||
checked: import("@vue/composition-api").Ref<string | string[]>; | ||
checked: import("vue-demi").ShallowRef<ValueType | ValueType[]>; | ||
getQuery: () => Record<string, any>; | ||
insetDisabled: import("@vue/composition-api").Ref<boolean>; | ||
insetHide: import("@vue/composition-api").Ref<boolean>; | ||
finalOption: import("@vue/composition-api").ComputedRef<Record<string, any>[]>; | ||
updateCheckedValue: (value: string | string[]) => void; | ||
change: (value: string | string[]) => void; | ||
insetDisabled: import("vue-demi").Ref<boolean>; | ||
insetHide: import("vue-demi").Ref<boolean>; | ||
finalOption: import("vue-demi").ComputedRef<Record<string, any>[]>; | ||
updateCheckedValue: (value: ValueType | ValueType[]) => void; | ||
change: (value: ValueType | ValueType[]) => void; | ||
reset: () => void; | ||
}; | ||
export {}; |
@@ -14,7 +14,2 @@ import { PropType } from 'vue-demi'; | ||
}; | ||
/** 是否多选 */ | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
/** 数据源 */ | ||
@@ -33,2 +28,6 @@ readonly options: { | ||
}; | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
readonly query: { | ||
@@ -35,0 +34,0 @@ readonly type: PropType<Record<string, any>>; |
@@ -54,2 +54,7 @@ import { type PropType } from 'vue-demi'; | ||
}; | ||
/** 是否多选 */ | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -56,0 +61,0 @@ readonly query: { |
@@ -5,5 +5,8 @@ import { ExtractPropTypes } from 'vue-demi'; | ||
/** 选中值类型 */ | ||
type ValueType = string | number | null | undefined; | ||
type ValueType = string | number | boolean | Record<string, any>; | ||
/** 外部需传递的 props */ | ||
export type TreeProps = ExtractPropTypes<typeof treeProps>; | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
/** 封装 tree 组件必备的信息 */ | ||
@@ -18,10 +21,10 @@ export declare function useTree(props: TreeProps): { | ||
}; | ||
checked: import("@vue/composition-api").Ref<ValueType[]>; | ||
checked: import("vue-demi").Ref<ValueType[] | ValueType[][] | undefined>; | ||
getQuery: () => Record<string, any>; | ||
finalOption: import("@vue/composition-api").ComputedRef<Record<string, any>[]>; | ||
insetDisabled: import("@vue/composition-api").Ref<boolean>; | ||
insetHide: import("@vue/composition-api").Ref<boolean>; | ||
change: (values: ValueType[] | ValueType) => void; | ||
finalOption: import("vue-demi").ComputedRef<Record<string, any>[]>; | ||
insetDisabled: import("vue-demi").Ref<boolean>; | ||
insetHide: import("vue-demi").Ref<boolean>; | ||
change: (values?: ValueType[] | ValueType[][]) => void; | ||
reset: () => import("../constant").CommonMethod; | ||
}; | ||
export {}; |
@@ -18,3 +18,3 @@ import { PropType } from 'vue-demi'; | ||
}; | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
readonly emitPath: { | ||
@@ -37,2 +37,6 @@ readonly type: PropType<boolean>; | ||
}; | ||
readonly multiple: { | ||
readonly type: PropType<boolean>; | ||
readonly default: undefined; | ||
}; | ||
readonly query: { | ||
@@ -39,0 +43,0 @@ readonly type: PropType<Record<string, any>>; |
@@ -17,3 +17,3 @@ import { ExtractPropTypes } from 'vue-demi'; | ||
wrapperInstance: { | ||
realtime: import("@vue/composition-api").Ref<boolean>; | ||
realtime: import("vue-demi").Ref<boolean | undefined>; | ||
register(compOption: CommonMethod): () => void; | ||
@@ -25,5 +25,3 @@ updateQueryValue(field: string, value: any): void; | ||
}; | ||
query: import("@vue/composition-api").Ref<{ | ||
[x: string]: string; | ||
}>; | ||
query: import("vue-demi").Ref<Record<string, string>>; | ||
getQuery: () => { | ||
@@ -30,0 +28,0 @@ [x: string]: any; |
@@ -8,8 +8,13 @@ import { VNode } from 'vue-demi'; | ||
export declare function emptyToValue<T extends unknown>(val: any, defaultVal: T): any; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
export declare function isEqualExcludeEmptyValue(x: any, y: any): boolean; | ||
/** 判断是否是空值(null, undefined, '') */ | ||
export declare function isEmptyValue(val: any): boolean; | ||
/** 判断两个值是否一致 */ | ||
export declare function isEqual(x: any, y: any): boolean; | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
export declare function isPrimitive(value: any): boolean; | ||
/** 拷贝 */ | ||
export declare function clone<T>(obj: T, deep?: boolean): T; | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
export declare function shallowDeep<T extends unknown>(val: T): T; | ||
/** | ||
* 获取指定层级的父级(包括自身) | ||
@@ -24,2 +29,2 @@ * @param {Record<string, any>[]} data 数据源 | ||
*/ | ||
export declare function getNode(node: string | ((...args: any[]) => VNode) | VNode | undefined | null, ...args: any[]): string | VNode | undefined; | ||
export declare function getNode(node: string | ((...args: any[]) => VNode) | VNode | undefined | null, ...args: any[]): {}; |
{ | ||
"name": "@xiaohaih/condition-core", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -11,4 +11,5 @@ import { | ||
toRaw, | ||
shallowRef, | ||
} from 'vue-demi'; | ||
import { emptyToValue, shallowDeep } from '../../utils/index'; | ||
import { clone, emptyToValue, isEqualExcludeEmptyValue, isEmptyValue, getChained } from '../../utils/index'; | ||
import { CommonMethod, defineCommonMethod, provideKey, ProvideValue } from '../constant'; | ||
@@ -21,6 +22,3 @@ import { useDisplay, useDisableInCurrentCycle, useInitialValue } from '../assist'; | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str(val?: string | number | undefined | null | any[]) { | ||
return val?.toString() ?? ''; | ||
} | ||
type ValueType = string | number | boolean | null | undefined | Record<string, any>; | ||
@@ -41,6 +39,4 @@ /** 封装扁平组件必备的信息 */ | ||
/** 当前选中值 */ | ||
const checked = ref<string | string[]>( | ||
initialBackfillValue || | ||
// 防止数组引用导致默认值发生改变 | ||
shallowDeep(props.defaultValue !== undefined ? initialValue.value : props.multiple ? [] : ''), | ||
const checked = shallowRef<ValueType | ValueType[]>( | ||
initialBackfillValue || props.defaultValue !== undefined ? clone(initialValue.value) : undefined, | ||
); | ||
@@ -53,8 +49,9 @@ /** 远程获取的数据源 */ | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
const _checked = clone(checked.value); | ||
return props.multiple && props.fields | ||
? props.fields.reduce( | ||
(p, k, i) => ((p[k] = emptyToValue(checked.value?.[i], props.emptyValue)), p), | ||
(p, k, i) => ((p[k] = emptyToValue((_checked as ValueType[])?.[i], props.emptyValue)), p), | ||
{} as Record<string, any>, | ||
) | ||
: { [props.field]: emptyToValue(shallowDeep(toRaw(checked.value)), props.emptyValue) }; | ||
: { [props.field]: emptyToValue(_checked, props.emptyValue) }; | ||
}; | ||
@@ -113,7 +110,6 @@ // 防止触发搜索时, query 产生变化内部重复赋值 | ||
([_field, val], [__field]) => { | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
// 仅在值发生变化时同步 忽视空值不一致的问题 | ||
if (_field.toString() !== __field.toString() || emptyValue2Str(_val) === emptyValue2Str(checked.value)) | ||
return; | ||
if (!realtimeFlag.value) return; | ||
const _val = props.backfillToValue(val, _field, props.query); | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateCheckedValue(_val); | ||
@@ -136,4 +132,3 @@ }, | ||
const _val = props.backfillToValue(val, _field, props.backfill); | ||
if (_field.toString() !== __field.toString() || emptyValue2Str(_val) === emptyValue2Str(checked.value)) | ||
return; | ||
if (_field.toString() !== __field.toString() || isEqualExcludeEmptyValue(_val, checked.value)) return; | ||
updateBackfillFlag(); | ||
@@ -151,8 +146,3 @@ updateCheckedValue(_val); | ||
props.dependFields, | ||
(props.dependFields && | ||
([] as string[]) | ||
.concat(props.dependFields) | ||
.map((k) => props.query?.[k]) | ||
.join(',')) || | ||
'', | ||
props.dependFields && ([] as string[]).concat(props.dependFields).map((k) => props.query?.[k]), | ||
] as const, | ||
@@ -164,4 +154,4 @@ ([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (checked.value === undefined || checked.value.toString() === '') return; | ||
if (_depend !== __depend || !isEqualExcludeEmptyValue(_dependFields, __dependFields)) return; | ||
if (isEmptyValue(checked.value)) return; | ||
updateCheckedValue(props.multiple ? [] : ''); | ||
@@ -200,5 +190,5 @@ }, | ||
* 更新选中值(父级也同步更改) | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
function updateCheckedValue(value: string | string[]) { | ||
function updateCheckedValue(value: ValueType | ValueType[]) { | ||
if (value === checked.value) return; | ||
@@ -210,5 +200,5 @@ checked.value = value; | ||
* 更新选中值并触发内部搜索事件 | ||
* @param {string | string[]} value 待更改的值 | ||
* @param {*} value 待更改的值 | ||
*/ | ||
function change(value: string | string[]) { | ||
function change(value: ValueType | ValueType[]) { | ||
updateCheckedValue(value); | ||
@@ -215,0 +205,0 @@ wrapper?.insetSearch(); |
@@ -16,4 +16,2 @@ import { PropType } from 'vue-demi'; | ||
}, | ||
/** 是否多选 */ | ||
multiple: { type: Boolean as PropType<boolean>, default: undefined }, | ||
/** 数据源 */ | ||
@@ -20,0 +18,0 @@ options: { type: Array as PropType<Record<string, any>[]>, default: () => [] }, |
@@ -64,2 +64,4 @@ import { type PropType } from 'vue-demi'; | ||
field: { type: String as PropType<string>, required: true }, | ||
/** 是否多选 */ | ||
multiple: { type: Boolean as PropType<boolean>, default: undefined }, | ||
/** 当前条件对象 - 实时变化 */ | ||
@@ -66,0 +68,0 @@ query: { type: Object as PropType<Record<string, any>>, required: true }, |
import { computed, ExtractPropTypes, inject, onBeforeUnmount, PropType, ref, watch, nextTick } from 'vue-demi'; | ||
import { emptyToValue, getChained } from '../../utils/index'; | ||
import { clone, emptyToValue, isEqualExcludeEmptyValue, isEmptyValue, getChained } from '../../utils/index'; | ||
import { defineCommonMethod, provideKey, ProvideValue } from '../constant'; | ||
@@ -8,10 +8,9 @@ import { useDisplay, useDisableInCurrentCycle, useInitialValue } from '../assist'; | ||
/** 选中值类型 */ | ||
type ValueType = string | number | null | undefined; | ||
type ValueType = string | number | boolean | Record<string, any>; | ||
/** 外部需传递的 props */ | ||
export type TreeProps = ExtractPropTypes<typeof treeProps>; | ||
/** 空值转字符串(去除空值不一致导致 formItem.rules 校验) */ | ||
function emptyValue2Str(val?: string | number | undefined | null | any[]) { | ||
return val?.toString() ?? ''; | ||
} | ||
/** | ||
* @file 目前该组件尚未被使用 | ||
*/ | ||
@@ -25,3 +24,3 @@ /** 封装 tree 组件必备的信息 */ | ||
/** 当前选中值 */ | ||
const checked = ref<ValueType[]>([]); | ||
const checked = ref<ValueType[] | ValueType[][]>(); | ||
/** 远程获取的数据源 */ | ||
@@ -31,2 +30,8 @@ const remoteOption = ref<Record<string, any>[]>([]); | ||
const finalOption = computed(() => (remoteOption.value.length ? remoteOption.value : props.options)); | ||
/** 字段集合 */ | ||
const fieldObj = computed(() => | ||
props.fields | ||
? props.fields.reduce((p, v) => ((p[v] = props.emptyValue), p), {} as Record<string, any>) | ||
: { [props.field]: props.emptyValue }, | ||
); | ||
/** 获取当前条件所拥有的值 */ | ||
@@ -36,14 +41,22 @@ const getQuery = () => { | ||
if (!sourceIsInit.value && !initialValue.value) return {}; | ||
if (props.customGetQuery) return props.customGetQuery(checked.value, emptyToValue, props); | ||
return props.fields?.length | ||
? props.fields.reduce( | ||
(p, v, i) => Object.assign(p, { [v]: emptyToValue(checked.value[i], props.emptyValue) }), | ||
{}, | ||
) | ||
: { | ||
[props.field]: emptyToValue( | ||
props.emitPath ? [...checked.value] : checked.value.slice(-1)[0], | ||
props.emptyValue, | ||
), | ||
}; | ||
let _checked: ValueType | ValueType[] | ValueType[][] | undefined = clone(checked.value); | ||
const { ...result } = fieldObj.value; | ||
if (props.customGetQuery) return props.customGetQuery(_checked, emptyToValue, props); | ||
if (!_checked) return result; | ||
if (!props.emitPath) { | ||
_checked = props.multiple | ||
? (_checked as ValueType[]).map((v) => (v as ValueType[])?.slice(-1)[0]) | ||
: (_checked as ValueType[]).slice(-1)[0]; | ||
} | ||
if (Array.isArray(_checked)) { | ||
props.fields | ||
? _checked.forEach((o, i) => { | ||
result[props.fields![i]] = emptyToValue(o, props.emptyValue); | ||
}) | ||
: (result[props.field] = _checked); | ||
} else { | ||
result[props.fields?.[0] || props.field] = emptyToValue(_checked, props.emptyValue); | ||
} | ||
return result; | ||
}; | ||
@@ -106,2 +119,4 @@ // 防止触发搜索时, query 产生变化内部重复赋值 | ||
if (initialValue.value?.length) { | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// 此种情况比较多 可封装成一个函数处理 | ||
checked.value = | ||
@@ -111,3 +126,3 @@ typeof initialValue.value === 'string' | ||
: initialValue.value.slice(); | ||
typeof initialValue.value === 'string' && (initialValue.value = checked.value.slice()); | ||
typeof initialValue.value === 'string' && (initialValue.value = clone(checked.value)); | ||
option.updateWrapperQuery(); | ||
@@ -125,3 +140,2 @@ } | ||
oldVal.forEach((o) => val.includes(o) || wrapper.removeUnreferencedField(o)); | ||
option.updateWrapperQuery(); | ||
@@ -141,5 +155,6 @@ }, | ||
// 仅在值发生变化时同步 | ||
if (_field !== __field || emptyValue2Str(val) === emptyValue2Str(oldVal)) return; | ||
if (!realtimeFlag.value) return; | ||
checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
if (_field !== __field || isEqualExcludeEmptyValue(val, oldVal)) return; | ||
// TODO 需考虑 emitPath 和多选的情况 | ||
// checked.value = typeof val === 'string' ? insideGetChained(val) : val; | ||
}, | ||
@@ -160,13 +175,7 @@ ), | ||
if (!sourceIsInit.value) return; | ||
if (emptyValue2Str(value) === emptyValue2Str(oldVal)) return; | ||
if (isEqualExcludeEmptyValue(value, oldVal)) return; | ||
updateBackfillFlag(); | ||
if (Array.isArray(value)) { | ||
updateCheckedValue(value); | ||
} else { | ||
if (!value && value !== 0) { | ||
checked.value.length && (checked.value = []); | ||
return; | ||
} | ||
updateCheckedValue(insideGetChained(value)); | ||
} | ||
// TODO 判断最后一个值是否与选中项最后一个一致(多选需遍历) | ||
// 如果一致则不更新, 不一致, 则通过 insideGetChained 获取到树形再赋值 | ||
// updateCheckedValue(value); | ||
}, | ||
@@ -182,17 +191,11 @@ ), | ||
props.dependFields, | ||
(props.dependFields && | ||
([] as string[]) | ||
.concat(props.dependFields) | ||
.map((k) => props.query?.[k]) | ||
.join(',')) || | ||
'', | ||
props.dependFields && ([] as string[]).concat(props.dependFields).map((k) => props.query?.[k]), | ||
] as const, | ||
([_depend, _dependFields, val], [__depend, __dependFields, oldVal]) => { | ||
if (!backfillFlag.value) return; | ||
if (val === oldVal) return; | ||
if (isEqualExcludeEmptyValue(val, oldVal)) return; | ||
getOption('depend'); | ||
// 更新依赖条件时不做改动 | ||
if (_depend !== __depend || _dependFields?.toString() !== __dependFields?.toString()) return; | ||
if (!checked.value.length) return; | ||
updateCheckedValue(typeof checked.value === 'string' ? '' : []); | ||
updateCheckedValue(undefined); | ||
}, | ||
@@ -229,6 +232,5 @@ ), | ||
*/ | ||
function updateCheckedValue(values: ValueType[] | ValueType) { | ||
const _checked = Array.isArray(values) ? values : insideGetChained(values); | ||
if (_checked.join('') === checked.value.join('')) return; | ||
checked.value = _checked; | ||
function updateCheckedValue(values?: ValueType[] | ValueType[][]) { | ||
if (isEqualExcludeEmptyValue(checked.value, values)) return; | ||
checked.value = values; | ||
option.updateWrapperQuery(); | ||
@@ -240,4 +242,4 @@ } | ||
*/ | ||
function change(values: ValueType[] | ValueType) { | ||
updateCheckedValue(values || []); | ||
function change(values?: ValueType[] | ValueType[][]) { | ||
updateCheckedValue(values); | ||
wrapper?.insetSearch(); | ||
@@ -249,3 +251,3 @@ } | ||
function insideGetChained(val: ValueType) { | ||
if (!val && val !== 0) return []; | ||
if (isEmptyValue(val)) return []; | ||
const { valueKey, childrenKey } = props; | ||
@@ -252,0 +254,0 @@ return getChained(finalOption.value, (item) => item[valueKey] === val) |
@@ -13,3 +13,3 @@ import { PropType } from 'vue-demi'; | ||
childrenKey: { type: String as PropType<string> }, | ||
/** 是否返回选中项中所有的值(数组形式), 否只返回最后一项(基础类型) */ | ||
/** 是否返回全路径 */ | ||
emitPath: { type: [Boolean] as PropType<boolean>, default: false }, | ||
@@ -16,0 +16,0 @@ /** 下拉选项的数据源 */ |
@@ -13,10 +13,50 @@ import { markRaw, VNode } from 'vue-demi'; | ||
/** | ||
* 对值进行浅拷贝 | ||
* @param {*} val | ||
*/ | ||
export function shallowDeep<T extends unknown>(val: T): T { | ||
return Array.isArray(val) ? ([...val] as T) : val; | ||
/** 判断两个值是否相等, 排除掉空值 */ | ||
export function isEqualExcludeEmptyValue(x: any, y: any) { | ||
return (isEmptyValue(x) && isEmptyValue(y)) || isEqual(x, y); | ||
} | ||
/** 判断是否是空值(null, undefined, '') */ | ||
export function isEmptyValue(val: any) { | ||
return val === undefined || val === '' || val === null; | ||
} | ||
/** 判断两个值是否一致 */ | ||
export function isEqual(x: any, y: any): boolean { | ||
if (Object.is(x, y)) return true; | ||
if (x instanceof Date && y instanceof Date) { | ||
return x.getTime() === y.getTime(); | ||
} | ||
if (x instanceof RegExp && y instanceof RegExp) { | ||
return x.toString() === y.toString(); | ||
} | ||
if (typeof x !== 'object' || x === null || typeof y !== 'object' || y === null) { | ||
return false; | ||
} | ||
const keysX = Reflect.ownKeys(x as unknown as object) as (keyof typeof x)[]; | ||
const keysY = Reflect.ownKeys(y as unknown as object); | ||
if (keysX.length !== keysY.length) return false; | ||
for (let i = 0; i < keysX.length; i++) { | ||
if (!Reflect.has(y as unknown as object, keysX[i])) return false; | ||
if (!isEqual(x[keysX[i]], y[keysX[i]])) return false; | ||
} | ||
return true; | ||
} | ||
/** 判断是否是原始类型(number , string , boolean , symbol, bigint, undefined, null) */ | ||
export function isPrimitive(value: any) { | ||
return value === undefined || value === null || (typeof value !== 'object' && typeof value !== 'function'); | ||
} | ||
/** 拷贝 */ | ||
export function clone<T>(obj: T, deep?: boolean): T { | ||
if (isPrimitive(obj)) return obj; | ||
if (typeof obj === 'function') return obj.bind({}); | ||
const newObj = new ((obj as object).constructor as { new (): T })(); | ||
Object.getOwnPropertyNames(obj).forEach((prop) => { | ||
(newObj as any)[prop] = deep ? clone((obj as any)[prop]) : (obj as any)[prop]; | ||
}); | ||
return newObj; | ||
} | ||
/** | ||
@@ -23,0 +63,0 @@ * 获取指定层级的父级(包括自身) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
452667
4532