Skip to content

Commit 559b176

Browse files
committed
feat(@clayui/language-picker): LPD-45714 Add defaultLocaleId to the API
1 parent 983d7fc commit 559b176

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/clay-core/src/language-picker/__tests__/index.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ describe('ClayLanguagePicker', () => {
161161
expect(container).toMatchSnapshot();
162162
});
163163

164+
it('renders the Default label in the correct default locale', () => {
165+
render(
166+
<ClayLanguagePicker
167+
defaultLocaleId="nl_NL"
168+
locales={locales}
169+
spritemap="/path/to/svg"
170+
translations={{
171+
'es-ES': {total: 4, translated: 2},
172+
'fr-FR': {total: 4, translated: 4},
173+
}}
174+
/>
175+
);
176+
177+
fireEvent.click(screen.getByRole('combobox'));
178+
179+
const nlOption = screen.getByRole('option', {name: /Dutch/});
180+
181+
expect(nlOption).toHaveTextContent('Default');
182+
});
183+
164184
it('renders different labels for translations', () => {
165185
render(
166186
<ClayLanguagePicker

packages/clay-core/src/language-picker/index.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ type Props = {
5454
*/
5555
defaultActive?: boolean;
5656

57+
/**
58+
* The default locale id.
59+
*/
60+
defaultLocaleId?: React.Key;
61+
5762
/**
5863
* The initial selected locale id (uncontrolled).
5964
*/
@@ -112,20 +117,20 @@ type Props = {
112117
};
113118

114119
const TranslationLabel = ({
115-
defaultLanguage,
120+
defaultLocaleId,
116121
locale,
117122
messages,
118123
translation,
119124
}: {
120-
defaultLanguage: Item;
125+
defaultLocaleId: React.Key;
121126
messages: Messages;
122127
locale: Item;
123128
translation: Translation;
124129
}) => {
125130
let displayType: DisplayType = 'warning';
126131
let label = messages.untranslated;
127132

128-
if (locale.label === defaultLanguage?.label) {
133+
if (locale.id === defaultLocaleId) {
129134
displayType = 'info';
130135
label = messages.default;
131136
} else if (translation) {
@@ -217,6 +222,7 @@ const ClayLanguagePicker = ({
217222
active,
218223
classNamesTrigger,
219224
defaultActive = false,
225+
defaultLocaleId,
220226
defaultSelectedLocaleId,
221227
hideTriggerText,
222228
id,
@@ -236,7 +242,6 @@ const ClayLanguagePicker = ({
236242
spritemap,
237243
translations = {},
238244
}: Props) => {
239-
const defaultLanguage = locales[0];
240245
const hasTranslations = Object.keys(translations).length;
241246
const selectedLocale = locales.find(({id}) => id === selectedLocaleId);
242247

@@ -283,7 +288,10 @@ const ClayLanguagePicker = ({
283288
<ClayLayout.ContentCol containerElement="span">
284289
<ClayLayout.ContentSection>
285290
<TranslationLabel
286-
defaultLanguage={defaultLanguage!}
291+
defaultLocaleId={
292+
defaultLocaleId ||
293+
locales[0]!.id
294+
}
287295
locale={locale}
288296
messages={messages}
289297
translation={

0 commit comments

Comments
 (0)