-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add renderSurvey option (#1324)
Adds the following functions to posthog core 1. posthog.renderSurvey('[survey-id]', '[js-selector]') 2. posthog.canRenderSurvey('[survey-id]')
- Loading branch information
Showing
12 changed files
with
357 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { usePostHog } from 'posthog-js/react' | ||
import { useEffect, useState } from 'react' | ||
import { Survey } from 'posthog-js' | ||
|
||
export default function SurveyForm() { | ||
const posthog = usePostHog() | ||
const [surveys, setSurveys] = useState([] as unknown as Survey[]) | ||
const [selectedSurvey, setSelectedSurvey] = useState('') | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setSelectedSurvey(event.target.value) | ||
} | ||
|
||
useEffect(() => { | ||
posthog.surveys.getSurveys((surveys: Survey[]) => { | ||
setSurveys(surveys) | ||
if (surveys.length > 0) { | ||
setSelectedSurvey(surveys[0].id) | ||
} | ||
}) | ||
}, []) | ||
|
||
const arraySurveyItems = surveys.map((survey) => ( | ||
<option key={survey.id} value={survey.id}> | ||
{survey.name} | ||
</option> | ||
)) | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center gap-2 flex-wrap"> | ||
<select value={selectedSurvey} onChange={handleChange}> | ||
<option value="">Select a survey</option> | ||
{arraySurveyItems} | ||
</select> | ||
<button | ||
onClick={() => posthog.renderSurvey(selectedSurvey, '#survey-container')} | ||
disabled={!selectedSurvey} | ||
> | ||
Render Survey below | ||
</button> | ||
</div> | ||
<div className="flex items-center gap-2 flex-wrap"> | ||
<div id="survey-container"> | ||
<h1>hello world</h1> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.