组件演示
[{"title":"基本用法","id":"components-transfer-demo-basic","tags":[],"filepath":"site/components/transfer/demo/basic.md","directory":"components/transfer/demo","filename":"basic","meta":{"title":"基本用法","description":"\n<p>基本使用和常见操作</p>\n","order":"0","filepath":"site/components/transfer/demo/basic.md","filename":"basic","directory":"components/transfer/demo","id":"components-transfer-demo-basic","template":"demos","html":"<script>(function(){'use strict';\n\nvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _uxcore = require('uxcore');\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar mockData = [];\nvar len = Math.random() * 10 + 40;\nfor (var i = 0; i < len; i += 1) {\n mockData.push({\n name: '\\u5185\\u5BB9' + (i + 1),\n value: i + 1,\n description: '\\u5185\\u5BB9' + (i + 1) + '\\u7684\\u63CF\\u8FF0',\n chosen: Math.random() * 2 > 1,\n keywords: ['neirong' + (i + 1)], // used in search & location\n disabled: i > 15\n });\n}\n\nvar TransferDemo = function (_React$Component) {\n _inherits(TransferDemo, _React$Component);\n\n function TransferDemo(props) {\n _classCallCheck(this, TransferDemo);\n\n var _this = _possibleConstructorReturn(this, (TransferDemo.__proto__ || Object.getPrototypeOf(TransferDemo)).call(this, props));\n\n _this.state = {\n disable: false,\n data: mockData\n };\n _this.handleChange = _this.handleChange.bind(_this);\n _this.handleClick = _this.handleClick.bind(_this);\n _this.handleClick2 = _this.handleClick2.bind(_this);\n _this.handleClick3 = _this.handleClick3.bind(_this);\n _this.handleClick4 = _this.handleClick4.bind(_this);\n return _this;\n }\n\n _createClass(TransferDemo, [{\n key: 'handleChange',\n value: function handleChange(data) {\n console.log(data);\n }\n }, {\n key: 'handleClick',\n value: function handleClick() {\n this.ref.selectItems([2, 3]);\n }\n }, {\n key: 'handleClick2',\n value: function handleClick2() {\n this.setState({\n disable: !this.state.disable\n });\n }\n }, {\n key: 'handleClick3',\n value: function handleClick3() {\n console.log('work');\n var mockData2 = [];\n var length = Math.random() * 10 + 40;\n for (var _i = 0; _i < length; _i += 1) {\n mockData2.push({\n name: '\\u5185\\u5BB9' + (_i + 1),\n value: _i + 1,\n description: '\\u5185\\u5BB9' + (_i + 1) + '\\u7684\\u63CF\\u8FF0',\n chosen: Math.random() * 2 > 1,\n keywords: ['neirong' + (_i + 1)] // used in search & location\n });\n }\n this.setState({\n data: mockData2\n });\n }\n }, {\n key: 'handleClick4',\n value: function handleClick4() {\n this.ref.reset();\n }\n }, {\n key: 'render',\n value: function render() {\n var _this2 = this;\n\n var me = this;\n return React.createElement(\n 'div',\n null,\n React.createElement(_uxcore.Transfer, {\n showSearch: true,\n disabled: me.state.disable,\n ref: function ref(c) {\n _this2.ref = c;\n },\n data: this.state.data,\n onChange: me.handleChange\n }),\n React.createElement(\n 'div',\n { style: { marginTop: '20px' } },\n React.createElement(\n _uxcore.Button,\n { onClick: me.handleClick },\n '\\u624B\\u52A8\\u66F4\\u6539\\u88AB\\u9009\\u4E2D\\u7684\\u9879'\n ),\n React.createElement(\n _uxcore.Button,\n { type: 'secondary', style: { marginLeft: 12 }, onClick: me.handleClick2 },\n '\\u66F4\\u6539 mode'\n ),\n React.createElement(\n _uxcore.Button,\n { type: 'secondary', style: { marginLeft: 12 }, onClick: me.handleClick3 },\n '\\u66F4\\u6539 props'\n ),\n React.createElement(\n _uxcore.Button,\n { type: 'secondary', style: { marginLeft: 12 }, onClick: me.handleClick4 },\n '\\u91CD\\u7F6E'\n )\n )\n );\n }\n }]);\n\n return TransferDemo;\n}(React.Component);\n\nReactDOM.render(React.createElement(TransferDemo, null), document.getElementById('components-transfer-demo-basic'));})()</script><div class=\"highlight\"><pre><code class=\"javascript\"><span class=\"hljs-keyword\">import</span> { Button } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n<span class=\"hljs-keyword\">import</span> { Transfer } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n\n<span class=\"hljs-keyword\">const</span> mockData = [];\n<span class=\"hljs-keyword\">const</span> len = (<span class=\"hljs-built_in\">Math</span>.random() * <span class=\"hljs-number\">10</span>) + <span class=\"hljs-number\">40</span>;\n<span class=\"hljs-keyword\">for</span> (<span class=\"hljs-keyword\">let</span> i = <span class=\"hljs-number\">0</span>; i < len; i += <span class=\"hljs-number\">1</span>) {\n mockData.push({\n name: <span class=\"hljs-string\">`内容<span class=\"hljs-subst\">${i + 1}</span>`</span>,\n value: (i + <span class=\"hljs-number\">1</span>),\n description: <span class=\"hljs-string\">`内容<span class=\"hljs-subst\">${i + 1}</span>的描述`</span>,\n chosen: <span class=\"hljs-built_in\">Math</span>.random() * <span class=\"hljs-number\">2</span> > <span class=\"hljs-number\">1</span>,\n keywords: [<span class=\"hljs-string\">`neirong<span class=\"hljs-subst\">${i + 1}</span>`</span>], <span class=\"hljs-comment\">// used in search & location</span>\n disabled: i > <span class=\"hljs-number\">15</span>,\n });\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">TransferDemo</span> <span class=\"hljs-keyword\">extends</span> <span class=\"hljs-title\">React</span>.<span class=\"hljs-title\">Component</span> </span>{\n constructor(props) {\n <span class=\"hljs-keyword\">super</span>(props);\n <span class=\"hljs-keyword\">this</span>.state = {\n disable: <span class=\"hljs-literal\">false</span>,\n data: mockData,\n };\n <span class=\"hljs-keyword\">this</span>.handleChange = <span class=\"hljs-keyword\">this</span>.handleChange.bind(<span class=\"hljs-keyword\">this</span>);\n <span class=\"hljs-keyword\">this</span>.handleClick = <span class=\"hljs-keyword\">this</span>.handleClick.bind(<span class=\"hljs-keyword\">this</span>);\n <span class=\"hljs-keyword\">this</span>.handleClick2 = <span class=\"hljs-keyword\">this</span>.handleClick2.bind(<span class=\"hljs-keyword\">this</span>);\n <span class=\"hljs-keyword\">this</span>.handleClick3 = <span class=\"hljs-keyword\">this</span>.handleClick3.bind(<span class=\"hljs-keyword\">this</span>);\n <span class=\"hljs-keyword\">this</span>.handleClick4 = <span class=\"hljs-keyword\">this</span>.handleClick4.bind(<span class=\"hljs-keyword\">this</span>);\n }\n\n handleChange(data) {\n <span class=\"hljs-built_in\">console</span>.log(data);\n }\n\n handleClick() {\n <span class=\"hljs-keyword\">this</span>.ref.selectItems([<span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>]);\n }\n\n handleClick2() {\n <span class=\"hljs-keyword\">this</span>.setState({\n disable: !<span class=\"hljs-keyword\">this</span>.state.disable,\n });\n }\n handleClick3() {\n <span class=\"hljs-built_in\">console</span>.log(<span class=\"hljs-string\">'work'</span>);\n <span class=\"hljs-keyword\">const</span> mockData2 = [];\n <span class=\"hljs-keyword\">const</span> length = (<span class=\"hljs-built_in\">Math</span>.random() * <span class=\"hljs-number\">10</span>) + <span class=\"hljs-number\">40</span>;\n <span class=\"hljs-keyword\">for</span> (<span class=\"hljs-keyword\">let</span> i = <span class=\"hljs-number\">0</span>; i < length; i += <span class=\"hljs-number\">1</span>) {\n mockData2.push({\n name: <span class=\"hljs-string\">`内容<span class=\"hljs-subst\">${i + 1}</span>`</span>,\n value: (i + <span class=\"hljs-number\">1</span>),\n description: <span class=\"hljs-string\">`内容<span class=\"hljs-subst\">${i + 1}</span>的描述`</span>,\n chosen: <span class=\"hljs-built_in\">Math</span>.random() * <span class=\"hljs-number\">2</span> > <span class=\"hljs-number\">1</span>,\n keywords: [<span class=\"hljs-string\">`neirong<span class=\"hljs-subst\">${i + 1}</span>`</span>], <span class=\"hljs-comment\">// used in search & location</span>\n });\n }\n <span class=\"hljs-keyword\">this</span>.setState({\n data: mockData2,\n });\n }\n\n handleClick4() {\n <span class=\"hljs-keyword\">this</span>.ref.reset();\n }\n\n render() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">return</span> (\n <span class=\"xml\"><span class=\"hljs-tag\"><<span class=\"hljs-title\">div</span>></span>\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">Transfer</span>\n <span class=\"hljs-attribute\">showSearch</span>\n <span class=\"hljs-attribute\">disabled</span>=<span class=\"hljs-value\">{me.state.disable}</span>\n <span class=\"hljs-attribute\">ref</span>=<span class=\"hljs-value\">{(c)</span> =></span> { this.ref = c; }}\n data={this.state.data}\n onChange={me.handleChange}\n />\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">div</span> <span class=\"hljs-attribute\">style</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">marginTop:</span> '<span class=\"hljs-attribute\">20px</span>' }}></span>\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick}</span>></span>手动更改被选中的项<span class=\"hljs-tag\"></<span class=\"hljs-title\">Button</span>></span>\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">type</span>=<span class=\"hljs-value\">\"secondary\"</span> <span class=\"hljs-attribute\">style</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">marginLeft:</span> <span class=\"hljs-attribute\">12</span> }} <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick2}</span>></span>更改 mode<span class=\"hljs-tag\"></<span class=\"hljs-title\">Button</span>></span>\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">type</span>=<span class=\"hljs-value\">\"secondary\"</span> <span class=\"hljs-attribute\">style</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">marginLeft:</span> <span class=\"hljs-attribute\">12</span> }} <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick3}</span>></span>更改 props<span class=\"hljs-tag\"></<span class=\"hljs-title\">Button</span>></span>\n <span class=\"hljs-tag\"><<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">type</span>=<span class=\"hljs-value\">\"secondary\"</span> <span class=\"hljs-attribute\">style</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">marginLeft:</span> <span class=\"hljs-attribute\">12</span> }} <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick4}</span>></span>重置<span class=\"hljs-tag\"></<span class=\"hljs-title\">Button</span>></span>\n <span class=\"hljs-tag\"></<span class=\"hljs-title\">div</span>></span>\n <span class=\"hljs-tag\"></<span class=\"hljs-title\">div</span>></span>\n )</span>;\n }\n}\n\nReactDOM.render(\n <span class=\"xml\"><span class=\"hljs-tag\"><<span class=\"hljs-title\">TransferDemo</span> /></span>,\n document.getElementById('components-transfer-demo-basic')\n);</span></code></pre></div>"},"status":"public","toc":""}]
import { Button } from 'uxcore';
import { Transfer } from 'uxcore';
const mockData = [];
const len = (Math.random() * 10) + 40;
for (let i = 0; i < len; i += 1) {
mockData.push({
name: `内容${i + 1}`,
value: (i + 1),
description: `内容${i + 1}的描述`,
chosen: Math.random() * 2 > 1,
keywords: [`neirong${i + 1}`], // used in search & location
disabled: i > 15,
});
}
class TransferDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
disable: false,
data: mockData,
};
this.handleChange = this.handleChange.bind(this);
this.handleClick = this.handleClick.bind(this);
this.handleClick2 = this.handleClick2.bind(this);
this.handleClick3 = this.handleClick3.bind(this);
this.handleClick4 = this.handleClick4.bind(this);
}
handleChange(data) {
console.log(data);
}
handleClick() {
this.ref.selectItems([2, 3]);
}
handleClick2() {
this.setState({
disable: !this.state.disable,
});
}
handleClick3() {
console.log('work');
const mockData2 = [];
const length = (Math.random() * 10) + 40;
for (let i = 0; i < length; i += 1) {
mockData2.push({
name: `内容${i + 1}`,
value: (i + 1),
description: `内容${i + 1}的描述`,
chosen: Math.random() * 2 > 1,
keywords: [`neirong${i + 1}`], // used in search & location
});
}
this.setState({
data: mockData2,
});
}
handleClick4() {
this.ref.reset();
}
render() {
const me = this;
return (
<div>
<Transfer
showSearch
disabled={me.state.disable}
ref={(c) => { this.ref = c; }}
data={this.state.data}
onChange={me.handleChange}
/>
<div style={{ marginTop: '20px' }}>
<Button onClick={me.handleClick}>手动更改被选中的项</Button>
<Button type="secondary" style={{ marginLeft: 12 }} onClick={me.handleClick2}>更改 mode</Button>
<Button type="secondary" style={{ marginLeft: 12 }} onClick={me.handleClick3}>更改 props</Button>
<Button type="secondary" style={{ marginLeft: 12 }} onClick={me.handleClick4}>重置</Button>
</div>
</div>
);
}
}
ReactDOM.render(
<TransferDemo />,
document.getElementById('components-transfer-demo-basic')
);
API
- selectItems(arr): 使对应 value 的变成高亮状态 (selected)。
参数:- arr
Array
一个由 value 组成的数组。
- arr
Props
参数 | 类型 | 必需 | 默认值 | 说明 |
---|---|---|---|---|
data | array | required | - | 用于初始化 transfer 的数据,格式见 Usage,其中 name 和 value 字段必有 |
disabled | boolean | optional | false | 是否启用 disable 模式 |
showSearch | boolean | optional | true | 是否显示搜索条 |
searchPlaceholder | string | optional | 定位输入内容 | |
leftTitle | string | optional | '未选中的' | 左侧标题 |
rightTitle | string | optional | '已选中的' | 右侧标题 |
onChange | func | optional | noop | 选中情况变化时触发,返回选中和未选中的项 |