修改 mjs 文件转化为 js
This commit is contained in:
parent
9299bbac15
commit
d25305aa0c
69
components/form-template/auto-fill.js
Normal file
69
components/form-template/auto-fill.js
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
const DATE_ONLY_RE = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
|
||||||
|
|
||||||
|
export function resolveAutoFillValue(item, form = {}) {
|
||||||
|
const rule = item && item.autoFill;
|
||||||
|
if (!rule || !rule.sourceField || !rule.type) return undefined;
|
||||||
|
|
||||||
|
const sourceValue = form ? form[rule.sourceField] : '';
|
||||||
|
if (!sourceValue) return '';
|
||||||
|
|
||||||
|
if (rule.type === 'addDays') {
|
||||||
|
const days = Number(rule.days);
|
||||||
|
if (!Number.isFinite(days)) return '';
|
||||||
|
return addDays(sourceValue, days);
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function collectAutoFillChanges(items = [], form = {}) {
|
||||||
|
if (!Array.isArray(items)) return [];
|
||||||
|
|
||||||
|
return items.reduce((changes, item) => {
|
||||||
|
if (!item || !item.title) return changes;
|
||||||
|
|
||||||
|
const nextValue = resolveAutoFillValue(item, form);
|
||||||
|
if (nextValue === undefined) return changes;
|
||||||
|
|
||||||
|
const currentValue = form && form[item.title] !== undefined && form[item.title] !== null
|
||||||
|
? String(form[item.title])
|
||||||
|
: '';
|
||||||
|
if (currentValue !== nextValue) {
|
||||||
|
changes.push({ title: item.title, value: nextValue });
|
||||||
|
}
|
||||||
|
return changes;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
function addDays(value, days) {
|
||||||
|
const date = parseDateOnly(value);
|
||||||
|
if (!date) return '';
|
||||||
|
date.setUTCDate(date.getUTCDate() + days);
|
||||||
|
return formatDateOnly(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseDateOnly(value) {
|
||||||
|
const match = String(value || '').trim().match(DATE_ONLY_RE);
|
||||||
|
if (!match) return null;
|
||||||
|
|
||||||
|
const year = Number(match[1]);
|
||||||
|
const month = Number(match[2]);
|
||||||
|
const day = Number(match[3]);
|
||||||
|
const date = new Date(Date.UTC(year, month - 1, day));
|
||||||
|
|
||||||
|
if (
|
||||||
|
date.getUTCFullYear() !== year ||
|
||||||
|
date.getUTCMonth() !== month - 1 ||
|
||||||
|
date.getUTCDate() !== day
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDateOnly(date) {
|
||||||
|
const year = date.getUTCFullYear();
|
||||||
|
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(date.getUTCDate()).padStart(2, '0');
|
||||||
|
return `${year}-${month}-${day}`;
|
||||||
|
}
|
||||||
@ -8,7 +8,7 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { computed, provide, ref, watch } from 'vue';
|
import { computed, provide, ref, watch } from 'vue';
|
||||||
import verifyForm from './verify.js';
|
import verifyForm from './verify.js';
|
||||||
import { collectAutoFillChanges } from './auto-fill.mjs';
|
import { collectAutoFillChanges } from './auto-fill.js';
|
||||||
|
|
||||||
import FormCell from './form-cell/index.vue';
|
import FormCell from './form-cell/index.vue';
|
||||||
// import CustomCell from './custom-cell/index.vue';
|
// import CustomCell from './custom-cell/index.vue';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user