fix(form): daterange不会自动更新

This commit is contained in:
roymondchen 2022-08-08 21:08:24 +08:00 committed by jia000
parent 0a126c1f71
commit a7057d2568
3 changed files with 82 additions and 57 deletions

View File

@ -13,70 +13,91 @@
></el-date-picker> ></el-date-picker>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, PropType, ref } from 'vue'; import { ref, watch } from 'vue';
import { datetimeFormatter } from '@tmagic/utils'; import { datetimeFormatter } from '@tmagic/utils';
import { DaterangeConfig } from '../schema'; import { DaterangeConfig } from '../schema';
import fieldProps from '../utils/fieldProps';
import { useAddField } from '../utils/useAddField'; import { useAddField } from '../utils/useAddField';
export default defineComponent({ const props = defineProps<{
name: 'm-fields-daterange', config: DaterangeConfig;
model: any;
initValues?: any;
values?: any;
name: string;
prop: string;
disabled?: boolean;
size: 'mini' | 'small' | 'medium';
}>();
props: { const emit = defineEmits(['change']);
...fieldProps,
config: {
type: Object as PropType<DaterangeConfig>,
required: true,
},
},
emits: ['change'], useAddField(props.prop);
setup(props, { emit }) { // eslint-disable-next-line vue/no-setup-props-destructure
useAddField(props.prop); const { names } = props.config;
const value = ref<(Date | undefined)[] | null>([]);
// eslint-disable-next-line vue/no-setup-props-destructure if (props.model !== undefined) {
const { names } = props.config; if (names?.length) {
const value = ref<(string | number)[]>([]); watch(
[() => props.model[names[0]], () => props.model[names[1]]],
if (props.model !== undefined) { ([start, end], [preStart, preEnd]) => {
if (names?.length) { if (!value.value) {
value.value = names.map((item) => datetimeFormatter(props.model?.[item], '')); value.value = [];
} else if (props.name && props.model[props.name]) { }
value.value = props.model[props.name].map((item: string) => datetimeFormatter(item, '')); if (!start || !end) value.value = [];
} if (start !== preStart) value.value[0] = new Date(start);
} if (end !== preEnd) value.value[1] = new Date(end);
const setValue = (v: Date[] | Date) => {
if (names?.length && v instanceof Array) {
names.forEach((item, index) => {
if (props.model) {
props.model[item] = datetimeFormatter(v[index].toString(), '');
}
});
} else if (props.model && props.name && v instanceof Date) {
props.model[props.name] = datetimeFormatter(v.toString(), '');
} else if (names?.length) {
names.forEach((item) => {
if (props.model) {
props.model[item] = undefined;
}
});
} else if (props.name) {
props.model[props.name] = undefined;
}
};
return {
value,
changeHandler(v: Date[]) {
setValue(v);
emit('change', v);
}, },
}; {
}, immediate: true,
}); },
);
} else if (props.name && props.model[props.name]) {
watch(
() => props.model[props.name],
(start, preStart) => {
if (start !== preStart) value.value = start.map((item: string) => (item ? new Date(item) : undefined));
},
{
immediate: true,
},
);
}
}
const setValue = (v: Date[] | Date) => {
names?.forEach((item, index) => {
if (!props.model) {
return;
}
if (Array.isArray(v)) {
props.model[item] = datetimeFormatter(v[index]?.toString(), '');
} else {
props.model[item] = undefined;
}
});
};
const changeHandler = (v: Date[]) => {
const value = v || [];
if (props.name) {
emit(
'change',
value.map((item?: Date) => {
if (item) return datetimeFormatter(item, '');
return undefined;
}),
);
} else {
if (names?.length) {
setValue(value);
}
emit('change', value);
}
};
</script> </script>

View File

@ -111,7 +111,7 @@ const install = (app: App, opt: any) => {
app.component(Time.name, Time); app.component(Time.name, Time);
app.component(Checkbox.name, Checkbox); app.component(Checkbox.name, Checkbox);
app.component(Switch.name, Switch); app.component(Switch.name, Switch);
app.component(Daterange.name, Daterange); app.component('m-fields-daterange', Daterange);
app.component('m-fields-color-picker', ColorPicker); app.component('m-fields-color-picker', ColorPicker);
app.component(CheckboxGroup.name, CheckboxGroup); app.component(CheckboxGroup.name, CheckboxGroup);
app.component(RadioGroup.name, RadioGroup); app.component(RadioGroup.name, RadioGroup);

View File

@ -34,7 +34,11 @@ export const sleep = (ms: number): Promise<void> =>
}, ms); }, ms);
}); });
export const datetimeFormatter = (v: string | Date, defaultValue = '-', format = 'YYYY-MM-DD HH:mm:ss'): any => { export const datetimeFormatter = (
v: string | Date,
defaultValue = '-',
format = 'YYYY-MM-DD HH:mm:ss',
): string | number => {
if (v) { if (v) {
let time = null; let time = null;
if (['x', 'timestamp'].includes(format)) { if (['x', 'timestamp'].includes(format)) {