Warning
This project is currently under development and not ready for public use.
A React hook that uses the find-language to provide the logic for a language chooser React component such as MUI Language Chooser.
Install with npm: npm i @ethnolib/language-chooser-react-hook
searchResultModifier: ( results: FuseResult<ILanguage>[], searchString: string ) => ILanguage[]
- Can be used to add, remove, and modify results. See find-language for details.
-
languageData: ILanguage[]
- The list of languages resulting from fuzzy-searchingsearchString
against the language database. Automatically updates wheneversearchString
changes. -
selectedLanguage: ILanguage | undefined
- A React state variable for storing a language selected by the user. Set bytoggleSelectedLanguage
,selectUnlistedLanguage
, andresetTo
. Automatically cleared wheneversearchString
changes. -
selectedScript: IScript | undefined
- A React state variable for storing a script selected by the user. Set bytoggleSelectedScript
,saveLanguageDetails
, andresetTo
. Automatically cleared wheneverselectedLanguage
changes. When a language with exactly 1 script option is selected,selectedScript
will automatically be set to that script. -
customizableLanguageDetails: ICustomizableLanguageDetails
- A React state variable for storing any custom display name, region, or dialect information the user has entered. Set bysaveLanguageDetails
andresetTo
. Automatically cleared whenever theselectedLanguage
changes. -
searchString: string
- A React state variable. Value is set by thesearchString
argument of the most recent call toonSearchStringChange
. -
onSearchStringChange: (searchString: string) => void
- Should be called whenever the user makes changes to the search string (debouncing first is recommended). -
toggleSelectLanguage: (language: ILanguage) => void
- Iflanguage
is theselectedLanguage
, clears theselectedLanguage
. Otherwise, setsselectedLanguage
tolanguage
. If selecting a language that has only one script option, this will automatically setselectedScript
to that script. -
toggleSelectScript: (script: IScript) => void
- Ifscript
is theselectedScript
, clears theselectedScript
. Otherwise, setsselectedScript
toscript
. -
isReadyToSubmit: boolean
- Returns true if the user has selected a language and, if that language requires a script selection, has selected a script. -
saveLanguageDetails: ( details: ICustomizableLanguageDetails, script: IScript | undefined ) => void
- SetscustomizableLanguageDetails
andselectedScript
-
selectUnlistedLanguage: () => void
- Set theselectedLanguage
to "Unknown Language" with code "qaa". To be used for languages that are not in the database. -
resetTo: (initialState: IOrthography) => void
- For restoring preexisting data when the LanguageChooser is first opened. SetsselectedLanguage
,selectedScript
, andcustomizableLanguageDetails
to the values ininitialState
.
See Mui React LanguageChooser for an example usage.
Changes made here will be automatically reflected in the MUI language chooser dev server: nx dev @ethnolib/language-chooser-react-mui
.
See the main README.
LanguageChooser-react-hook
uses Vitest for unit testing. Run
nx test @ethnolib/language-chooser-react-hook
from anywhere in the monorepo, or
npm run test
from package root.