UNPKG

antd-mobile-taro-ui

Version:

以antd-mobile为设计标准,基于taro框架的微信小程序组件库

46 lines (40 loc) 1.23 kB
import { __rest } from "tslib"; import React, { useContext } from 'react'; import { Field } from 'rc-field-form'; import formRules from './FormRules'; import { FormContext } from './context'; const FormItem = props => { const { label, name = '', rules = [], children } = props, itemProps = __rest(props, ["label", "name", "rules", "children"]); const { value } = useContext(FormContext); const rule = rules === null || rules === void 0 ? void 0 : rules.map(item => { if (item instanceof Function) return item; if (!!item.type && !!formRules[item.type]) { const fn = formRules[item.type]; Object.assign(item, { validator: () => Promise.resolve(fn(item, value === null || value === void 0 ? void 0 : value[name])), type: 'string' }); } if (item.required) { if (!item.validator) { Object.assign(item, { message: item.message || `请输入${typeof label === 'string' ? label : name !== null && name !== void 0 ? name : ''}` }); } } return item; }); return React.createElement(Field, Object.assign({ rules: rule, name: name }, itemProps), children); }; export default FormItem;