|
| 1 | +--- |
| 2 | +order: 19 |
| 3 | +title: |
| 4 | + zh-CN: 新增数据功能 |
| 5 | + en-US: Add new option |
| 6 | +--- |
| 7 | + |
| 8 | +## zh-CN |
| 9 | + |
| 10 | +新增数据功能自定义渲染。 |
| 11 | + |
| 12 | +## en-US |
| 13 | + |
| 14 | +新增数据功能自定义渲染。 |
| 15 | + |
| 16 | +````jsx |
| 17 | +import React, { useState, useEffect } from 'react'; |
| 18 | +import { DataSet, Select, Button, Row, Col, Tooltip, Icon } from 'choerodon-ui/pro'; |
| 19 | +import { configure, Spin } from 'choerodon-ui'; |
| 20 | + |
| 21 | +const C7NAddNewOptionPromptRender = ({ |
| 22 | + type, |
| 23 | + component, |
| 24 | + record, |
| 25 | + field, |
| 26 | + code, |
| 27 | + path, |
| 28 | + disabledTooltipTitle, |
| 29 | +}) => { |
| 30 | + url = path; |
| 31 | + disabledTooltipTitle = disabledTooltipTitle || `当前账号无该字段对应功能的菜单权限,请联系管理员`; |
| 32 | + |
| 33 | + // TODO: 按钮根据用户是否有 url 权限, 判断是否禁用 |
| 34 | + const [isDisabled, setIsDisabled] = useState(false); |
| 35 | + const [isLoading, setIsLoading] = useState(true); |
| 36 | + useEffect(() => { |
| 37 | + // TODO: 模拟异步请求权限 |
| 38 | + setTimeout(() => { |
| 39 | + setIsLoading(false); |
| 40 | + setIsDisabled(true); |
| 41 | + }, 1000); |
| 42 | + }, []); |
| 43 | + |
| 44 | + const tooltip = isDisabled ? [ |
| 45 | + 'always', |
| 46 | + { |
| 47 | + title: disabledTooltipTitle, |
| 48 | + }, |
| 49 | + ] : undefined; |
| 50 | + |
| 51 | + if (!path) { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + |
| 55 | + if (isLoading) { |
| 56 | + return <Spin />; |
| 57 | + } |
| 58 | + |
| 59 | + if (component === 'Select' && type === 'prompt') { |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <span className={`c7n-pro-select-new-option-prompt-tip`}>没有找到数据?</span> |
| 63 | + <Button funcType="flat" color="primary" href={url} disabled={isDisabled} tooltip={tooltip}> |
| 64 | + 立即添加{isDisabled ? <Icon type="help" /> : undefined} |
| 65 | + </Button> |
| 66 | + </> |
| 67 | + ); |
| 68 | + } |
| 69 | + if (component === 'Select' && type === 'noDataPrompt') { |
| 70 | + return ( |
| 71 | + <> |
| 72 | + <span className={`c7n-pro-select-new-option-prompt-tip`}>暂无数据</span> |
| 73 | + <Button funcType="flat" color="primary" href={url} disabled={isDisabled} tooltip={tooltip}> |
| 74 | + 立即添加{isDisabled ? <Icon type="help" /> : undefined} |
| 75 | + </Button> |
| 76 | + </> |
| 77 | + ); |
| 78 | + } |
| 79 | + if (component === 'Lov' && type === 'prompt') { |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <span className={`c7n-pro-select-new-option-prompt-tip`}>没有找到数据?</span> |
| 83 | + <Button funcType="flat" color="primary" href={url} disabled={isDisabled} tooltip={tooltip}> |
| 84 | + 立即添加{isDisabled ? <Icon type="help" /> : undefined} |
| 85 | + </Button> |
| 86 | + </> |
| 87 | + ); |
| 88 | + } |
| 89 | + if (component === 'Lov' && type === 'noDataPrompt') { |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <div>图片占位</div> |
| 93 | + <span className={`c7n-pro-select-new-option-prompt-tip`}>暂无数据</span> |
| 94 | + <Button funcType="flat" color="primary" href={url} disabled={isDisabled} tooltip={tooltip}> |
| 95 | + 立即添加{isDisabled ? <Icon type="help" /> : undefined} |
| 96 | + </Button> |
| 97 | + </> |
| 98 | + ); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +const addNewOptionPromptRender = (props) => { |
| 103 | + return React.createElement(C7NAddNewOptionPromptRender, { ...props }); |
| 104 | +}; |
| 105 | + |
| 106 | +configure({ |
| 107 | + addNewOptionPromptRender: addNewOptionPromptRender, |
| 108 | +}); |
| 109 | + |
| 110 | +function handleDataSetChange({ record, name, value, oldValue }) { |
| 111 | + console.log('[dataset newValue]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name)); |
| 112 | +} |
| 113 | + |
| 114 | +const { Option } = Select; |
| 115 | + |
| 116 | +const data = [{ |
| 117 | + user: 'wu', |
| 118 | + user2: { |
| 119 | + text: 'Wu', |
| 120 | + value: 'wu', |
| 121 | + }, |
| 122 | +}]; |
| 123 | + |
| 124 | +class App extends React.Component { |
| 125 | + ds = new DataSet({ |
| 126 | + data, |
| 127 | + fields: [ |
| 128 | + { name: 'user', type: 'string', textField: 'text', label: '用户' }, |
| 129 | + { name: 'user2', type: 'object', textField: 'text', label: '用户object' }, |
| 130 | + ], |
| 131 | + events: { |
| 132 | + update: handleDataSetChange, |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + render() { |
| 137 | + return ( |
| 138 | + <Row> |
| 139 | + <Col span={12}> |
| 140 | + <Select dataSet={this.ds} searchable name="user" addNewOptionPrompt={{ path: "/components-pro/button-cn/" }}> |
| 141 | + <Option value="jack">Jack</Option> |
| 142 | + <Option value="lucy">Lucy</Option> |
| 143 | + <Option value="wu">Wu</Option> |
| 144 | + </Select> |
| 145 | + </Col> |
| 146 | + <Col span={12}> |
| 147 | + <Select dataSet={this.ds} name="user2" addNewOptionPrompt={{ path: "/components-pro/button-cn/", disabledTooltipTitle: '自定义提示' }}> |
| 148 | + <Option value="jack">Jack</Option> |
| 149 | + <Option value="lucy">Lucy</Option> |
| 150 | + <Option value="wu">Wu</Option> |
| 151 | + </Select> |
| 152 | + </Col> |
| 153 | + </Row> |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +ReactDOM.render( |
| 159 | + <App />, |
| 160 | + mountNode |
| 161 | +); |
| 162 | +```` |
0 commit comments