liuzhidong
2021-05-25 785732f438916d7767ad44789c16216a6f6505a8
提交 | 用户 | age
4ff1c4 1 import type { ValidationRule } from 'ant-design-vue/lib/form/Form';
84c9d7 2 import type { ComponentType } from './types/index';
dc09de 3 import { useI18n } from '/@/hooks/web/useI18n';
cb3534 4 import { dateUtil } from '/@/utils/dateUtil';
V 5 import { isNumber, isObject } from '/@/utils/is';
dc09de 6
962f90 7 const { t } = useI18n();
84c9d7 8
2f6253 9 /**
10  * @description: 生成placeholder
11  */
12 export function createPlaceholderMessage(component: ComponentType) {
13   if (component.includes('Input') || component.includes('Complete')) {
efbde0 14     return t('common.inputText');
2f6253 15   }
0b6110 16   if (component.includes('Picker')) {
efbde0 17     return t('common.chooseText');
2f6253 18   }
19   if (
20     component.includes('Select') ||
21     component.includes('Cascader') ||
22     component.includes('Checkbox') ||
23     component.includes('Radio') ||
24     component.includes('Switch')
25   ) {
26     // return `请选择${label}`;
efbde0 27     return t('common.chooseText');
2f6253 28   }
29   return '';
30 }
84c9d7 31
cb3534 32 const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'];
V 33
2f6253 34 function genType() {
cb3534 35   return [...DATE_TYPE, 'RangePicker'];
2f6253 36 }
84c9d7 37
cb3534 38 export function setComponentRuleType(
V 39   rule: ValidationRule,
40   component: ComponentType,
41   valueFormat: string
42 ) {
4ff1c4 43   if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) {
cb3534 44     rule.type = valueFormat ? 'string' : 'object';
4ff1c4 45   } else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) {
V 46     rule.type = 'array';
47   } else if (['InputNumber'].includes(component)) {
48     rule.type = 'number';
49   }
50 }
51
cb3534 52 export function processDateValue(attr: Recordable, component: string) {
V 53   const { valueFormat, value } = attr;
54   if (valueFormat) {
55     attr.value = isObject(value) ? dateUtil(value).format(valueFormat) : value;
56   } else if (DATE_TYPE.includes(component) && value) {
57     attr.value = dateUtil(attr.value);
58   }
59 }
60
c9089c 61 export function handleInputNumberValue(component?: ComponentType, val?: any) {
ac1a36 62   if (!component) return val;
V 63   if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) {
64     return val && isNumber(val) ? `${val}` : val;
65   }
66   return val;
67 }
68
2f6253 69 /**
70  * 时间字段
71  */
72 export const dateItemType = genType();