8080</template >
8181
8282<script lang="ts">
83+ import type { PropType } from ' vue'
8384import type { FormsOption } from ' ../../models/Entities.d.ts'
8485
8586import IconPlus from ' @material-symbols/svg-400/outlined/add.svg?raw'
@@ -118,7 +119,7 @@ export default defineComponent({
118119
119120 props: {
120121 answer: {
121- type: Object ,
122+ type: Object as PropType < FormsOption > ,
122123 required: true ,
123124 },
124125
@@ -180,18 +181,24 @@ export default defineComponent({
180181 const buttonOptionDown = ref <{ $el? : HTMLElement } | null >(null )
181182 const buttonOptionUp = ref <{ $el? : HTMLElement } | null >(null )
182183 const isIMEComposing = ref (false )
183- const localText = ref ((props .answer as FormsOption | undefined )?.text ?? ' ' )
184+ const answer = computed (() => props .answer )
185+ const localText = ref (answer .value .text ?? ' ' )
186+
187+ const getInputTarget = (
188+ event : Event | { target: EventTarget | null },
189+ ): HTMLInputElement | null => {
190+ return event .target instanceof HTMLInputElement ? event .target : null
191+ }
184192
185193 const canCreateLocalAnswer = computed (() => {
186- if (( props . answer as FormsOption ) .local ) {
194+ if (answer . value .local ) {
187195 return !! localText .value .trim ()
188196 }
189- return !! ( props . answer as FormsOption ) .text ?.trim ()
197+ return !! answer . value .text ?.trim ()
190198 })
191199
192200 const ariaLabel = computed (() => {
193- const answer = props .answer as FormsOption
194- if (answer .local ) {
201+ if (answer .value .local ) {
195202 if (props .optionType === OptionType .Column ) {
196203 return t (' forms' , ' Add a new column' )
197204 }
@@ -220,13 +227,11 @@ export default defineComponent({
220227 })
221228
222229 const optionDragMenuId = computed (() => {
223- const answer = props .answer as FormsOption
224- return ` q${answer .questionId }o${answer .id }o${props .optionType }__drag_menu `
230+ return ` q${answer .value .questionId }o${answer .value .id }o${props .optionType }__drag_menu `
225231 })
226232
227233 const placeholder = computed (() => {
228- const answer = props .answer as FormsOption
229- if (answer .local ) {
234+ if (answer .value .local ) {
230235 if (props .optionType === OptionType .Column ) {
231236 return t (' forms' , ' Add a new column' )
232237 }
@@ -252,8 +257,7 @@ export default defineComponent({
252257 })
253258
254259 const pseudoIcon = computed (() => {
255- const answer = props .answer as FormsOption
256- if (answer .local ) {
260+ if (answer .value .local ) {
257261 return IconPlus
258262 }
259263
@@ -305,20 +309,19 @@ export default defineComponent({
305309 async function onInput(
306310 event : InputEvent | { target: HTMLInputElement ; isComposing? : boolean },
307311 ): Promise <void > {
308- const target = event . target as HTMLInputElement | null
312+ const target = getInputTarget ( event )
309313 if (! target ) {
310314 return
311315 }
312316
313- const answer = props .answer as FormsOption
314- if (answer .local ) {
317+ if (answer .value .local ) {
315318 localText .value = target .value
316319 return
317320 }
318321
319322 if (! event .isComposing && ! isIMEComposing .value && target .value !== ' ' ) {
320323 // clone answer
321- const answerCopy = { ... answer }
324+ const answerCopy = { ... answer . value }
322325 if (! input .value ) {
323326 return
324327 }
@@ -339,7 +342,7 @@ export default defineComponent({
339342 * @param e The keyboard event.
340343 */
341344 const onEnter = (e : KeyboardEvent ): void => {
342- if (( props . answer as FormsOption ) .local ) {
345+ if (answer . value .local ) {
343346 void createLocalAnswer (e )
344347 return
345348 }
@@ -363,8 +366,8 @@ export default defineComponent({
363366 return
364367 }
365368
366- const answer = {
367- ... ( props . answer as FormsOption ) ,
369+ const answerValue = {
370+ ... answer . value ,
368371 text: value ,
369372 local: false ,
370373 }
@@ -373,7 +376,7 @@ export default defineComponent({
373376 try {
374377 // Forward changes, but use current answer.text to avoid erasing
375378 // any in-between changes while creating the answer
376- const newAnswer = await createAnswer (answer )
379+ const newAnswer = await createAnswer (answerValue )
377380 if (! input .value ) {
378381 return
379382 }
@@ -413,7 +416,7 @@ export default defineComponent({
413416 return
414417 }
415418
416- if (( props . answer as FormsOption ) .local ) {
419+ if (answer . value .local ) {
417420 return
418421 }
419422
@@ -424,7 +427,7 @@ export default defineComponent({
424427 // Dismiss delete key action
425428 e .preventDefault ()
426429 void queue .value .add (() => {
427- emit (' delete' , props .answer as FormsOption )
430+ emit (' delete' , props .answer )
428431 queue .value .pause ()
429432 queue .value .clear ()
430433 })
@@ -543,7 +546,7 @@ export default defineComponent({
543546 const onCompositionEnd = (
544547 event : CompositionEvent & { isComposing? : boolean },
545548 ): void => {
546- const target = event . target as HTMLInputElement | null
549+ const target = getInputTarget ( event )
547550 isIMEComposing .value = false
548551 if (! event .isComposing && target ) {
549552 void onInput ({ target , isComposing: event .isComposing })
0 commit comments