From 44b2ea08a01a3edfd325f5e5ea02e4b372423f41 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Fri, 6 Nov 2026 10:52:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E6=88=B7=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/surveyFill/index.vue | 13 +++++++- tests/survey-option-notes.test.mjs | 51 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/survey-option-notes.test.mjs diff --git a/packages/surveyFill/index.vue b/packages/surveyFill/index.vue index 3b0dd6c..0e2276d 100644 --- a/packages/surveyFill/index.vue +++ b/packages/surveyFill/index.vue @@ -37,7 +37,7 @@ @@ -239,6 +239,17 @@ export default { if (!q.show_other) q.other_text = '' if (this.errorId === q.id) this.errorId = null }, + setOptionText(questionId, optionKey, value) { + if (this.profileRequired || this.submitting) return + // Mini-program nested v-for event datasets contain serialized question copies. + // Resolve the original answer by ID instead of mutating the rendered copy. + const question = this.questionList.find(q => String(q.id) === String(questionId)) + if (!question) return + const text = value && value.detail ? value.detail.value : value + if (!question.option_texts || typeof question.option_texts !== 'object' || Array.isArray(question.option_texts)) this.$set(question, 'option_texts', {}) + this.$set(question.option_texts, String(optionKey), text == null ? '' : String(text)) + if (String(this.errorId) === String(question.id)) this.errorId = null + }, setDimension(q, dim, value) { if (this.profileRequired || this.submitting) return this.$set(q.value, dim.field, value) diff --git a/tests/survey-option-notes.test.mjs b/tests/survey-option-notes.test.mjs new file mode 100644 index 0000000..39bf8be --- /dev/null +++ b/tests/survey-option-notes.test.mjs @@ -0,0 +1,51 @@ +import fs from 'node:fs' +import assert from 'node:assert/strict' +import { createRequire } from 'node:module' +const require = createRequire(import.meta.url) +const Vue = require('../../sz-kjsxy-web/node_modules/vue') +const helperSource = fs.readFileSync(new URL('../common/survey.js', import.meta.url), 'utf8') +const helpers = await import('data:text/javascript;base64,' + Buffer.from(helperSource).toString('base64')) +const pageSource = fs.readFileSync(new URL('../packages/surveyFill/index.vue', import.meta.url), 'utf8') +const script = pageSource.split('')[0] + .replace(/import[\s\S]*?from\s+['"][^'"]+['"]/g, '').replace('export default', 'return') +const imports = ['prepareQuestions', 'required', 'showOther', 'firstError', 'progress', 'submission', 'formatDescription', 'fillUserFields', 'visibleQuestions', 'selectedNotes'] +const page = new Function('SurveyField', ...imports, script)({}, ...imports.map(name => name === 'formatDescription' ? helpers.descriptionHtml : helpers[name])) +const vm = new Vue(page) +vm.questionList = helpers.prepareQuestions([ + { id: 30, field: 'evening', name: '晚上:参加的主题及人数', edit_input: 'checkbox', need_fill: true, config: { min_choices: 1, max_choices: 3, option_notes: { tax: { required: true }, law: { required: true } } }, select_item: [{ key: 'tax', value: '财税' }, { key: 'law', value: '法律' }, { key: 'hr', value: '人力资源' }] }, + { id: 31, field: 'other', name: '另一题', edit_input: 'radio', config: { option_notes: { '0': { required: true } } }, select_item: [{ key: '0', value: '其他' }] } +]) +vm.setValue(vm.questionList[0], ['tax']) +const detached = JSON.parse(JSON.stringify(vm.shownQuestions[0])) +Vue.set(detached.option_texts, 'tax', '国际税,2人') +assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/) // Reproduce the old serialized-event update. +vm.errorId = 30 +vm.setOptionText(detached.id, 'tax', '国际税,2人') +await Vue.nextTick() +assert.equal(vm.questionList[0].option_texts.tax, '国际税,2人') +assert.equal(vm.errorId, null) +assert.equal(helpers.firstError(vm.questionList), null) +assert.deepEqual(helpers.submission(vm.questionList)[0].option_texts, { tax: '国际税,2人' }) +// Input and blur can supply either plain text (u-input) or a native detail object. +vm.setOptionText('30', 'tax', { detail: { value: '国际税,3人' } }) +assert.equal(helpers.submission(vm.questionList)[0].option_texts.tax, '国际税,3人') +vm.setValue(vm.questionList[0], ['tax', 'law']) +assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/) +vm.setOptionText(30, 'law', '合同,1人') +assert.equal(helpers.firstError(vm.questionList), null) +vm.setValue(vm.questionList[1], '0') +vm.setOptionText(31, 0, '另一题的说明') +assert.equal(helpers.submission(vm.questionList)[1].option_texts['0'], '另一题的说明') +assert.equal(vm.questionList[0].option_texts.tax, '国际税,3人') +vm.setValue(vm.questionList[0], ['law']) +assert.deepEqual(helpers.submission(vm.questionList)[0].option_texts, { law: '合同,1人' }) +vm.setOptionText(30, 'law', ' ') +assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/) +vm.setOptionText(30, 'law', '') +assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/) +vm.setOptionText(30, 'law', '正常内容') +vm.submitting = true +vm.setOptionText(30, 'law', '提交期间不得覆盖') +assert.equal(vm.questionList[0].option_texts.law, '正常内容') +vm.$destroy() +console.log('Survey option notes: serialized event regression, reactive input, blur synchronization, independent options and required validation passed')