组件演示

[{"title":"表单联动","id":"scene-commonform-demo-basic","tags":[],"filepath":"site/scene/commonForm/demo/basic.md","directory":"scene/commonForm/demo","filename":"basic","meta":{"title":"表单联动","description":"\n","order":"0","filepath":"site/scene/commonForm/demo/basic.md","filename":"basic","directory":"scene/commonForm/demo","id":"scene-commonform-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 Input = _uxcore.Form.InputFormField,\n Select = _uxcore.Form.SelectFormField,\n Check = _uxcore.Form.CheckboxGroupFormField,\n Validators = _uxcore.Form.Validators,\n Other = _uxcore.Form.OtherFormField;\n\nvar Demo = function (_React$Component) {\n _inherits(Demo, _React$Component);\n\n function Demo(props) {\n _classCallCheck(this, Demo);\n\n var _this = _possibleConstructorReturn(this, (Demo.__proto__ || Object.getPrototypeOf(Demo)).call(this, props));\n\n _this.state = {\n values: {}\n };\n return _this;\n }\n\n _createClass(Demo, [{\n key: 'handleFieldChange',\n value: function handleFieldChange(value, name, pass) {\n var names = ['a', 'b'];\n if (pass && names.indexOf(name) !== -1) {\n var me = this;\n var data = me.refs.form.getValues();\n var values = data.values;\n me.refs.form.setValues({\n total: parseFloat(values.a || 0) + parseFloat(values.b || 0)\n });\n }\n }\n }, {\n key: 'handleClick',\n value: function handleClick() {\n var me = this;\n alert(JSON.stringify(me.refs.form.getValues(true)));\n }\n }, {\n key: 'render',\n value: function render() {\n var me = this;\n\n return React.createElement(\n 'div',\n { className: 'page-demo1' },\n React.createElement(\n _uxcore.Form,\n { jsxonChange: me.handleFieldChange.bind(me), jsxvalues: me.state.values, ref: 'form', instantValidate: false },\n React.createElement(Input, { jsxlabel: 'A', jsxname: 'a', jsxrules: { validator: Validators.isNum, errMsg: '必须为数字' } }),\n React.createElement(Input, { jsxlabel: 'B', jsxname: 'b', jsxrules: { validator: Validators.isNum, errMsg: '必须为数字' } }),\n React.createElement(Input, { jsxlabel: 'A+B', jsxname: 'total' }),\n React.createElement(\n Other,\n { className: 'other' },\n React.createElement(\n _uxcore.Button,\n { onClick: me.handleClick.bind(me) },\n '\\u786E\\u5B9A'\n )\n )\n )\n );\n }\n }]);\n\n return Demo;\n}(React.Component);\n\nReactDOM.render(React.createElement(Demo, null), document.getElementById('scene-commonform-demo-basic'));\n\n/* JS END CSS START*/})()</script><div class=\"highlight\"><pre><code class=\"javascript\">\n<span class=\"hljs-keyword\">import</span> { Form } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n<span class=\"hljs-keyword\">import</span> { Button } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n\n<span class=\"hljs-keyword\">const</span> {\n InputFormField: Input,\n SelectFormField: Select,\n CheckboxGroupFormField: Check,\n Validators,\n OtherFormField: Other,\n} = Form;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Demo</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 values: {},\n };\n }\n\n handleFieldChange(value, name, pass) {\n <span class=\"hljs-keyword\">const</span> names = [<span class=\"hljs-string\">'a'</span>, <span class=\"hljs-string\">'b'</span>];\n <span class=\"hljs-keyword\">if</span> (pass &amp;&amp; names.indexOf(name) !== -<span class=\"hljs-number\">1</span>) {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> data = me.refs.form.getValues();\n <span class=\"hljs-keyword\">const</span> values = data.values;\n me.refs.form.setValues({\n total: <span class=\"hljs-built_in\">parseFloat</span>(values.a || <span class=\"hljs-number\">0</span>) + <span class=\"hljs-built_in\">parseFloat</span>(values.b || <span class=\"hljs-number\">0</span>),\n });\n }\n }\n\n handleClick() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n alert(<span class=\"hljs-built_in\">JSON</span>.stringify(me.refs.form.getValues(<span class=\"hljs-literal\">true</span>)));\n }\n\n render() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n\n <span class=\"hljs-keyword\">return</span> (\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">div</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"page-demo1\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Form</span> <span class=\"hljs-attribute\">jsxonChange</span>=<span class=\"hljs-value\">{me.handleFieldChange.bind(me)}</span> <span class=\"hljs-attribute\">jsxvalues</span>=<span class=\"hljs-value\">{me.state.values}</span> <span class=\"hljs-attribute\">ref</span>=<span class=\"hljs-value\">\"form\"</span> <span class=\"hljs-attribute\">instantValidate</span>=<span class=\"hljs-value\">{false}</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Input</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"A\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"a\"</span> <span class=\"hljs-attribute\">jsxrules</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">validator:</span> <span class=\"hljs-attribute\">Validators.isNum</span>, <span class=\"hljs-attribute\">errMsg:</span> '必须为数字' }} /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Input</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"B\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"b\"</span> <span class=\"hljs-attribute\">jsxrules</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">validator:</span> <span class=\"hljs-attribute\">Validators.isNum</span>, <span class=\"hljs-attribute\">errMsg:</span> '必须为数字' }} /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Input</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"A+B\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"total\"</span> /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Other</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"other\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick.bind(me)}</span>&gt;</span>确定<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Button</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Form</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">div</span>&gt;</span>\n )</span>;\n }\n\n}\n\nReactDOM.render(\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Demo</span> /&gt;</span>\n, document.getElementById('scene-commonform-demo-basic'));\n\n/* JS END CSS START*/</span></code></pre></div><style type=\"text/css\">.other {\n padding-left: 100px\n}</style><div class=\"highlight\"><pre><code class=\"css\"><span class=\"hljs-class\">.other</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">padding-left</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">100px</span>\n</span></span></span>}</code></pre></div>"},"status":"public","toc":""},{"title":"表单的扩展1 -- 自定义一个 FormField","id":"scene-commonform-demo-definefield","tags":[],"filepath":"site/scene/commonForm/demo/defineField.md","directory":"scene/commonForm/demo","filename":"defineField","meta":{"title":"表单的扩展1 -- 自定义一个 FormField","description":"\n<p>通过继承 Form.FormField,用户可以自己定义特制的 FormField,以满足各种业务上的需求。<br> 定制的 FormField 同样可以使用 Form 带来通用配置,如布局、样式、校验、值的管理等等。</p>\n","order":"1","filepath":"site/scene/commonForm/demo/defineField.md","filename":"defineField","directory":"scene/commonForm/demo","id":"scene-commonform-demo-definefield","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\nvar _deepcopy = require('deepcopy');\n\nvar _deepcopy2 = _interopRequireDefault(_deepcopy);\n\nvar _objectAssign = require('object-assign');\n\nvar _objectAssign2 = _interopRequireDefault(_objectAssign);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\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\n;\n;\nvar Other = _uxcore.Form.OtherFormField;\n\nvar DoubleInputFormField = function (_FormField) {\n _inherits(DoubleInputFormField, _FormField);\n\n function DoubleInputFormField(props) {\n _classCallCheck(this, DoubleInputFormField);\n\n return _possibleConstructorReturn(this, (DoubleInputFormField.__proto__ || Object.getPrototypeOf(DoubleInputFormField)).call(this, props));\n // 这里不要直接覆盖 this.state = {};\n // 如果希望增加 state,使用扩展。\n // this.state = assign({}, this.state, {xxx: 1})\n }\n\n // 改写 addSpecificClass,增加该 FormField 专属的 class\n\n _createClass(DoubleInputFormField, [{\n key: 'addSpecificClass',\n value: function addSpecificClass() {\n return 'kuma-doubleinput-uxform-field';\n }\n\n // 在合适的时机调用 me.handleDataChange,将变动通知给 Form\n\n }, {\n key: 'handleChange',\n value: function handleChange(index, e) {\n var me = this;\n var value = e.currentTarget.value;\n var stateValue = me.state.value || [];\n stateValue[index] = value;\n me.handleDataChange(stateValue);\n }\n\n // 改写 renderField,决定渲染方式\n\n }, {\n key: 'renderField',\n value: function renderField() {\n var me = this;\n var arr = [];\n // 这里注意取值使用 state.value,完成和 Form 的通信。\n var values = me.state.value || [];\n arr.push(React.createElement('input', { className: 'kuma-input', value: values[0], onChange: me.handleChange.bind(me, 0), key: 'input1' }));\n arr.push(React.createElement(\n 'span',\n { className: 'split', key: 'split' },\n '-'\n ));\n arr.push(React.createElement('input', { className: 'kuma-input', value: values[1], onChange: me.handleChange.bind(me, 1), key: 'input2' }));\n return arr;\n }\n }]);\n\n return DoubleInputFormField;\n}(_uxcore.FormField);\n\nDoubleInputFormField.displayName = 'DoubleInputFormField'; // 重要,必须 displayName 中带有 FormField 才可以被识别为 FormField。\n\n// 如果你的代码需要兼容 IE,以下两行不可缺少。\n// 即使不需要兼容 IE,也不可直接覆盖,如 DoubleInputFormField.defaultProps = {};\nDoubleInputFormField.defaultProps = (0, _objectAssign2.default)({}, _uxcore.FormField.defaultProps);\nDoubleInputFormField.propTypes = (0, _objectAssign2.default)({}, _uxcore.FormField.propTypes);\n\nvar Demo = function (_React$Component) {\n _inherits(Demo, _React$Component);\n\n function Demo(props) {\n _classCallCheck(this, Demo);\n\n var _this2 = _possibleConstructorReturn(this, (Demo.__proto__ || Object.getPrototypeOf(Demo)).call(this, props));\n\n _this2.state = {\n values: {}\n };\n return _this2;\n }\n\n _createClass(Demo, [{\n key: 'handleClick',\n value: function handleClick() {\n var me = this;\n alert(JSON.stringify(me.refs.form.getValues()));\n }\n }, {\n key: 'handleReset',\n value: function handleReset() {\n var me = this;\n me.refs.form.resetValues();\n }\n }, {\n key: 'render',\n value: function render() {\n var me = this;\n\n // FormField 通用的 props 都可以使用。\n return React.createElement(\n 'div',\n { className: 'page-demo2' },\n React.createElement(\n _uxcore.Form,\n { ref: 'form', jsxvalues: {\n double: [2, 4]\n }\n },\n React.createElement(DoubleInputFormField, { jsxlabel: '\\u53CC\\u8F93\\u5165\\u6846', jsxname: 'double' }),\n React.createElement(\n Other,\n { className: 'other' },\n React.createElement(\n _uxcore.Button,\n { onClick: me.handleClick.bind(this) },\n '\\u786E\\u5B9A'\n )\n )\n )\n );\n }\n }]);\n\n return Demo;\n}(React.Component);\n\nReactDOM.render(React.createElement(Demo, null), document.getElementById('scene-commonform-demo-definefield'));\n\n/* JS END CSS START*/})()</script><div class=\"highlight\"><pre><code class=\"javascript\">\n<span class=\"hljs-keyword\">import</span> { Form } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n<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> deepcopy <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'deepcopy'</span>;;\n<span class=\"hljs-keyword\">import</span> assign <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'object-assign'</span>;;\n<span class=\"hljs-keyword\">import</span> { FormField } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n\n<span class=\"hljs-keyword\">const</span> {\n OtherFormField: Other,\n} = Form;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">DoubleInputFormField</span> <span class=\"hljs-keyword\">extends</span> <span class=\"hljs-title\">FormField</span> </span>{\n constructor(props) {\n <span class=\"hljs-keyword\">super</span>(props);\n <span class=\"hljs-comment\">// 这里不要直接覆盖 this.state = {};</span>\n <span class=\"hljs-comment\">// 如果希望增加 state,使用扩展。</span>\n <span class=\"hljs-comment\">// this.state = assign({}, this.state, {xxx: 1})</span>\n }\n\n <span class=\"hljs-comment\">// 改写 addSpecificClass,增加该 FormField 专属的 class</span>\n\n addSpecificClass() {\n <span class=\"hljs-keyword\">return</span> <span class=\"hljs-string\">'kuma-doubleinput-uxform-field'</span>;\n }\n\n\n <span class=\"hljs-comment\">// 在合适的时机调用 me.handleDataChange,将变动通知给 Form</span>\n\n handleChange(index, e) {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> value = e.currentTarget.value;\n <span class=\"hljs-keyword\">const</span> stateValue = me.state.value || [];\n stateValue[index] = value;\n me.handleDataChange(stateValue);\n }\n\n <span class=\"hljs-comment\">// 改写 renderField,决定渲染方式</span>\n\n renderField() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> arr = [];\n <span class=\"hljs-comment\">// 这里注意取值使用 state.value,完成和 Form 的通信。</span>\n <span class=\"hljs-keyword\">const</span> values = me.state.value || [];\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">input</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"kuma-input\"</span> <span class=\"hljs-attribute\">value</span>=<span class=\"hljs-value\">{values[0]}</span> <span class=\"hljs-attribute\">onChange</span>=<span class=\"hljs-value\">{me.handleChange.bind(me,</span> <span class=\"hljs-attribute\">0</span>)} <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"input1\"</span> /&gt;</span>)</span>;\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">span</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"split\"</span> <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"split\"</span>&gt;</span>-<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">span</span>&gt;</span>)</span>;\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">input</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"kuma-input\"</span> <span class=\"hljs-attribute\">value</span>=<span class=\"hljs-value\">{values[1]}</span> <span class=\"hljs-attribute\">onChange</span>=<span class=\"hljs-value\">{me.handleChange.bind(me,</span> <span class=\"hljs-attribute\">1</span>)} <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"input2\"</span> /&gt;</span>)</span>;\n <span class=\"hljs-keyword\">return</span> arr;\n }\n\n}\n\nDoubleInputFormField.displayName = <span class=\"hljs-string\">'DoubleInputFormField'</span>; <span class=\"hljs-comment\">// 重要,必须 displayName 中带有 FormField 才可以被识别为 FormField。</span>\n\n<span class=\"hljs-comment\">// 如果你的代码需要兼容 IE,以下两行不可缺少。</span>\n<span class=\"hljs-comment\">// 即使不需要兼容 IE,也不可直接覆盖,如 DoubleInputFormField.defaultProps = {};</span>\nDoubleInputFormField.defaultProps = assign({}, FormField.defaultProps);\nDoubleInputFormField.propTypes = assign({}, FormField.propTypes);\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Demo</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 values: {},\n };\n }\n\n handleClick() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n alert(<span class=\"hljs-built_in\">JSON</span>.stringify(me.refs.form.getValues()));\n }\n\n handleReset() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n me.refs.form.resetValues();\n }\n\n render() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n\n <span class=\"hljs-comment\">// FormField 通用的 props 都可以使用。</span>\n <span class=\"hljs-keyword\">return</span> (\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">div</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"page-demo2\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Form</span> <span class=\"hljs-attribute\">ref</span>=<span class=\"hljs-value\">\"form\"</span> <span class=\"hljs-attribute\">jsxvalues</span>=<span class=\"hljs-value\">{{</span>\n <span class=\"hljs-attribute\">double:</span> [<span class=\"hljs-attribute\">2</span>, <span class=\"hljs-attribute\">4</span>],\n }}\n &gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">DoubleInputFormField</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"双输入框\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"double\"</span> /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Other</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"other\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick.bind(this)}</span>&gt;</span>确定<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Button</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Form</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">div</span>&gt;</span>\n )</span>;\n }\n\n}\n\nReactDOM.render(\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Demo</span> /&gt;</span>\n, document.getElementById('scene-commonform-demo-definefield'));\n\n/* JS END CSS START*/</span></code></pre></div><style type=\"text/css\">.page-demo2 .other {\n padding-left: 100px\n}\n.page-demo2 .kuma-doubleinput-uxform-field .kuma-input {\n width: 200px\n}\n.page-demo2 .kuma-doubleinput-uxform-field .split {\n margin: 0 5px;\n}\n.page-demo2 .kuma-button {\n margin-right: 10px;\n}</style><div class=\"highlight\"><pre><code class=\"css\"><span class=\"hljs-class\">.page-demo2</span> <span class=\"hljs-class\">.other</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">padding-left</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">100px</span>\n</span></span></span>}\n<span class=\"hljs-class\">.page-demo2</span> <span class=\"hljs-class\">.kuma-doubleinput-uxform-field</span> <span class=\"hljs-class\">.kuma-input</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">width</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">200px</span>\n</span></span></span>}\n<span class=\"hljs-class\">.page-demo2</span> <span class=\"hljs-class\">.kuma-doubleinput-uxform-field</span> <span class=\"hljs-class\">.split</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">margin</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">0</span> <span class=\"hljs-number\">5px</span></span></span>;\n}</span>\n<span class=\"hljs-class\">.page-demo2</span> <span class=\"hljs-class\">.kuma-button</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">margin-right</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">10px</span></span></span>;\n}</span></code></pre></div>"},"status":"public","toc":""},{"title":"表单的扩展2 -- 使用 API 自定义一个 FormField","id":"scene-commonform-demo-definefieldapi","tags":[],"filepath":"site/scene/commonForm/demo/defineFieldApi.md","directory":"scene/commonForm/demo","filename":"defineFieldApi","meta":{"title":"表单的扩展2 -- 使用 API 自定义一个 FormField","description":"\n<p>通过继承 Form.createFormField API,用户可以快速地将自己的业务组件封装成 FormField,以满足各种业务上的需求。<br>定制的 FormField 同样可以使用 Form 带来通用配置,如布局、样式、校验、值的管理等等。<br>可以与上面的例子进行一个对比,感受一下之间的不同性,这种方式没有扩展 FormField 的方式定制,但相比较起来封装更加简单。</p>\n","order":"2","filepath":"site/scene/commonForm/demo/defineFieldApi.md","filename":"defineFieldApi","directory":"scene/commonForm/demo","id":"scene-commonform-demo-definefieldapi","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 Other = _uxcore.Form.OtherFormField;\n\n// 组件拥有 prop `value` 和 `onChange`\n// 当然也可以不叫 `value` 和 `onChange`,只需要根据文档,传入对应的 prop 名即可。\n\nvar DoubleInput = function (_React$Component) {\n _inherits(DoubleInput, _React$Component);\n\n function DoubleInput() {\n _classCallCheck(this, DoubleInput);\n\n return _possibleConstructorReturn(this, (DoubleInput.__proto__ || Object.getPrototypeOf(DoubleInput)).apply(this, arguments));\n }\n\n _createClass(DoubleInput, [{\n key: 'handleChange',\n value: function handleChange(index, e) {\n var me = this;\n var value = e.currentTarget.value;\n var stateValue = (me.props.value || []).slice(0);\n stateValue[index] = value;\n me.props.onChange(stateValue);\n }\n }, {\n key: 'render',\n value: function render() {\n var me = this;\n var arr = [];\n var values = me.props.value || [];\n arr.push(React.createElement('input', { className: 'kuma-input', value: values[0], onChange: me.handleChange.bind(me, 0), key: 'input1' }));\n arr.push(React.createElement(\n 'span',\n { className: 'split', key: 'split' },\n '-'\n ));\n arr.push(React.createElement('input', { className: 'kuma-input', value: values[1], onChange: me.handleChange.bind(me, 1), key: 'input2' }));\n return React.createElement(\n 'div',\n null,\n arr\n );\n }\n }]);\n\n return DoubleInput;\n}(React.Component);\n\nvar DoubleInputFormField = _uxcore.Form.createFormField({\n component: React.createElement(DoubleInput, null),\n fieldName: 'DoubleInputFormField'\n});\n\nvar Demo = function (_React$Component2) {\n _inherits(Demo, _React$Component2);\n\n function Demo(props) {\n _classCallCheck(this, Demo);\n\n var _this2 = _possibleConstructorReturn(this, (Demo.__proto__ || Object.getPrototypeOf(Demo)).call(this, props));\n\n _this2.state = {\n values: {}\n };\n return _this2;\n }\n\n _createClass(Demo, [{\n key: 'handleClick',\n value: function handleClick() {\n var me = this;\n alert(JSON.stringify(me.refs.form.getValues()));\n }\n }, {\n key: 'handleReset',\n value: function handleReset() {\n var me = this;\n me.refs.form.resetValues();\n }\n }, {\n key: 'render',\n value: function render() {\n var me = this;\n\n // FormField 通用的 props 都可以使用。\n return React.createElement(\n 'div',\n { className: 'page-demo3' },\n React.createElement(\n _uxcore.Form,\n { ref: 'form', jsxvalues: {\n double: [2, 4]\n }\n },\n React.createElement(DoubleInputFormField, { jsxlabel: '\\u53CC\\u8F93\\u5165\\u6846', jsxname: 'double', className: 'kuma-doubleinput-uxform-field' }),\n React.createElement(\n Other,\n { className: 'other' },\n React.createElement(\n _uxcore.Button,\n { onClick: me.handleClick.bind(this) },\n '\\u786E\\u5B9A'\n )\n )\n )\n );\n }\n }]);\n\n return Demo;\n}(React.Component);\n\nReactDOM.render(React.createElement(Demo, null), document.getElementById('scene-commonform-demo-definefieldapi'));\n\n/* JS END CSS START*/})()</script><div class=\"highlight\"><pre><code class=\"javascript\">\n<span class=\"hljs-keyword\">import</span> { Form } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n<span class=\"hljs-keyword\">import</span> { Button } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n\n<span class=\"hljs-keyword\">const</span> {\n OtherFormField: Other,\n} = Form;\n\n<span class=\"hljs-comment\">// 组件拥有 prop `value` 和 `onChange`</span>\n<span class=\"hljs-comment\">// 当然也可以不叫 `value` 和 `onChange`,只需要根据文档,传入对应的 prop 名即可。</span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">DoubleInput</span> <span class=\"hljs-keyword\">extends</span> <span class=\"hljs-title\">React</span>.<span class=\"hljs-title\">Component</span> </span>{\n handleChange(index, e) {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> value = e.currentTarget.value;\n <span class=\"hljs-keyword\">const</span> stateValue = (me.props.value || []).slice(<span class=\"hljs-number\">0</span>);\n stateValue[index] = value;\n me.props.onChange(stateValue);\n }\n render() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> arr = [];\n <span class=\"hljs-keyword\">const</span> values = me.props.value || [];\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">input</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"kuma-input\"</span> <span class=\"hljs-attribute\">value</span>=<span class=\"hljs-value\">{values[0]}</span> <span class=\"hljs-attribute\">onChange</span>=<span class=\"hljs-value\">{me.handleChange.bind(me,</span> <span class=\"hljs-attribute\">0</span>)} <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"input1\"</span> /&gt;</span>)</span>;\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">span</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"split\"</span> <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"split\"</span>&gt;</span>-<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">span</span>&gt;</span>)</span>;\n arr.push(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">input</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"kuma-input\"</span> <span class=\"hljs-attribute\">value</span>=<span class=\"hljs-value\">{values[1]}</span> <span class=\"hljs-attribute\">onChange</span>=<span class=\"hljs-value\">{me.handleChange.bind(me,</span> <span class=\"hljs-attribute\">1</span>)} <span class=\"hljs-attribute\">key</span>=<span class=\"hljs-value\">\"input2\"</span> /&gt;</span>)</span>;\n <span class=\"hljs-keyword\">return</span> (\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">div</span>&gt;</span>{arr}<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">div</span>&gt;</span>\n )</span>;\n }\n}\n\n<span class=\"hljs-keyword\">const</span> DoubleInputFormField = Form.createFormField({\n component: <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">DoubleInput</span> /&gt;</span>,\n fieldName: 'DoubleInputFormField',\n});\n\nclass Demo extends React.Component {\n constructor(props) {\n super(props);\n this.state = {\n values: {},\n };\n }\n\n handleClick() {\n const me = this;\n alert(JSON.stringify(me.refs.form.getValues()));\n }\n\n handleReset() {\n const me = this;\n me.refs.form.resetValues();\n }\n\n render() {\n const me = this;\n\n // FormField 通用的 props 都可以使用。\n return (\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">div</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"page-demo3\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Form</span> <span class=\"hljs-attribute\">ref</span>=<span class=\"hljs-value\">\"form\"</span> <span class=\"hljs-attribute\">jsxvalues</span>=<span class=\"hljs-value\">{{</span>\n <span class=\"hljs-attribute\">double:</span> [<span class=\"hljs-attribute\">2</span>, <span class=\"hljs-attribute\">4</span>],\n }}\n &gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">DoubleInputFormField</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"双输入框\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"double\"</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"kuma-doubleinput-uxform-field\"</span> /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Other</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"other\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick.bind(this)}</span>&gt;</span>确定<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Button</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Form</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">div</span>&gt;</span>\n )</span>;\n }\n\n}\n\nReactDOM.render(\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Demo</span> /&gt;</span>\n, document.getElementById('scene-commonform-demo-definefieldapi'));\n\n/* JS END CSS START*/</span></code></pre></div><style type=\"text/css\">.page-demo3 .other {\n padding-left: 100px\n}\n.page-demo3 .kuma-doubleinput-uxform-field .kuma-input {\n width: 200px\n}\n.page-demo3 .kuma-doubleinput-uxform-field .split {\n margin: 0 5px;\n}\n.page-demo3 .kuma-button {\n margin-right: 10px;\n}</style><div class=\"highlight\"><pre><code class=\"css\"><span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.other</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">padding-left</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">100px</span>\n</span></span></span>}\n<span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.kuma-doubleinput-uxform-field</span> <span class=\"hljs-class\">.kuma-input</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">width</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">200px</span>\n</span></span></span>}\n<span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.kuma-doubleinput-uxform-field</span> <span class=\"hljs-class\">.split</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">margin</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">0</span> <span class=\"hljs-number\">5px</span></span></span>;\n}</span>\n<span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.kuma-button</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">margin-right</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">10px</span></span></span>;\n}</span></code></pre></div>"},"status":"public","toc":""},{"title":"表单的扩展3 -- 一些表单装饰物的使用","id":"scene-commonform-demo-decoration","tags":[],"filepath":"site/scene/commonForm/demo/decoration.md","directory":"scene/commonForm/demo","filename":"decoration","meta":{"title":"表单的扩展3 -- 一些表单装饰物的使用","description":"\n<p>在表单中,一些不需要表单交互的模块,我们称之为装饰物,例如按钮、观看用的表格等等,<br>这个场景演示了如何在 Form 中自定义地定义装饰物的。</p>\n","order":"3","filepath":"site/scene/commonForm/demo/decoration.md","filename":"decoration","directory":"scene/commonForm/demo","id":"scene-commonform-demo-decoration","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 Input = _uxcore.Form.InputFormField,\n FormRow = _uxcore.Form.FormRow,\n Validators = _uxcore.Form.Validators,\n Other = _uxcore.Form.OtherFormField;\n\nvar Demo = function (_React$Component) {\n _inherits(Demo, _React$Component);\n\n function Demo(props) {\n _classCallCheck(this, Demo);\n\n var _this = _possibleConstructorReturn(this, (Demo.__proto__ || Object.getPrototypeOf(Demo)).call(this, props));\n\n _this.state = {\n values: {}\n };\n return _this;\n }\n\n _createClass(Demo, [{\n key: 'handleClick',\n value: function handleClick() {\n var me = this;\n alert(JSON.stringify(me.form.getValues(true)));\n }\n }, {\n key: 'render',\n value: function render() {\n var _this2 = this;\n\n var me = this;\n var tableProps = {\n width: 700,\n jsxdata: {\n data: [{\n name: '小王',\n pinyin: 'xiaowang'\n }, {\n name: '小李',\n pinyin: 'xiaoli'\n }]\n },\n jsxcolumns: [{ dataKey: 'name', title: '姓名', width: 300 }, { dataKey: 'pinyin', title: '拼音', width: 300 }]\n };\n return React.createElement(\n 'div',\n { className: 'page-demo3' },\n React.createElement(\n _uxcore.Form,\n { jsxvalues: me.state.values, ref: function ref(c) {\n _this2.form = c;\n } },\n React.createElement(\n FormRow,\n null,\n React.createElement(Input, { jsxlabel: 'A', jsxname: 'a', jsxrules: { validator: Validators.isNum, errMsg: '必须为数字' } }),\n React.createElement(\n Other,\n null,\n React.createElement(\n 'span',\n { className: 'yuan' },\n '\\u5143'\n )\n )\n ),\n React.createElement(\n Other,\n { className: 'other' },\n React.createElement(_uxcore.Table, tableProps),\n React.createElement(\n _uxcore.Button,\n { onClick: me.handleClick.bind(me), className: 'confirmButton' },\n '\\u786E\\u5B9A'\n )\n )\n )\n );\n }\n }]);\n\n return Demo;\n}(React.Component);\n\nReactDOM.render(React.createElement(Demo, null), document.getElementById('scene-commonform-demo-decoration'));\n\n/* JS END CSS START*/})()</script><div class=\"highlight\"><pre><code class=\"javascript\">\n<span class=\"hljs-keyword\">import</span> { Form } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n<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> { Table } <span class=\"hljs-keyword\">from</span> <span class=\"hljs-string\">'uxcore'</span>;\n\n<span class=\"hljs-keyword\">const</span> {\n InputFormField: Input,\n FormRow,\n Validators,\n OtherFormField: Other,\n} = Form;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">Demo</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 values: {},\n };\n }\n\n handleClick() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n alert(<span class=\"hljs-built_in\">JSON</span>.stringify(me.form.getValues(<span class=\"hljs-literal\">true</span>)));\n }\n\n render() {\n <span class=\"hljs-keyword\">const</span> me = <span class=\"hljs-keyword\">this</span>;\n <span class=\"hljs-keyword\">const</span> tableProps = {\n width: <span class=\"hljs-number\">700</span>,\n jsxdata: {\n data: [\n {\n name: <span class=\"hljs-string\">'小王'</span>,\n pinyin: <span class=\"hljs-string\">'xiaowang'</span>,\n },\n {\n name: <span class=\"hljs-string\">'小李'</span>,\n pinyin: <span class=\"hljs-string\">'xiaoli'</span>,\n },\n ],\n },\n jsxcolumns: [\n { dataKey: <span class=\"hljs-string\">'name'</span>, title: <span class=\"hljs-string\">'姓名'</span>, width: <span class=\"hljs-number\">300</span> },\n { dataKey: <span class=\"hljs-string\">'pinyin'</span>, title: <span class=\"hljs-string\">'拼音'</span>, width: <span class=\"hljs-number\">300</span> },\n ],\n };\n <span class=\"hljs-keyword\">return</span> (\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">div</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"page-demo3\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Form</span> <span class=\"hljs-attribute\">jsxvalues</span>=<span class=\"hljs-value\">{me.state.values}</span> <span class=\"hljs-attribute\">ref</span>=<span class=\"hljs-value\">{(c)</span> =&gt;</span> {this.form = c;}}&gt;\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">FormRow</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Input</span> <span class=\"hljs-attribute\">jsxlabel</span>=<span class=\"hljs-value\">\"A\"</span> <span class=\"hljs-attribute\">jsxname</span>=<span class=\"hljs-value\">\"a\"</span> <span class=\"hljs-attribute\">jsxrules</span>=<span class=\"hljs-value\">{{</span> <span class=\"hljs-attribute\">validator:</span> <span class=\"hljs-attribute\">Validators.isNum</span>, <span class=\"hljs-attribute\">errMsg:</span> '必须为数字' }} /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">span</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"yuan\"</span>&gt;</span>元<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">span</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">FormRow</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Other</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"other\"</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Table</span> {<span class=\"hljs-attribute\">...tableProps</span>} /&gt;</span>\n <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Button</span> <span class=\"hljs-attribute\">onClick</span>=<span class=\"hljs-value\">{me.handleClick.bind(me)}</span> <span class=\"hljs-attribute\">className</span>=<span class=\"hljs-value\">\"confirmButton\"</span>&gt;</span>确定<span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Button</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Other</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">Form</span>&gt;</span>\n <span class=\"hljs-tag\">&lt;/<span class=\"hljs-title\">div</span>&gt;</span>\n )</span>;\n }\n}\n\nReactDOM.render(\n <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">Demo</span> /&gt;</span>\n, document.getElementById('scene-commonform-demo-decoration'));\n\n/* JS END CSS START*/</span></code></pre></div><style type=\"text/css\">.page-demo3 .other {\n padding-left: 88px\n}\n\n.page-demo3 .confirmButton {\n margin-top: 10px;\n}\n\n.page-demo3 .yuan {\n line-height: 34px;\n}</style><div class=\"highlight\"><pre><code class=\"css\"><span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.other</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">padding-left</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">88px</span>\n</span></span></span>}\n\n<span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.confirmButton</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">margin-top</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">10px</span></span></span>;\n}</span>\n\n<span class=\"hljs-class\">.page-demo3</span> <span class=\"hljs-class\">.yuan</span> <span class=\"hljs-rules\">{\n <span class=\"hljs-rule\"><span class=\"hljs-attribute\">line-height</span>:<span class=\"hljs-value\"> <span class=\"hljs-number\">34px</span></span></span>;\n}</span></code></pre></div>"},"status":"public","toc":""}]

import { Form } from 'uxcore';
import { Button } from 'uxcore';

const {
    InputFormField: Input,
    SelectFormField: Select,
    CheckboxGroupFormField: Check,
    Validators,
    OtherFormField: Other,
} = Form;

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      values: {},
    };
  }

  handleFieldChange(value, name, pass) {
    const names = ['a', 'b'];
    if (pass && names.indexOf(name) !== -1) {
      const me = this;
      const data = me.refs.form.getValues();
      const values = data.values;
      me.refs.form.setValues({
        total: parseFloat(values.a || 0) + parseFloat(values.b || 0),
      });
    }
  }

  handleClick() {
    const me = this;
    alert(JSON.stringify(me.refs.form.getValues(true)));
  }

  render() {
    const me = this;

    return (
      <div className="page-demo1">
        <Form jsxonChange={me.handleFieldChange.bind(me)} jsxvalues={me.state.values} ref="form" instantValidate={false}>
          <Input jsxlabel="A" jsxname="a" jsxrules={{ validator: Validators.isNum, errMsg: '必须为数字' }} />
          <Input jsxlabel="B" jsxname="b" jsxrules={{ validator: Validators.isNum, errMsg: '必须为数字' }} />
          <Input jsxlabel="A+B" jsxname="total" />
          <Other className="other">
            <Button onClick={me.handleClick.bind(me)}>确定</Button>
          </Other>
        </Form>
      </div>
    );
  }

}

ReactDOM.render(
  <Demo />
, document.getElementById('scene-commonform-demo-basic'));

/* JS END CSS START*/
.other {
    padding-left: 100px
}

通过继承 Form.FormField,用户可以自己定义特制的 FormField,以满足各种业务上的需求。
定制的 FormField 同样可以使用 Form 带来通用配置,如布局、样式、校验、值的管理等等。


import { Form } from 'uxcore';
import { Button } from 'uxcore';
import deepcopy from 'deepcopy';;
import assign from 'object-assign';;
import { FormField } from 'uxcore';

const {
    OtherFormField: Other,
} = Form;

class DoubleInputFormField extends FormField {
  constructor(props) {
    super(props);
        // 这里不要直接覆盖 this.state = {};
        // 如果希望增加 state,使用扩展。
        // this.state = assign({}, this.state, {xxx: 1})
  }

    // 改写 addSpecificClass,增加该 FormField 专属的 class

  addSpecificClass() {
    return 'kuma-doubleinput-uxform-field';
  }


    // 在合适的时机调用 me.handleDataChange,将变动通知给 Form

  handleChange(index, e) {
    const me = this;
    const value = e.currentTarget.value;
    const stateValue = me.state.value || [];
    stateValue[index] = value;
    me.handleDataChange(stateValue);
  }

    // 改写 renderField,决定渲染方式

  renderField() {
    const me = this;
    const arr = [];
        // 这里注意取值使用 state.value,完成和 Form 的通信。
    const values = me.state.value || [];
    arr.push(<input className="kuma-input" value={values[0]} onChange={me.handleChange.bind(me, 0)} key="input1" />);
    arr.push(<span className="split" key="split">-</span>);
    arr.push(<input className="kuma-input" value={values[1]} onChange={me.handleChange.bind(me, 1)} key="input2" />);
    return arr;
  }

}

DoubleInputFormField.displayName = 'DoubleInputFormField'; // 重要,必须 displayName 中带有 FormField 才可以被识别为 FormField。

// 如果你的代码需要兼容 IE,以下两行不可缺少。
// 即使不需要兼容 IE,也不可直接覆盖,如 DoubleInputFormField.defaultProps = {};
DoubleInputFormField.defaultProps = assign({}, FormField.defaultProps);
DoubleInputFormField.propTypes = assign({}, FormField.propTypes);

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      values: {},
    };
  }

  handleClick() {
    const me = this;
    alert(JSON.stringify(me.refs.form.getValues()));
  }

  handleReset() {
    const me = this;
    me.refs.form.resetValues();
  }

  render() {
    const me = this;

        // FormField 通用的 props 都可以使用。
    return (
      <div className="page-demo2">
        <Form ref="form" jsxvalues={{
          double: [2, 4],
        }}
        >
          <DoubleInputFormField jsxlabel="双输入框" jsxname="double" />
          <Other className="other">
            <Button onClick={me.handleClick.bind(this)}>确定</Button>
          </Other>
        </Form>
      </div>
    );
  }

}

ReactDOM.render(
  <Demo />
, document.getElementById('scene-commonform-demo-definefield'));

/* JS END CSS START*/
.page-demo2 .other {
    padding-left: 100px
}
.page-demo2 .kuma-doubleinput-uxform-field .kuma-input {
    width: 200px
}
.page-demo2 .kuma-doubleinput-uxform-field .split {
    margin: 0 5px;
}
.page-demo2 .kuma-button {
    margin-right: 10px;
}

通过继承 Form.createFormField API,用户可以快速地将自己的业务组件封装成 FormField,以满足各种业务上的需求。
定制的 FormField 同样可以使用 Form 带来通用配置,如布局、样式、校验、值的管理等等。
可以与上面的例子进行一个对比,感受一下之间的不同性,这种方式没有扩展 FormField 的方式定制,但相比较起来封装更加简单。


import { Form } from 'uxcore';
import { Button } from 'uxcore';

const {
    OtherFormField: Other,
} = Form;

// 组件拥有 prop `value` 和 `onChange`
// 当然也可以不叫 `value` 和 `onChange`,只需要根据文档,传入对应的 prop 名即可。

class DoubleInput extends React.Component {
  handleChange(index, e) {
    const me = this;
    const value = e.currentTarget.value;
    const stateValue = (me.props.value || []).slice(0);
    stateValue[index] = value;
    me.props.onChange(stateValue);
  }
  render() {
    const me = this;
    const arr = [];
    const values = me.props.value || [];
    arr.push(<input className="kuma-input" value={values[0]} onChange={me.handleChange.bind(me, 0)} key="input1" />);
    arr.push(<span className="split" key="split">-</span>);
    arr.push(<input className="kuma-input" value={values[1]} onChange={me.handleChange.bind(me, 1)} key="input2" />);
    return (
      <div>{arr}</div>
    );
  }
}

const DoubleInputFormField = Form.createFormField({
  component: <DoubleInput />,
  fieldName: 'DoubleInputFormField',
});

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      values: {},
    };
  }

  handleClick() {
    const me = this;
    alert(JSON.stringify(me.refs.form.getValues()));
  }

  handleReset() {
    const me = this;
    me.refs.form.resetValues();
  }

  render() {
    const me = this;

        // FormField 通用的 props 都可以使用。
    return (
      <div className="page-demo3">
        <Form ref="form" jsxvalues={{
          double: [2, 4],
        }}
        >
          <DoubleInputFormField jsxlabel="双输入框" jsxname="double" className="kuma-doubleinput-uxform-field" />
          <Other className="other">
            <Button onClick={me.handleClick.bind(this)}>确定</Button>
          </Other>
        </Form>
      </div>
    );
  }

}

ReactDOM.render(
  <Demo />
, document.getElementById('scene-commonform-demo-definefieldapi'));

/* JS END CSS START*/
.page-demo3 .other {
    padding-left: 100px
}
.page-demo3 .kuma-doubleinput-uxform-field .kuma-input {
    width: 200px
}
.page-demo3 .kuma-doubleinput-uxform-field .split {
    margin: 0 5px;
}
.page-demo3 .kuma-button {
    margin-right: 10px;
}

在表单中,一些不需要表单交互的模块,我们称之为装饰物,例如按钮、观看用的表格等等,
这个场景演示了如何在 Form 中自定义地定义装饰物的。


import { Form } from 'uxcore';
import { Button } from 'uxcore';
import { Table } from 'uxcore';

const {
  InputFormField: Input,
  FormRow,
  Validators,
  OtherFormField: Other,
} = Form;

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      values: {},
    };
  }

  handleClick() {
    const me = this;
    alert(JSON.stringify(me.form.getValues(true)));
  }

  render() {
    const me = this;
    const tableProps = {
      width: 700,
      jsxdata: {
        data: [
          {
            name: '小王',
            pinyin: 'xiaowang',
          },
          {
            name: '小李',
            pinyin: 'xiaoli',
          },
        ],
      },
      jsxcolumns: [
        { dataKey: 'name', title: '姓名', width: 300 },
        { dataKey: 'pinyin', title: '拼音', width: 300 },
      ],
    };
    return (
      <div className="page-demo3">
        <Form jsxvalues={me.state.values} ref={(c) => {this.form = c;}}>
          <FormRow>
            <Input jsxlabel="A" jsxname="a" jsxrules={{ validator: Validators.isNum, errMsg: '必须为数字' }} />
            <Other>
              <span className="yuan"></span>
            </Other>
          </FormRow>
          <Other className="other">
            <Table {...tableProps} />
            <Button onClick={me.handleClick.bind(me)} className="confirmButton">确定</Button>
          </Other>
        </Form>
      </div>
    );
  }
}

ReactDOM.render(
  <Demo />
, document.getElementById('scene-commonform-demo-decoration'));

/* JS END CSS START*/
.page-demo3 .other {
    padding-left: 88px
}

.page-demo3 .confirmButton {
    margin-top: 10px;
}

.page-demo3 .yuan {
    line-height: 34px;
}