@@ -34,6 +34,14 @@ export default function kbaCreateComponent(
3434 questionInput . appendChild ( option ) ;
3535 } ) ;
3636
37+ // Add option to create a question if allowed
38+ if ( callback . isAllowedUserDefinedQuestions ( ) ) {
39+ const userDefinedOption = document . createElement ( 'option' ) ;
40+ userDefinedOption . value = 'user-defined' ;
41+ userDefinedOption . text = 'Enter your own question' ;
42+ questionInput . appendChild ( userDefinedOption ) ;
43+ }
44+
3745 // Answer field
3846 const answerLabel = document . createElement ( 'label' ) ;
3947 answerLabel . htmlFor = `${ collectorKey } -answer` ;
@@ -53,10 +61,32 @@ export default function kbaCreateComponent(
5361
5462 // Event listeners
5563 questionInput . addEventListener ( 'input' , ( event ) => {
56- callback . setQuestion ( ( event . target as HTMLInputElement ) . value ) ;
64+ const selectedQuestion = ( event . target as HTMLInputElement ) . value ;
65+ if ( selectedQuestion === 'user-defined' ) {
66+ // If user-defined option is selected, prompt for custom question
67+ const customQuestionLabel = document . createElement ( 'label' ) ;
68+ customQuestionLabel . htmlFor = `${ collectorKey } -question-user-defined` ;
69+ customQuestionLabel . innerText = 'Type your question ' + idx + ':' ;
70+
71+ const customQuestionInput = document . createElement ( 'input' ) ;
72+ customQuestionInput . type = 'text' ;
73+ customQuestionInput . id = `${ collectorKey } -question-user-defined` ;
74+ customQuestionInput . placeholder = 'Type your question' ;
75+
76+ container . lastElementChild ?. before ( customQuestionLabel ) ;
77+ container . lastElementChild ?. before ( customQuestionInput ) ;
78+ customQuestionInput . addEventListener ( 'input' , ( e ) => {
79+ callback . setQuestion ( ( e . target as HTMLInputElement ) . value ) ;
80+ console . log ( 'Custom question ' + idx + ':' , callback . getInputValue ( 0 ) ) ;
81+ } ) ;
82+ } else {
83+ callback . setQuestion ( ( event . target as HTMLInputElement ) . value ) ;
84+ console . log ( 'Selected question ' + idx + ':' , callback . getInputValue ( 0 ) ) ;
85+ }
5786 } ) ;
5887
5988 answerInput . addEventListener ( 'input' , ( event ) => {
6089 callback . setAnswer ( ( event . target as HTMLInputElement ) . value ) ;
90+ console . log ( 'Answer ' + idx + ':' , callback . getInputValue ( 1 ) ) ;
6191 } ) ;
6292}
0 commit comments