${surveyQuestionChoices
.map((option, idx) => {
- const inputType = singleOrMultiSelect === 'single_choice' ? 'radio' : 'checkbox'
- const singleOrMultiSelectString = `
- ${checkSVG}
`
+ let choiceClass = 'choice-option'
+ let val = option
+ if (hasOpenChoice && idx === surveyQuestionChoices.length - 1) {
+ option = `
${option}:`
+ choiceClass += ' choice-option-open'
+ val = ''
+ }
+ const inputType = isSingleChoice ? 'radio' : 'checkbox'
+ const singleOrMultiSelectString = `
+
+
+ ${checkSVG}
+
`
return singleOrMultiSelectString
})
.join(' ')}
@@ -639,14 +674,13 @@ export const createMultipleChoicePopup = (
onsubmit: (e: Event) => {
e.preventDefault()
const targetElement = e.target as HTMLFormElement
- const selectedChoices =
- singleOrMultiSelect === 'single_choice'
- ? (targetElement.querySelector('input[type=radio]:checked') as HTMLInputElement)?.value
- : [
- ...(targetElement.querySelectorAll(
- 'input[type=checkbox]:checked'
- ) as NodeListOf
),
- ].map((choice) => choice.value)
+ const selectedChoices = isSingleChoice
+ ? (targetElement.querySelector('input[type=radio]:checked') as HTMLInputElement)?.value
+ : [
+ ...(targetElement.querySelectorAll(
+ 'input[type=checkbox]:checked'
+ ) as NodeListOf),
+ ].map((choice) => choice.value)
posthog.capture('survey sent', {
$survey_name: survey.name,
$survey_id: survey.id,
@@ -670,10 +704,9 @@ export const createMultipleChoicePopup = (
}
if (!isOptional) {
formElement.addEventListener('change', () => {
- const selectedChoices: NodeListOf =
- singleOrMultiSelect === 'single_choice'
- ? formElement.querySelectorAll('input[type=radio]:checked')
- : formElement.querySelectorAll('input[type=checkbox]:checked')
+ const selectedChoices: NodeListOf = isSingleChoice
+ ? formElement.querySelectorAll('input[type=radio]:checked')
+ : formElement.querySelectorAll('input[type=checkbox]:checked')
if ((selectedChoices.length ?? 0) > 0) {
;(formElement.querySelector('.form-submit') as HTMLButtonElement).disabled = false
} else {
@@ -681,6 +714,30 @@ export const createMultipleChoicePopup = (
}
})
}
+ const openChoiceWrappers = formElement.querySelectorAll('.choice-option-open')
+ for (const openChoiceWrapper of openChoiceWrappers) {
+ const textInput = openChoiceWrapper.querySelector('input[type=text]') as HTMLInputElement
+ const inputType = isSingleChoice ? 'radio' : 'checkbox'
+ const checkInput = openChoiceWrapper.querySelector(`input[type=${inputType}]`) as HTMLInputElement
+ openChoiceWrapper.addEventListener('click', () => {
+ if (checkInput?.checked || checkInput?.disabled) textInput?.focus()
+ })
+ textInput.addEventListener('click', (e) => e.stopPropagation())
+ textInput.addEventListener('input', (e) => {
+ const textInput = e.target as HTMLInputElement
+ if (checkInput) {
+ checkInput.value = textInput.value
+ if (textInput.value) {
+ checkInput.disabled = false
+ checkInput.checked = true
+ } else {
+ checkInput.disabled = true
+ checkInput.checked = false
+ }
+ formElement.dispatchEvent(new Event('change'))
+ }
+ })
+ }
return formElement
}
diff --git a/src/posthog-surveys-types.ts b/src/posthog-surveys-types.ts
index 30b122b9f..077dfc0eb 100644
--- a/src/posthog-surveys-types.ts
+++ b/src/posthog-surveys-types.ts
@@ -62,6 +62,7 @@ export interface RatingSurveyQuestion extends SurveyQuestionBase {
export interface MultipleSurveyQuestion extends SurveyQuestionBase {
type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice
choices: string[]
+ hasOpenChoice?: boolean
}
export enum SurveyQuestionType {