Commit cb35341b8fd44eb649a79c3a2ae799c7bab8c4f6
1 parent
16ecf718
fix(form): ensure that the DateTime component checked properly,fix #511
Showing
4 changed files
with
43 additions
and
10 deletions
src/components/Form/src/components/FormItem.vue
@@ -170,8 +170,8 @@ | @@ -170,8 +170,8 @@ | ||
170 | if (component.includes('Input') || component.includes('Textarea')) { | 170 | if (component.includes('Input') || component.includes('Textarea')) { |
171 | rule.whitespace = true; | 171 | rule.whitespace = true; |
172 | } | 172 | } |
173 | - | ||
174 | - setComponentRuleType(rule, component); | 173 | + const valueFormat = unref(getComponentsProps)?.valueFormat; |
174 | + setComponentRuleType(rule, component, valueFormat); | ||
175 | } | 175 | } |
176 | } | 176 | } |
177 | 177 | ||
@@ -203,9 +203,7 @@ | @@ -203,9 +203,7 @@ | ||
203 | if (propsData[eventKey]) { | 203 | if (propsData[eventKey]) { |
204 | propsData[eventKey](e); | 204 | propsData[eventKey](e); |
205 | } | 205 | } |
206 | - | ||
207 | const target = e ? e.target : null; | 206 | const target = e ? e.target : null; |
208 | - | ||
209 | const value = target ? (isCheck ? target.checked : target.value) : e; | 207 | const value = target ? (isCheck ? target.checked : target.value) : e; |
210 | props.setFormModel(field, value); | 208 | props.setFormModel(field, value); |
211 | }, | 209 | }, |
src/components/Form/src/helper.ts
1 | import type { ValidationRule } from 'ant-design-vue/lib/form/Form'; | 1 | import type { ValidationRule } from 'ant-design-vue/lib/form/Form'; |
2 | import type { ComponentType } from './types/index'; | 2 | import type { ComponentType } from './types/index'; |
3 | import { useI18n } from '/@/hooks/web/useI18n'; | 3 | import { useI18n } from '/@/hooks/web/useI18n'; |
4 | -import { isNumber } from '/@/utils/is'; | 4 | +import { dateUtil } from '/@/utils/dateUtil'; |
5 | +import { isNumber, isObject } from '/@/utils/is'; | ||
5 | 6 | ||
6 | const { t } = useI18n(); | 7 | const { t } = useI18n(); |
7 | 8 | ||
@@ -28,13 +29,19 @@ export function createPlaceholderMessage(component: ComponentType) { | @@ -28,13 +29,19 @@ export function createPlaceholderMessage(component: ComponentType) { | ||
28 | return ''; | 29 | return ''; |
29 | } | 30 | } |
30 | 31 | ||
32 | +const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker']; | ||
33 | + | ||
31 | function genType() { | 34 | function genType() { |
32 | - return ['DatePicker', 'MonthPicker', 'RangePicker', 'WeekPicker', 'TimePicker']; | 35 | + return [...DATE_TYPE, 'RangePicker']; |
33 | } | 36 | } |
34 | 37 | ||
35 | -export function setComponentRuleType(rule: ValidationRule, component: ComponentType) { | 38 | +export function setComponentRuleType( |
39 | + rule: ValidationRule, | ||
40 | + component: ComponentType, | ||
41 | + valueFormat: string | ||
42 | +) { | ||
36 | if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) { | 43 | if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) { |
37 | - rule.type = 'object'; | 44 | + rule.type = valueFormat ? 'string' : 'object'; |
38 | } else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) { | 45 | } else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) { |
39 | rule.type = 'array'; | 46 | rule.type = 'array'; |
40 | } else if (['InputNumber'].includes(component)) { | 47 | } else if (['InputNumber'].includes(component)) { |
@@ -42,6 +49,15 @@ export function setComponentRuleType(rule: ValidationRule, component: ComponentT | @@ -42,6 +49,15 @@ export function setComponentRuleType(rule: ValidationRule, component: ComponentT | ||
42 | } | 49 | } |
43 | } | 50 | } |
44 | 51 | ||
52 | +export function processDateValue(attr: Recordable, component: string) { | ||
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 | + | ||
45 | export function handleInputNumberValue(component?: ComponentType, val?: any) { | 61 | export function handleInputNumberValue(component?: ComponentType, val?: any) { |
46 | if (!component) return val; | 62 | if (!component) return val; |
47 | if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) { | 63 | if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) { |
src/components/Form/src/hooks/useFormEvents.ts
@@ -73,7 +73,12 @@ export function useFormEvents({ | @@ -73,7 +73,12 @@ export function useFormEvents({ | ||
73 | } | 73 | } |
74 | formModel[key] = arr; | 74 | formModel[key] = arr; |
75 | } else { | 75 | } else { |
76 | - formModel[key] = value ? dateUtil(value) : null; | 76 | + const { componentProps } = schema || {}; |
77 | + let _props = componentProps as any; | ||
78 | + if (typeof componentProps === 'function') { | ||
79 | + _props = _props(); | ||
80 | + } | ||
81 | + formModel[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; | ||
77 | } | 82 | } |
78 | } else { | 83 | } else { |
79 | formModel[key] = value; | 84 | formModel[key] = value; |
src/views/demo/form/RuleForm.vue
@@ -56,6 +56,18 @@ | @@ -56,6 +56,18 @@ | ||
56 | required: true, | 56 | required: true, |
57 | }, | 57 | }, |
58 | { | 58 | { |
59 | + field: 'field33', | ||
60 | + component: 'DatePicker', | ||
61 | + label: '字段33', | ||
62 | + colProps: { | ||
63 | + span: 8, | ||
64 | + }, | ||
65 | + componentProps: { | ||
66 | + valueFormat: 'YYYY-MM-DD', | ||
67 | + }, | ||
68 | + rules: [{ required: true, type: 'string' }], | ||
69 | + }, | ||
70 | + { | ||
59 | field: 'field44', | 71 | field: 'field44', |
60 | component: 'InputCountDown', | 72 | component: 'InputCountDown', |
61 | label: '验证码', | 73 | label: '验证码', |
@@ -95,7 +107,7 @@ | @@ -95,7 +107,7 @@ | ||
95 | ], | 107 | ], |
96 | }, | 108 | }, |
97 | { | 109 | { |
98 | - field: 'field44', | 110 | + field: 'field441', |
99 | component: 'Input', | 111 | component: 'Input', |
100 | label: '自定义校验', | 112 | label: '自定义校验', |
101 | colProps: { | 113 | colProps: { |
@@ -198,6 +210,8 @@ | @@ -198,6 +210,8 @@ | ||
198 | field1: 1111, | 210 | field1: 1111, |
199 | field5: ['1'], | 211 | field5: ['1'], |
200 | field7: '1', | 212 | field7: '1', |
213 | + field33: '2020-12-12', | ||
214 | + field3: '2020-12-12', | ||
201 | }); | 215 | }); |
202 | } | 216 | } |
203 | return { | 217 | return { |