Skip to content

Commit

Permalink
feat: 修复 list lodash.get 默认值问题
Browse files Browse the repository at this point in the history
  • Loading branch information
l h b x s authored and l h b x s committed Aug 26, 2024
1 parent 0060c02 commit dae1f8a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/form-render/src/models/bindValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
_cloneDeep,
isArray,
isObject,
safeGet
} from '../utils/index';

const isMultiBind = (array: string[]) => isArray(array) && array.every(item => typeof item === 'string');
Expand Down Expand Up @@ -140,7 +141,7 @@ export const parseBindToValues = (values: any, flatten: any) => {
const data = _cloneDeep(values);
const dealFieldList = (obj: any, [path, ...rest]: any, bind: any) => {
if (rest.length === 1) {
const list = get(obj, path, []);
const list = safeGet(obj, path, []);
list.forEach((item: any, index: number) => {
if (bind === 'root') {
list[index] = { [rest[0]] : item };
Expand Down
13 changes: 13 additions & 0 deletions packages/form-render/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ export const hasFuncProperty = (obj: any) => {
});
};

/**
* 安全地获取对象的值,如果值为 null 或 undefined,则返回 defaultValue。
*
* @param {Object} object - 要获取值的对象。
* @param {string|Array} path - 要获取的路径,可以是字符串或数组。
* @param {*} [defaultValue] - 如果值为 null 或 undefined,则返回 defaultValue。
* @returns {*} - 返回获取的值,或者默认值。
*/
export const safeGet = (object: any, path: string, defaultValue: any) => {
const value = get(object, path);
return value === null || value === undefined ? defaultValue : value;
};


0 comments on commit dae1f8a

Please sign in to comment.