-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Language selector: add prop for display lang for bug fix (#2622)
- Loading branch information
1 parent
d38b31b
commit df68d0d
Showing
3 changed files
with
165 additions
and
57 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
80 changes: 80 additions & 0 deletions
80
src/components/LanguageSelector/LanguageSelectorDropdown.tsx
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,80 @@ | ||
import React, { useState } from 'react' | ||
import { Menu } from '../header/Menu/Menu' | ||
import { LanguageSelectorButton } from './LanguageSelectorButton' | ||
import classnames from 'classnames' | ||
import { LanguageDefinition, LanguageSelectorProps } from './LanguageSelector' | ||
import { Button } from '../Button/Button' | ||
|
||
const generateMenuItems = (langs: LanguageDefinition[]) => { | ||
return langs.map((lang, index) => { | ||
const label = ( | ||
<> | ||
<span lang={lang.attr}> | ||
<strong>{lang.label}</strong> | ||
</span> | ||
{lang.label_local && ` (${lang.label_local})`} | ||
</> | ||
) | ||
if (typeof lang.on_click === 'string') { | ||
return ( | ||
<a key={index} href={lang.on_click} data-testid={lang.attr}> | ||
{label} | ||
</a> | ||
) | ||
} else { | ||
return ( | ||
<Button | ||
key={index} | ||
onClick={lang.on_click} | ||
data-testid={lang.attr} | ||
type="button" | ||
unstyled> | ||
{label} | ||
</Button> | ||
) | ||
} | ||
}) | ||
} | ||
|
||
const LanguageSelectorDropdown: React.FC<LanguageSelectorProps> = ({ | ||
label, | ||
langs, | ||
small, | ||
className, | ||
displayLang, | ||
...divProps | ||
}) => { | ||
const [isOpen, setIsOpen] = useState(false) | ||
|
||
const classes = classnames( | ||
'usa-language-container', | ||
{ | ||
[`usa-language--small`]: small !== undefined, | ||
}, | ||
className | ||
) | ||
const displayLabel = langs.find((langDef) => langDef.attr === displayLang) | ||
|
||
return ( | ||
<div className={classes} data-testid="languageSelector" {...divProps}> | ||
<ul className="usa-language__primary usa-accordion"> | ||
<li className="usa-language__primary-item"> | ||
<LanguageSelectorButton | ||
className={classes} | ||
label={displayLabel?.label || label || langs[0].label} | ||
isOpen={isOpen} | ||
onToggle={() => setIsOpen((prevIsOpen) => !prevIsOpen)} | ||
/> | ||
<Menu | ||
items={generateMenuItems(langs)} | ||
isOpen={isOpen} | ||
id="language-options" | ||
type="language" | ||
/> | ||
</li> | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export default LanguageSelectorDropdown |