问卷地址 键盘输入

master
lion 4 days ago
parent c8bf4c6671
commit 0b0b1b68d2

@ -15,17 +15,23 @@
<checkbox-group v-else-if="field.edit_input === 'checkbox'" class="choices" @change="checkboxChange">
<label v-for="opt in field.select_item" :key="opt.key" class="choice"><checkbox :value="String(opt.key)" :checked="selected(opt.key)" color="#2018a0" :disabled="disabled || (!selected(opt.key) && !!field.config.max_choices && value.length >= field.config.max_choices)" /><text>{{ opt.value }}</text></label>
</checkbox-group>
<u-input
<textarea
v-else-if="field.edit_input === 'textarea'"
type="textarea"
:value="value || ''"
class="survey-native-textarea"
:value="inputValue"
:disabled="disabled"
:maxlength="10000"
:placeholder="textPlaceholder"
:cursor-spacing="100"
:auto-height="false"
:adjust-position="true"
:disable-default-padding="true"
:show-confirm-bar="true"
:cursor-spacing="0"
@tap.stop
@touchstart="$emit('editing-touch')"
@input="input"
@focus="$emit('focus')"
@blur="$emit('blur')"
@focus="$emit('focus', $event)"
@blur="textareaBlur"
/>
<view
v-else-if="field.edit_input === 'date' || field.edit_input === 'datetime'"
@ -41,7 +47,8 @@
:disabled="disabled"
:maxlength="30"
placeholder="请输入数量"
:cursor-spacing="100"
:cursor-spacing="0"
:adjust-position="true"
@input="numberInput"
@focus="$emit('focus')"
@blur="$emit('blur')"
@ -53,7 +60,8 @@
:disabled="disabled"
:maxlength="field.edit_input === 'number' ? 30 : 10000"
:placeholder="textPlaceholder"
:cursor-spacing="100"
:cursor-spacing="0"
:adjust-position="true"
@input="input"
@focus="$emit('focus')"
@blur="$emit('blur')"
@ -134,6 +142,11 @@ export default {
input(e) {
this.$emit('input', e && e.detail ? e.detail.value : e)
},
textareaBlur(event) {
// Preserve the final native textarea value before notifying the page of blur.
if (event && event.detail && event.detail.value !== undefined) this.input(event)
this.$emit('blur', event)
},
openPicker() {
if (!this.disabled) this.pickerVisible = true
},
@ -148,6 +161,7 @@ export default {
}
</script>
<style scoped>
.survey-native-textarea { display: block; box-sizing: content-box; width: 100%; height: 100rpx; padding: 10rpx 0; font-size: 28rpx; line-height: 1.6; color: #303133; background: transparent; }
.choices { display: flex; flex-wrap: wrap; }
.choice { display: flex; align-items: flex-start; flex-basis: 100%; margin-bottom: 10rpx; font-size: 28rpx; line-height: 1.8; }
.choice checkbox { flex-shrink: 0; }

@ -18,7 +18,7 @@
<view v-else-if="profileRequired" class="profile-gate"><view>请先登录或注册后填写问卷</view><button class="profile-gate-action" hover-class="profile-gate-action--pressed" @click="showRegister = true">登录或注册</button></view>
<template v-else>
<view class="survey-header"><view class="survey-title">{{ survey.title }}</view><view class="survey-description"><u-parse :html="descriptionHtml" :autoset-title="false" /></view></view>
<view class="survey-form" :class="{ 'survey-form--last-input-focused': keyboardOpen }">
<view class="survey-form">
<view v-for="q in shownQuestions" :key="q.id" :id="'question-' + q.id" class="question-item" :class="{ invalid: errorId === q.id, 'chapter-card': q.edit_input === 'section', 'chapter-with-help': q.edit_input === 'section' && q.help }">
<view class="question-header">
<view v-if="q.edit_input !== 'section'" class="question-number">{{ questionNumber(q) }}</view>
@ -27,18 +27,18 @@
</view>
</view>
<view v-if="q.help && q.edit_input !== 'checkbox' && q.edit_input !== 'text' && q.edit_input !== 'textarea'" class="question-title-theme">{{ q.help }}</view>
<view v-if="q.edit_input !== 'section'" class="question-content">
<view v-if="q.edit_input !== 'section'" :id="'answer-' + q.id" class="question-content">
<view v-if="q.edit_input === 'multi_dimension'" class="multi-dimension-container">
<view v-for="dim in q.dimensions" :key="dim.field" class="dimension-item">
<view v-for="dim in q.dimensions" :key="dim.field" :id="'dimension-' + q.id + '-' + dim.field" class="dimension-item">
<view class="dimension-title"><text class="dimension-name">{{ dim.name }}</text><text v-if="dim.help" class="dimension-help">{{ dim.help }}</text></view>
<SurveyField :field="dim" :value="q.value[dim.field]" :disabled="submitting" @input="setDimension(q, dim, $event)" @focus="focus(q)" @blur="keyboardOpen = false" />
<SurveyField :field="dim" :value="q.value[dim.field]" :disabled="submitting" @input="setDimension(q, dim, $event)" @editing-touch="traceKeyboard('touch', { target: '#dimension-' + q.id + '-' + dim.field })" @focus="focus(q, 'dimension-' + q.id + '-' + dim.field, $event)" @blur="blur(q, 'dimension-' + q.id + '-' + dim.field)" />
</view>
</view>
<template v-else-if="q.edit_input !== 'section'">
<view v-if="q.edit_input === 'checkbox'" class="dimension-help">最少选择{{ q.config.min_choices || 1 }}项<text v-if="q.config.max_choices > 0">,最多选择{{ q.config.max_choices }}项</text>,已选择{{ q.value.length }}项</view>
<SurveyField :field="q" :value="q.value" @uploading="setUploading(q, $event)" :disabled="submitting" @input="setValue(q, $event)" @focus="focus(q)" @blur="keyboardOpen = false" />
<view v-for="opt in notesFor(q)" :key="opt.key" class="other-input"><text>{{ opt.value }} · 补充说明</text><u-input :value="q.option_texts[opt.key] || ''" type="textarea" :placeholder="q.config.option_notes[opt.key].placeholder || '请填写补充说明'" :disabled="submitting" :maxlength="10000" :cursor-spacing="80" @input="setOptionText(q.id, opt.key, $event)" @focus="focus(q)" @blur="setOptionText(q.id, opt.key, $event); keyboardOpen = false" /></view>
<view v-if="q.show_other" class="other-input"><text>补充说明</text><text v-if="q.config.other_required" class="required-mark">*</text><u-input v-model="q.other_text" type="textarea" :placeholder="q.config.other_placeholder || '请填写补充说明'" :disabled="submitting" :maxlength="10000" :cursor-spacing="80" @focus="focus(q)" @blur="keyboardOpen = false" /></view>
<SurveyField :field="q" :value="q.value" @uploading="setUploading(q, $event)" :disabled="submitting" @input="setValue(q, $event)" @editing-touch="traceKeyboard('touch', { target: '#answer-' + q.id })" @focus="focus(q, 'answer-' + q.id, $event)" @blur="blur(q)" />
<view v-for="(opt, noteIndex) in notesFor(q)" :key="opt.key" :id="'note-' + q.id + '-' + noteIndex" class="other-input"><text>{{ opt.value }} · 补充说明</text><textarea class="survey-native-textarea" :value="q.option_texts[opt.key] || ''" :placeholder="q.config.option_notes[opt.key].placeholder || '请填写补充说明'" :disabled="submitting" :maxlength="10000" :cursor-spacing="0" :adjust-position="true" :auto-height="false" :disable-default-padding="true" :show-confirm-bar="true" @tap.stop @input="setOptionText(q.id, opt.key, $event)" @touchstart="traceKeyboard('touch', { target: '#note-' + q.id + '-' + noteIndex })" @focus="focus(q, 'note-' + q.id + '-' + noteIndex, $event)" @blur="setOptionText(q.id, opt.key, $event); blur(q, 'note-' + q.id + '-' + noteIndex)" /></view>
<view v-if="q.show_other" :id="'other-' + q.id" class="other-input"><text>补充说明</text><text v-if="q.config.other_required" class="required-mark">*</text><textarea class="survey-native-textarea" v-model="q.other_text" :placeholder="q.config.other_placeholder || '请填写补充说明'" :disabled="submitting" :maxlength="10000" :cursor-spacing="0" :adjust-position="true" :auto-height="false" :disable-default-padding="true" :show-confirm-bar="true" @tap.stop @touchstart="traceKeyboard('touch', { target: '#other-' + q.id })" @focus="focus(q, 'other-' + q.id, $event)" @blur="blur(q, 'other-' + q.id)" /></view>
</template>
</view>
</view>
@ -96,7 +96,6 @@ export default {
showRegister: false,
errorId: null,
activeChapter: null,
keyboardOpen: false,
startedAt: 0
}
},
@ -111,19 +110,26 @@ export default {
}
},
onLoad(options) {
console.info('[survey-keyboard] native-keyboard-20261123c')
this._keyboardHeight = 0
this._focusedInput = null
const info = uni.getSystemInfoSync ? uni.getSystemInfoSync() : {}
this._keyboardEnvironment = {}
for (const key of ['platform', 'system', 'brand', 'model', 'version', 'SDKVersion', 'windowHeight', 'screenHeight']) {
if (info[key] != null) this._keyboardEnvironment[key] = info[key]
}
this.id = options.id
this.course_id = options.course_id || ''
this.startedAt = Date.now()
this.load()
this._keyboardListener = (res) => {
if (!res.height) this.keyboardOpen = false
}
this._keyboardListener = res => this.updateKeyboardHeight(res)
uni.onKeyboardHeightChange(this._keyboardListener)
},
onShow() {
if (this.profileRequired && !this.loading) this.load()
},
onUnload() {
this._focusedInput = null
if (this._keyboardListener)
uni.offKeyboardHeightChange(this._keyboardListener)
},
@ -192,7 +198,6 @@ export default {
this.completed = false
this.showRegister = false
this.profileRequired = false
this.keyboardOpen = false
this.activeChapter = null
this.errorId = null
return true
@ -209,7 +214,8 @@ export default {
this.profileRequired = false
this.errorId = null
this.activeChapter = null
this.keyboardOpen = false
this._keyboardHeight = 0
this._focusedInput = null
try {
const res = await this.$u.api.courseEvaluationDetail({
course_content_evaluation_id: this.id
@ -275,10 +281,34 @@ export default {
uni.pageScrollTo({ scrollTop: Math.max(0, target.top + viewport.scrollTop - (header ? header.height : 0) - 12), duration: 300 })
})
},
focus(q) {
this.keyboardOpen = true
focus(q, targetId, event) {
this._focusedInput = { selector: '#' + (targetId || ('answer-' + q.id)) }
const detail = event && event.detail ? event.detail : {}
this.traceKeyboard('focus', { nativeHeight: detail.height })
},
blur(q, targetId) {
const selector = '#' + (targetId || ('answer-' + q.id))
if (!this._focusedInput || this._focusedInput.selector !== selector) return
this.traceKeyboard('blur')
this._focusedInput = null
},
updateKeyboardHeight(event) {
// Observation only. Do not set reactive data, resize the page, scroll it or
// request focus while iOS is opening/closing a third-party keyboard.
this._keyboardHeight = Math.max(0, Number(event.height) || 0)
this.traceKeyboard('keyboard', { duration: event.duration })
},
traceKeyboard(event, detail = {}) {
const events = this._keyboardTrace || (this._keyboardTrace = [])
events.push({ time: Date.now(), event, target: this._focusedInput ? this._focusedInput.selector : '', height: this._keyboardHeight || 0, ...detail })
if (events.length > 60) events.shift()
},
getKeyboardDiagnostics() {
// Debug metadata only: no answers, personal information or login credentials.
return { version: 'native-keyboard-20261123c', mode: 'native-adjust-position', environment: { ...this._keyboardEnvironment }, events: (this._keyboardTrace || []).slice() }
},
submit() {
this.traceKeyboard('submit')
if (Object.values(this.uploadingFiles).some(Boolean)) return uni.showToast({ title: '请等待文件上传完成', icon: 'none' })
if (this.submitting || this.profileRequired || this.loading || this.unavailableMessage || this.error) return
const error = firstError(this.questionList)
@ -288,6 +318,7 @@ export default {
uni.showToast({ title: error.message, icon: 'none', duration: 3000 })
return
}
if (this._keyboardHeight > 0 && uni.hideKeyboard) uni.hideKeyboard()
this.submitting = true
uni.showModal({
title: '提交问卷',
@ -463,8 +494,8 @@ export default {
}
}
.survey-native-textarea { display: block; box-sizing: content-box; width: 100%; height: 100rpx; padding: 10rpx 0; font-size: 28rpx; line-height: 1.6; color: #303133; background: transparent; }
.survey-form {}
.survey-form--last-input-focused { padding-bottom: 450rpx; }
.question-item {
// padding: 30rpx 0;

@ -0,0 +1,89 @@
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 read = path => fs.readFileSync(new URL(path, import.meta.url), 'utf8')
const helpers = await import('data:text/javascript;base64,' + Buffer.from(read('../common/survey.js')).toString('base64'))
const source = read('../packages/surveyFill/index.vue')
const script = source.split('<script>')[1].split('</script>')[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 previousUni = globalThis.uni
let removedListener
let scrolls = 0
let hides = 0
const uni = {
pageScrollTo: () => { scrolls++ }, hideKeyboard: () => { hides++ },
createSelectorQuery: () => { throw new Error('Keyboard events must not measure or scroll the page') },
offKeyboardHeightChange: listener => { removedListener = listener }
}
globalThis.uni = uni
const vm = new Vue(page)
try {
vm.questionList = helpers.prepareQuestions([{ id: 109, name: '收件地址', edit_input: 'textarea' }])
const originalState = JSON.stringify(vm.$data)
// Replay the important ordering from the real iPhone trace: keyboard-height
// events arrive before focus, and repeated 429/0 events precede native blur.
const events = [
['keyboard', 0], ['keyboard', 429], ['keyboard', 429], ['keyboard', 0], ['focus'],
['keyboard', 429], ['keyboard', 429], ['keyboard', 0], ['blur'], ['focus'],
['keyboard', 429], ['keyboard', 429], ['keyboard', 429], ['keyboard', 0], ['blur'], ['focus']
]
for (const [event, height] of events) {
if (event === 'keyboard') vm.updateKeyboardHeight({ height })
else if (event === 'focus') vm.focus({ id: 109 }, undefined, { detail: { height: 429 } })
else vm.blur({ id: 109 })
await Vue.nextTick()
assert.equal(JSON.stringify(vm.$data), originalState, 'Keyboard/focus callbacks must not change any reactive page layout')
}
assert.equal(scrolls, 0)
assert.equal(hides, 0)
vm.focus({ id: 3 }, 'note-3-0')
vm.blur({ id: 109 })
assert.equal(vm._focusedInput.selector, '#note-3-0')
vm.setValue(vm.questionList[0], '地址第一行\n第二行')
assert.equal(helpers.submission(vm.questionList)[0].value, '地址第一行\n第二行')
const diagnostics = vm.getKeyboardDiagnostics()
assert.equal(diagnostics.version, 'native-keyboard-20261123c')
assert.equal(diagnostics.mode, 'native-adjust-position')
assert.ok(diagnostics.events.some(event => event.event === 'focus'))
assert.ok(diagnostics.events.every(event => !('value' in event) && !('token' in event)))
for (let i = 0; i < 80; i++) vm.updateKeyboardHeight({ height: 429 })
assert.equal(vm.getKeyboardDiagnostics().events.length, 60)
vm._keyboardListener = () => {}
page.onUnload.call(vm)
assert.equal(removedListener, vm._keyboardListener)
assert.equal(vm._focusedInput, null)
console.log('iPhone keyboard trace regression passed: zero layout writes, zero scripted scrolls/hides, delayed focus/blur ordering, answer retention and diagnostics')
} finally {
vm.$destroy()
globalThis.uni = previousUni
}
const compiler = require('../../sz-kjsxy-web/node_modules/vue-template-compiler')
const fieldSource = read('../packages/surveyFill/components/SurveyField.vue')
const fieldScript = fieldSource.split('<script>')[1].split('</script>')[0].replace(/import[^\n]+\n/g, '').replace('export default', 'return')
const field = new Function('SurveyFile', fieldScript)({})
const render = compiler.compileToFunctions(fieldSource.split('<template>')[1].split('</template>')[0])
const fieldVm = new Vue({ ...field, ...render, propsData: { field: { edit_input: 'textarea', help: '请输入收件地址' }, value: '', disabled: false } })
let emittedText, blurred = false, touch = false
fieldVm.$on('input', value => { emittedText = value })
fieldVm.$on('blur', () => { blurred = true })
fieldVm.$on('editing-touch', () => { touch = true })
const textarea = fieldVm._render().children.find(node => node.tag === 'textarea')
assert.ok(textarea)
assert.equal(textarea.data.attrs['auto-height'], false)
assert.equal(textarea.data.attrs['adjust-position'], true)
assert.equal(textarea.data.attrs.focus, undefined)
assert.equal(textarea.data.attrs['hold-keyboard'], undefined)
assert.equal(textarea.data.attrs.placeholder, '请输入收件地址')
textarea.data.on.touchstart({})
assert.equal(touch, true)
textarea.data.on.input({ detail: { value: '收件地址第一行' } })
assert.equal(emittedText, '收件地址第一行')
textarea.data.on.blur({ detail: { value: '收件地址第一行\n第二行' } })
assert.equal(emittedText, '收件地址第一行\n第二行')
assert.equal(blurred, true)
fieldVm.$destroy()
console.log('Native textarea regression passed: stable size, native keyboard positioning, uncontrolled focus, touch trace and final blur value')
Loading…
Cancel
Save