Skip to content

Commit

Permalink
fix(form): ensure that the DateTime component checked properly,fix #511
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Apr 24, 2021
1 parent 16ecf71 commit cb35341
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/components/Form/src/components/FormItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
if (component.includes('Input') || component.includes('Textarea')) {
rule.whitespace = true;
}
setComponentRuleType(rule, component);
const valueFormat = unref(getComponentsProps)?.valueFormat;
setComponentRuleType(rule, component, valueFormat);
}
}
Expand Down Expand Up @@ -203,9 +203,7 @@
if (propsData[eventKey]) {
propsData[eventKey](e);
}
const target = e ? e.target : null;
const value = target ? (isCheck ? target.checked : target.value) : e;
props.setFormModel(field, value);
},
Expand Down
24 changes: 20 additions & 4 deletions src/components/Form/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ValidationRule } from 'ant-design-vue/lib/form/Form';
import type { ComponentType } from './types/index';
import { useI18n } from '/@/hooks/web/useI18n';
import { isNumber } from '/@/utils/is';
import { dateUtil } from '/@/utils/dateUtil';
import { isNumber, isObject } from '/@/utils/is';

const { t } = useI18n();

Expand All @@ -28,20 +29,35 @@ export function createPlaceholderMessage(component: ComponentType) {
return '';
}

const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'];

function genType() {
return ['DatePicker', 'MonthPicker', 'RangePicker', 'WeekPicker', 'TimePicker'];
return [...DATE_TYPE, 'RangePicker'];
}

export function setComponentRuleType(rule: ValidationRule, component: ComponentType) {
export function setComponentRuleType(
rule: ValidationRule,
component: ComponentType,
valueFormat: string
) {
if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) {
rule.type = 'object';
rule.type = valueFormat ? 'string' : 'object';
} else if (['RangePicker', 'Upload', 'CheckboxGroup', 'TimePicker'].includes(component)) {
rule.type = 'array';
} else if (['InputNumber'].includes(component)) {
rule.type = 'number';
}
}

export function processDateValue(attr: Recordable, component: string) {
const { valueFormat, value } = attr;
if (valueFormat) {
attr.value = isObject(value) ? dateUtil(value).format(valueFormat) : value;
} else if (DATE_TYPE.includes(component) && value) {
attr.value = dateUtil(attr.value);
}
}

export function handleInputNumberValue(component?: ComponentType, val?: any) {
if (!component) return val;
if (['Input', 'InputPassword', 'InputSearch', 'InputTextArea'].includes(component)) {
Expand Down
7 changes: 6 additions & 1 deletion src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export function useFormEvents({
}
formModel[key] = arr;
} else {
formModel[key] = value ? dateUtil(value) : null;
const { componentProps } = schema || {};
let _props = componentProps as any;
if (typeof componentProps === 'function') {
_props = _props();
}
formModel[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null;
}
} else {
formModel[key] = value;
Expand Down
16 changes: 15 additions & 1 deletion src/views/demo/form/RuleForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
},
required: true,
},
{
field: 'field33',
component: 'DatePicker',
label: '字段33',
colProps: {
span: 8,
},
componentProps: {
valueFormat: 'YYYY-MM-DD',
},
rules: [{ required: true, type: 'string' }],
},
{
field: 'field44',
component: 'InputCountDown',
Expand Down Expand Up @@ -95,7 +107,7 @@
],
},
{
field: 'field44',
field: 'field441',
component: 'Input',
label: '自定义校验',
colProps: {
Expand Down Expand Up @@ -198,6 +210,8 @@
field1: 1111,
field5: ['1'],
field7: '1',
field33: '2020-12-12',
field3: '2020-12-12',
});
}
return {
Expand Down

0 comments on commit cb35341

Please sign in to comment.