Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tree keys disabled #1558

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions js/tree/tree-node-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isUndefined from 'lodash/isUndefined';
import isBoolean from 'lodash/isBoolean';
import omit from 'lodash/omit';
import get from 'lodash/get';
import { TreeNode } from './tree-node';
import { OptionData } from '../common';
import {
Expand Down Expand Up @@ -253,12 +254,14 @@ export class TreeNodeModel {
public setData(data: OptionData) {
const node = this[nodeKey];
// 详细细节可见 https://github.com/Tencent/tdesign-common/issues/655
const _data = omit(data, ['children', 'value', 'label']);
const _data = omit(data, ['children', 'value', 'label', 'disabled']);
const { keys } = node.tree.config;
const dataValue = data[keys?.value || 'value'];
const dataLabel = data[keys?.label || 'label'];
const dataValue = get(data, keys?.value || 'value');
const dataLabel = get(data, keys?.label || 'label');
const dataDisabled = get(data, keys?.disabled || 'disabled');
if (!isUndefined(dataValue)) _data.value = dataValue;
if (!isUndefined(dataLabel)) _data.label = dataLabel;
if (!isUndefined(dataDisabled)) _data.disable = dataDisabled;

Object.assign(node.data, _data);
Object.assign(node, _data);
Expand Down
11 changes: 6 additions & 5 deletions js/tree/tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const setableStatus: Record<string, boolean | null> = {
expandMutex: null,
activable: null,
checkable: null,
disabled: null,
draggable: null,
loading: false,
};
Expand Down Expand Up @@ -140,6 +139,7 @@ export class TreeNode {
const propChildren = keys.children || 'children';
const propLabel = keys.label || 'label';
const propValue = keys.value || 'value';
const propsDisabled = keys.disabled || 'disabled';

// 节点自身初始化数据
this.model = null;
Expand Down Expand Up @@ -168,7 +168,6 @@ export class TreeNode {
// 这种处理方式主要是解决 treeStore.setConfig 方法配置全局属性导致的状态切换与保留的问题
this.activable = null;
this.checkable = null;
this.disabled = null;
this.expandMutex = null;
this.draggable = null;

Expand All @@ -179,7 +178,7 @@ export class TreeNode {

// 设置 value
// 没有 value 的时候,value 默认使用自动生成的 唯一 id
this.value = isNil(data[propValue]) ? this[privateKey] : data[propValue];
this.value = isNil(get(data, propValue)) ? this[privateKey] : get(data, propValue);
const { nodeMap, privateMap } = tree;
if (nodeMap.get(this.value)) {
log.warn('Tree', `Dulplicate value: ${this.value}`);
Expand All @@ -188,7 +187,9 @@ export class TreeNode {
privateMap.set(this[privateKey], this);

// 设置标签
this.label = data[propLabel] || '';
this.label = get(data, propLabel) || '';
// 设置是否禁用
this.disabled = get(data, propsDisabled);

// 设置子节点
const children = data[propChildren];
Expand Down Expand Up @@ -557,7 +558,7 @@ export class TreeNode {
const { tree } = this;
const keys = Object.keys(item);
keys.forEach((key) => {
if (hasOwnProperty.call(setableStatus, key) || key === 'label') {
if (hasOwnProperty.call(setableStatus, key) || key === 'label' || key === 'disabled') {
this[key] = item[key];
}
});
Expand Down
1 change: 1 addition & 0 deletions js/tree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface KeysType {
value?: string;
label?: string;
children?: string;
disabled?: string;
}

export interface TreeNodeState {
Expand Down
Loading