Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown Select by Language #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/components/VoiceDropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const VoiceDropdown = () => {
})

voices.then((voices) => {
// Sort voices by language
voices.sort((a, b) => a.lang.localeCompare(b.lang));

// Limit number of voices shown to 250
voices = voices.slice(0, 250);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?


setVoices(voices);
})
}, []);
Expand All @@ -28,21 +34,38 @@ const VoiceDropdown = () => {
setSelectedVoice(event.target.value);
setVoice(voices.find(voice => voice.name === event.target.value));
};

const handleLanguageChange = (event) => {
setSelectedLanguage(event.target.value);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't exist in this PR.

// Filter voices by selected language
const filteredVoices = voices.filter(voice => voice.lang === event.target.value);

// Set selected voice to the first voice in the filtered voices
setSelectedVoice(filteredVoices[0].name);
setVoice(filteredVoices[0]);
};

return (
<div style={{ display: "flex", alignItems: 'flex-end', justifyContent: "space-between" }}>
<label style={{ paddingRight:" 10px" }}>
Voice
</label>
<select value={ selectedVoice } onChange={handleChange}>
{voices.map((voice) => (
<option key={voice.name} value={voice.name}>
{voice.name} ({voice.lang})
</option>
))}
<select value={ selectedLanguage } onChange={handleLanguageChange}>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat the code using Prettier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will do

{voices.map((voice) => (
<option key={voice.name} value={voice.lang}>
{voice.lang}
</option>
))}
</select>
<select value={ selectedVoice } onChange={handleVoiceChange}>
{voices.map((voice) => (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this list be filtered by the chosen language? We should filter a new list on change and display that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly

<option key={voice.name} value={voice.name}>
{voice.name}
</option>
))}
</select>
</div>
);
};

export default VoiceDropdown ;
export default VoiceDropdown ;