-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avenmia/add workshop dropdown (#125)
* Adding dropdown option for the workshop * Adding second dropdown * Fixing blank answer issue and refactoring dropdown * Refactoring dropdown * Fixing dropdown enabled and disabled * Fixing dropdown styling * Updating delimiter * Removing extra console.log * Fixing enabled and disabled for multiselect andclearing text
- Loading branch information
Showing
5 changed files
with
137 additions
and
10 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
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
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,95 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import type { SurveyAnswer } from "./demographicssurvey"; | ||
|
||
interface DropdownAnswersProps { | ||
answers: SurveyAnswer[]; | ||
updateCurrentAnswer: (val: string) => void; | ||
setDisabled: (val: boolean) => void; | ||
} | ||
|
||
export default function DropdownAnswers({ | ||
answers, | ||
updateCurrentAnswer, | ||
setDisabled, | ||
}: DropdownAnswersProps) { | ||
const uniqueCounties = useMemo(() => { | ||
const counties = answers.map((a) => a.answer.split("-")[0]); | ||
return ["--", ...new Set(counties)] as string[]; | ||
}, [answers]); | ||
|
||
const [county, setCounty] = useState(uniqueCounties[0] ?? ""); | ||
|
||
const getFilteredWorkshopsByCounty = (val: string) => { | ||
const filteredWorkshops: string[] = answers | ||
.map((a) => a.answer) | ||
.filter((c) => c.includes(val)); | ||
return [ | ||
"--", | ||
...(filteredWorkshops.map((c) => c.split("-")[1]) as string[]), | ||
]; | ||
}; | ||
|
||
const [countyWorkshops, setCountyWorkshops] = useState<string[]>( | ||
getFilteredWorkshopsByCounty(uniqueCounties[0] ?? "") | ||
); | ||
|
||
useEffect(() => { | ||
if (county === "--") { | ||
setDisabled(true); | ||
} | ||
setCountyWorkshops(getFilteredWorkshopsByCounty(county)); | ||
}, [county, answers]); | ||
|
||
const handleChange = (val: string) => { | ||
updateCurrentAnswer(`${county}-${val}`); | ||
if (county !== "--" && val !== "--") { | ||
setDisabled(false); | ||
} | ||
if (county === "--" || val === "--") { | ||
setDisabled(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="flex flex-col"> | ||
<> | ||
<label htmlFor="county" className="text-med mb-5 mr-2"> | ||
Select your county | ||
</label> | ||
<select | ||
id="county" | ||
className="form-select mb-5 w-64 rounded" | ||
onChange={(e) => setCounty((e.target as HTMLSelectElement).value)} | ||
> | ||
{uniqueCounties.map((c, index) => ( | ||
<option key={index} value={c}> | ||
{c} | ||
</option> | ||
))} | ||
</select> | ||
</> | ||
{countyWorkshops.length > 0 && ( | ||
<> | ||
<label htmlFor="workshop" className="mb-5 mr-2 text-base"> | ||
Select your workshop | ||
</label> | ||
<select | ||
id="workshop" | ||
className="form-select mb-5 w-64 rounded" | ||
onChange={(e) => | ||
handleChange((e.target as HTMLSelectElement).value) | ||
} | ||
> | ||
{countyWorkshops.map((w, index) => ( | ||
<option key={index} value={w}> | ||
{w} | ||
</option> | ||
))} | ||
</select> | ||
</> | ||
)} | ||
</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
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
dca747b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
hierr – ./
hierr-codeforhawaii.vercel.app
hierr.vercel.app
hierr-git-main-codeforhawaii.vercel.app