Commit e696089660131786ea24632ed75adc57b6ea58f4

Authored by Vben
1 parent 80b47c84

feat(tree): add clickRowToExpand option close #318

CHANGELOG.zh_CN.md
1 1 ## Wip
2 2  
  3 +### ✨ Features
  4 +
  5 +- `BasicTree` 新增`clickRowToExpand`,用于单击树节点展开
  6 +
3 7 ### 🐛 Bug Fixes
4 8  
5 9 - 修复`Description`已知问题
6 10 - 修复`BasicForm`已知问题
7 11 - 修复`BasicTree`下 ActionItem 的 show 属性逻辑问题
  12 +- 修复树组件 demo 示例样式错误
  13 +- 修复账号管理新增未清空旧数据
8 14  
9 15 ## 2.0.2 (2021-03-04)
10 16  
... ...
src/components/Tree/src/index.vue
... ... @@ -121,7 +121,7 @@
121 121 return icon;
122 122 }
123 123  
124   - async function handleRightClick({ event, node }: any) {
  124 + async function handleRightClick({ event, node }: Recordable) {
125 125 const { rightMenuList: menuList = [], beforeRightClick } = props;
126 126 let rightMenuList: ContextMenuItem[] = [];
127 127  
... ... @@ -137,14 +137,14 @@
137 137 });
138 138 }
139 139  
140   - function setExpandedKeys(keys: string[]) {
  140 + function setExpandedKeys(keys: Keys) {
141 141 state.expandedKeys = keys;
142 142 }
143 143  
144 144 function getExpandedKeys() {
145 145 return state.expandedKeys;
146 146 }
147   - function setSelectedKeys(keys: string[]) {
  147 + function setSelectedKeys(keys: Keys) {
148 148 state.selectedKeys = keys;
149 149 }
150 150  
... ... @@ -182,10 +182,23 @@
182 182 searchState.searchData = filter(unref(treeDataRef), (node) => {
183 183 const { title } = node;
184 184 return title?.includes(searchValue) ?? false;
185   - // || key?.includes(searchValue);
186 185 });
187 186 }
188 187  
  188 + function handleClickNode(key: string, children: TreeItem[]) {
  189 + if (!props.clickRowToExpand || !children || children.length === 0) return;
  190 + if (!state.expandedKeys.includes(key)) {
  191 + setExpandedKeys([...state.expandedKeys, key]);
  192 + } else {
  193 + const keys = [...state.expandedKeys];
  194 + const index = keys.findIndex((item) => item === key);
  195 + if (index !== -1) {
  196 + keys.splice(index, 1);
  197 + }
  198 + setExpandedKeys(keys);
  199 + }
  200 + }
  201 +
189 202 watchEffect(() => {
190 203 treeDataRef.value = props.treeData as TreeItem[];
191 204 state.expandedKeys = props.expandedKeys;
... ... @@ -264,11 +277,15 @@
264 277  
265 278 const propsData = omit(item, 'title');
266 279 const icon = getIcon({ ...item, level }, item.icon);
  280 + const children = get(item, childrenField) || [];
267 281 return (
268 282 <Tree.TreeNode {...propsData} node={toRaw(item)} key={get(item, keyField)}>
269 283 {{
270 284 title: () => (
271   - <span class={`${prefixCls}-title pl-2`}>
  285 + <span
  286 + class={`${prefixCls}-title pl-2`}
  287 + onClick={handleClickNode.bind(null, item.key, children)}
  288 + >
272 289 {icon && <TreeIcon icon={icon} />}
273 290 <span
274 291 class={`${prefixCls}__content`}
... ... @@ -279,8 +296,7 @@
279 296 <span class={`${prefixCls}__actions`}> {renderAction({ ...item, level })}</span>
280 297 </span>
281 298 ),
282   - default: () =>
283   - renderTreeNode({ data: get(item, childrenField) || [], level: level + 1 }),
  299 + default: () => renderTreeNode({ data: children, level: level + 1 }),
284 300 }}
285 301 </Tree.TreeNode>
286 302 );
... ... @@ -289,7 +305,7 @@
289 305 return () => {
290 306 const { title, helpMessage, toolbar, search } = props;
291 307 return (
292   - <div class={[prefixCls, 'h-full bg-white']}>
  308 + <div class={[prefixCls, 'h-full bg-white', attrs.class]}>
293 309 {(title || toolbar || search) && (
294 310 <TreeHeader
295 311 checkAll={checkAll}
... ...
src/components/Tree/src/props.ts
... ... @@ -21,6 +21,7 @@ export const basicProps = {
21 21 toolbar: propTypes.bool,
22 22 search: propTypes.bool,
23 23 checkStrictly: propTypes.bool,
  24 + clickRowToExpand: propTypes.bool.def(true),
24 25  
25 26 replaceFields: {
26 27 type: Object as PropType<ReplaceFields>,
... ...
src/components/Tree/src/types.ts
... ... @@ -14,11 +14,10 @@ export interface ReplaceFields {
14 14 key?: string;
15 15 }
16 16  
17   -export type Keys = string[] | number[];
  17 +export type Keys = (string | number)[];
18 18 export type CheckKeys =
19   - | string[]
20   - | number[]
21   - | { checked: string[] | number[]; halfChecked: string[] | number[] };
  19 + | (string | number)[]
  20 + | { checked: (string | number)[]; halfChecked: (string | number)[] };
22 21  
23 22 export interface TreeActionType {
24 23 checkAll: (checkAll: boolean) => void;
... ...
src/views/demo/system/account/AccountModal.vue
... ... @@ -17,7 +17,7 @@
17 17 setup(_, { emit }) {
18 18 const isUpdate = ref(true);
19 19  
20   - const [registerForm, { setFieldsValue, updateSchema, validate }] = useForm({
  20 + const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
21 21 labelWidth: 100,
22 22 schemas: accountFormSchema,
23 23 showActionButtonGroup: false,
... ... @@ -27,6 +27,7 @@
27 27 });
28 28  
29 29 const [registerModal, { setModalProps }] = useModalInner(async (data) => {
  30 + resetFields();
30 31 setModalProps({ confirmLoading: false });
31 32 isUpdate.value = !!data?.isUpdate;
32 33  
... ...