UNPKG

11.2 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.default = undefined;
5
6var _extends2 = require('babel-runtime/helpers/extends');
7
8var _extends3 = _interopRequireDefault(_extends2);
9
10var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
11
12var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
13
14var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
15
16var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
17
18var _inherits2 = require('babel-runtime/helpers/inherits');
19
20var _inherits3 = _interopRequireDefault(_inherits2);
21
22var _typeof2 = require('babel-runtime/helpers/typeof');
23
24var _typeof3 = _interopRequireDefault(_typeof2);
25
26var _class, _temp;
27
28var _react = require('react');
29
30var _react2 = _interopRequireDefault(_react);
31
32var _propTypes = require('prop-types');
33
34var _propTypes2 = _interopRequireDefault(_propTypes);
35
36var _classnames = require('classnames');
37
38var _classnames2 = _interopRequireDefault(_classnames);
39
40var _util = require('../util');
41
42var _field = require('../field');
43
44var _field2 = _interopRequireDefault(_field);
45
46var _responsiveGrid = require('../responsive-grid');
47
48var _responsiveGrid2 = _interopRequireDefault(_responsiveGrid);
49
50function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
52function pickerDefined(obj) {
53 var newObj = {};
54 Object.keys(obj).forEach(function (i) {
55 if (typeof obj[i] !== 'undefined') {
56 newObj[i] = obj[i];
57 }
58 });
59 return newObj;
60}
61
62function preventDefault(e) {
63 e.preventDefault();
64}
65var getNewChildren = function getNewChildren(children, props) {
66 var size = props.size,
67 device = props.device,
68 labelAlign = props.labelAlign,
69 labelTextAlign = props.labelTextAlign,
70 labelCol = props.labelCol,
71 wrapperCol = props.wrapperCol,
72 responsive = props.responsive,
73 colon = props.colon;
74
75
76 return _react2.default.Children.map(children, function (child) {
77 if (_util.obj.isReactFragment(child)) {
78 return getNewChildren(child.props.children, props);
79 }
80
81 if (child && ['function', 'object'].indexOf((0, _typeof3.default)(child.type)) > -1 && child.type._typeMark === 'form_item') {
82 var childrenProps = {
83 labelCol: child.props.labelCol ? child.props.labelCol : labelCol,
84 wrapperCol: child.props.wrapperCol ? child.props.wrapperCol : wrapperCol,
85 labelAlign: child.props.labelAlign ? child.props.labelAlign : device === 'phone' ? 'top' : labelAlign,
86 labelTextAlign: child.props.labelTextAlign ? child.props.labelTextAlign : labelTextAlign,
87 colon: 'colon' in child.props ? child.props.colon : colon,
88 size: child.props.size ? child.props.size : size,
89 responsive: responsive
90 };
91 return _react2.default.cloneElement(child, pickerDefined(childrenProps));
92 }
93 return child;
94 });
95};
96
97/** Form */
98var Form = (_temp = _class = function (_React$Component) {
99 (0, _inherits3.default)(Form, _React$Component);
100
101 function Form(props) {
102 (0, _classCallCheck3.default)(this, Form);
103
104 var _this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call(this, props));
105
106 _this.onChange = function (name, value) {
107 _this.props.onChange(_this._formField.getValues(), {
108 name: name,
109 value: value,
110 field: _this._formField
111 });
112 };
113
114 _this._formField = null;
115 if (props.field !== false) {
116 var options = (0, _extends3.default)({}, props.fieldOptions, {
117 onChange: _this.onChange
118 });
119
120 if (props.field) {
121 _this._formField = props.field;
122 var onChange = _this._formField.options.onChange;
123 options.onChange = _util.func.makeChain(onChange, _this.onChange);
124 _this._formField.setOptions && _this._formField.setOptions(options);
125 } else {
126 if ('value' in props) {
127 options.values = props.value;
128 }
129
130 _this._formField = new _field2.default(_this, options);
131 }
132
133 if (props.locale && props.locale.Validate) {
134 _this._formField.setOptions({ messages: props.locale.Validate });
135 }
136
137 props.saveField(_this._formField);
138 }
139 return _this;
140 }
141
142 Form.prototype.getChildContext = function getChildContext() {
143 return {
144 _formField: this.props.field ? this.props.field : this._formField,
145 _formSize: this.props.size,
146 _formDisabled: this.props.disabled,
147 _formPreview: this.props.isPreview,
148 _formFullWidth: this.props.fullWidth,
149 _formLabelForErrorMessage: this.props.useLabelForErrorMessage
150 };
151 };
152
153 Form.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
154 var props = this.props;
155
156 if (this._formField) {
157 if ('value' in props && props.value !== prevProps.value) {
158 this._formField.setValues(props.value);
159 }
160 if ('error' in props && props.error !== prevProps.error) {
161 this._formField.setValues(props.error);
162 }
163 }
164 };
165
166 Form.prototype.render = function render() {
167 var _classNames;
168
169 var _props = this.props,
170 className = _props.className,
171 inline = _props.inline,
172 size = _props.size,
173 device = _props.device,
174 labelAlign = _props.labelAlign,
175 labelTextAlign = _props.labelTextAlign,
176 onSubmit = _props.onSubmit,
177 children = _props.children,
178 labelCol = _props.labelCol,
179 wrapperCol = _props.wrapperCol,
180 style = _props.style,
181 prefix = _props.prefix,
182 rtl = _props.rtl,
183 isPreview = _props.isPreview,
184 Tag = _props.component,
185 responsive = _props.responsive,
186 gap = _props.gap,
187 colon = _props.colon;
188
189
190 var formClassName = (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'form'] = true, _classNames[prefix + 'inline'] = inline, _classNames['' + prefix + size] = size, _classNames[prefix + 'form-responsive-grid'] = responsive, _classNames[prefix + 'form-preview'] = isPreview, _classNames[className] = !!className, _classNames));
191
192 var newChildren = getNewChildren(children, this.props);
193
194 return _react2.default.createElement(
195 Tag,
196 (0, _extends3.default)({
197 role: 'grid'
198 }, _util.obj.pickOthers(Form.propTypes, this.props), {
199 className: formClassName,
200 style: style,
201 dir: rtl ? 'rtl' : undefined,
202 onSubmit: onSubmit
203 }),
204 responsive ? _react2.default.createElement(
205 _responsiveGrid2.default,
206 { gap: gap },
207 newChildren
208 ) : newChildren
209 );
210 };
211
212 return Form;
213}(_react2.default.Component), _class.propTypes = {
214 /**
215 * 样式前缀
216 */
217 prefix: _propTypes2.default.string,
218 /**
219 * 内联表单
220 */
221 inline: _propTypes2.default.bool,
222 /**
223 * 单个 Item 的 size 自定义,优先级高于 Form 的 size, 并且当组件与 Item 一起使用时,组件自身设置 size 属性无效。
224 * @enumdesc 大, 中, 小
225 */
226 size: _propTypes2.default.oneOf(['large', 'medium', 'small']),
227 /**
228 * 单个 Item 中表单类组件宽度是否是100%
229 */
230 fullWidth: _propTypes2.default.bool,
231 /**
232 * 标签的位置, 如果不设置 labelCol 和 wrapperCol 那么默认是标签在上
233 * @enumdesc 上, 左, 内
234 */
235 labelAlign: _propTypes2.default.oneOf(['top', 'left', 'inset']),
236 /**
237 * 标签的左右对齐方式
238 * @enumdesc 左, 右
239 */
240 labelTextAlign: _propTypes2.default.oneOf(['left', 'right']),
241 /**
242 * field 实例, 传 false 会禁用 field
243 */
244 field: _propTypes2.default.any,
245 /**
246 * 保存 Form 自动生成的 field 对象
247 */
248 saveField: _propTypes2.default.func,
249 /**
250 * 控制第一级 Item 的 labelCol
251 */
252 labelCol: _propTypes2.default.object,
253 /**
254 * 控制第一级 Item 的 wrapperCol
255 */
256 wrapperCol: _propTypes2.default.object,
257 /**
258 * form内有 `htmlType="submit"` 的元素的时候会触发
259 */
260 onSubmit: _propTypes2.default.func,
261 /**
262 * 子元素
263 */
264 children: _propTypes2.default.any,
265 /**
266 * 扩展class
267 */
268 className: _propTypes2.default.string,
269 /**
270 * 自定义内联样式
271 */
272 style: _propTypes2.default.object,
273 /**
274 * 表单数值
275 */
276 value: _propTypes2.default.object,
277 /**
278 * 表单变化回调
279 * @param {Object} values 表单数据
280 * @param {Object} item 详细
281 * @param {String} item.name 变化的组件名
282 * @param {String} item.value 变化的数据
283 * @param {Object} item.field field 实例
284 */
285 onChange: _propTypes2.default.func,
286 /**
287 * 设置标签类型
288 */
289 component: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]),
290 fieldOptions: _propTypes2.default.object,
291 rtl: _propTypes2.default.bool,
292 /**
293 * 预设屏幕宽度
294 */
295 device: _propTypes2.default.oneOf(['phone', 'tablet', 'desktop']),
296 /**
297 * 是否开启内置的响应式布局 (使用ResponsiveGrid)
298 * @version 1.19
299 */
300 responsive: _propTypes2.default.bool,
301 /**
302 * 是否开启预览态
303 * @version 1.19
304 */
305 isPreview: _propTypes2.default.bool,
306 /**
307 * 是否使用 label 替换校验信息的 name 字段
308 * @version 1.20
309 */
310 useLabelForErrorMessage: _propTypes2.default.bool,
311 /**
312 * 表示是否显示 label 后面的冒号
313 * @version 1.22
314 */
315 colon: _propTypes2.default.bool,
316 /**
317 * 是否禁用表单
318 */
319 disabled: _propTypes2.default.bool,
320 // 在 responsive模式下,透传给 ResponsiveGrid的, 表示 每个 cell 之间的间距, [bottom&top, right&left]
321 gap: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.number), _propTypes2.default.number])
322}, _class.defaultProps = {
323 prefix: 'next-',
324 onSubmit: preventDefault,
325 size: 'medium',
326 labelAlign: 'left',
327 onChange: _util.func.noop,
328 component: 'form',
329 saveField: _util.func.noop,
330 device: 'desktop',
331 colon: false,
332 disabled: false
333}, _class.childContextTypes = {
334 _formField: _propTypes2.default.object,
335 _formSize: _propTypes2.default.string,
336 _formDisabled: _propTypes2.default.bool,
337 _formPreview: _propTypes2.default.bool,
338 _formFullWidth: _propTypes2.default.bool,
339 _formLabelForErrorMessage: _propTypes2.default.bool
340}, _temp);
341Form.displayName = 'Form';
342exports.default = Form;
343module.exports = exports['default'];
\No newline at end of file