Skip to content

Commit

Permalink
refactor(Tree): 增加Tree组件在Form中使用的例子,修改Tree onChange参数 (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuilanxin committed Mar 9, 2022
1 parent ac8a9a1 commit 7280e79
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
134 changes: 133 additions & 1 deletion packages/react-tree/README.md
Expand Up @@ -18,7 +18,7 @@ import Tree from '@uiw/react-tree';
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
```jsx
import ReactDOM from 'react-dom';
import { Tree, Card, Row, Col } from 'uiw';
import { Tree, Card, Row, Col, } from 'uiw';

const data = [
{
Expand Down Expand Up @@ -651,6 +651,138 @@ const Demo = () => (
ReactDOM.render(<Demo />, _mount_);
```


### Form中使用

<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
```jsx
import ReactDOM from 'react-dom';
import { Tree, Row, Col, Form, Button } from 'uiw';

const data = [
{
label: '湖北省',
key: '0-0-0',
children:[
{
label: '武汉市',
key: '0-1-0',
children:[
{ label: '新洲区', key: '0-1-1' },
{ label: '武昌区', key: '0-1-2' },
{
label: '汉南区',
key: '0-1-3',
children:[
{ label: '汉南区1', key: '0-1-3-1' },
{ label: '汉南区2', key: '0-1-3-2' },
{ label: '汉南区3', key: '0-1-3-3' },
]
},
]
},
{ label: '黄冈市', key: '0-2-0' },
{
label: '黄石市',
key: '0-3-0',
children:[
{ label: '青山区', key: '0-3-1' },
{ label: '黄陂区', key: '0-3-2' },
{ label: '青山区', key: '0-3-3' },
]
},
]
},
{
label: '上海市',
key: '1-0-0',
children:[
{ label: '黄浦区', key: '1-0-1' },
{ label: '卢湾区', key: '1-0-2' },
{
label: '徐汇区',
key: '1-0-3',
children:[
{ label: '半淞园路街道', key: '1-1-0' },
{ label: '南京东路街道', key: '1-2-0' },
{ label: '外滩街道', key: '1-3-0' },
]
},
]
},
{
label: '北京市',
key: '2-0-0',
children:[
{ label: '东城区', key: '2-1-0' },
{ label: '西城区', key: '2-2-0' },
]
}
];

const Demo = () => {
return (
<Form
onChange={({ initial, current }) => {}}
resetOnSubmit={false}
onSubmitError={(error) => error && error.filed ? { ...error.filed } : null}
onSubmit={({initial, current}) => {
console.log('current: ', current);
const ErrObj = {};
if (current.tree === 'unknown') {
ErrObj.radioGroup = '请选择性别!';
}
if(Object.keys(ErrObj).length > 0) {
const err = new Error();
err.filed = ErrObj;
throw err;
}
Notify.success({
title: '提交成功!', description: `填写:【填写成功】!`
});
}}
fields={{
tree: {
value: 'girl',
label: '请输入内容',
help: '必须选择性别!',
initialValue: ['0-1-1'],
children: <Tree
// multiple
checkStrictly
data={data}
/>,
},
}}
>
{({ fields, state, canSubmit }) => {
return (
<div>
<Row>
<Col>{fields.tree}</Col>
</Row>
<Row>
<Col>
<Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
</Col>
</Row>
<Row>
<Col>
<pre style={{ padding: 10, marginTop: 10 }}>
{JSON.stringify(state.current, null, 2)}
</pre>
</Col>
</Row>
</div>
);
}}
</Form>
)
}
ReactDOM.render(<Demo />, _mount_);
```


## Props

| 参数 | 说明 | 类型 | 默认值 |
Expand Down
4 changes: 2 additions & 2 deletions packages/react-tree/src/index.tsx
Expand Up @@ -45,7 +45,7 @@ export interface TreeProps extends IProps, Omit<HTMLDivProps, 'onChange'> {
item: TreeData,
evn: React.MouseEvent<HTMLElement>,
) => void;
onChange?: (keys: (string | number | undefined)[]) => void;
onChange?: (key: string | number, eys: (string | number | undefined)[]) => void;
value?: TreeData['key'][];
}

Expand Down Expand Up @@ -210,7 +210,7 @@ export default function Tree(props: TreeProps) {
}
setCurSelectedKeys(selKeys);
onSelected && onSelected(selKeys, item.key, selected, item, evn);
onChange?.(selKeys);
onChange?.(item.key, selKeys);
}
return (
<div className={cls} {...elementProps}>
Expand Down
4 changes: 2 additions & 2 deletions website/src/routes/components/tree/index.tsx
@@ -1,11 +1,11 @@
import React from 'react';
import { Tree, Row, Col, Card, Icon } from 'uiw';
import { Tree, Row, Col, Card, Icon, Form, Button } from 'uiw';
import Markdown from '../../../components/Markdown';

export default () => (
<Markdown
path="https://github.com/uiwjs/uiw/tree/master/packages/react-tree/README.md"
dependencies={{ Tree, Row, Col, Card, Icon }}
dependencies={{ Tree, Row, Col, Card, Icon, Form, Button }}
renderPage={async () => {
const md = await import('uiw/node_modules/@uiw/react-tree/README.md');
return md.default || md;
Expand Down

0 comments on commit 7280e79

Please sign in to comment.