Skip to content

feat(@clayui/localized-input): LPD-50722 Use Language Picker for switching languages #6049

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/clay-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
export {Provider, useProvider} from '@clayui/provider';

export {IconSelector} from './icon-selector';
export {Item} from './language-picker';
export {Heading, Text, TextHighlight} from './typography';
export {OverlayMask} from './overlay-mask';
export {TreeView} from './tree-view';
Expand Down
84 changes: 45 additions & 39 deletions packages/clay-core/src/language-picker/LanguagePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import ClayLabel from '@clayui/label';
import ClayLayout from '@clayui/layout';
import {InternalDispatch, sub} from '@clayui/shared';
import {ClayTooltipProvider} from '@clayui/tooltip';
import classNames from 'classnames';
import React from 'react';

import {Option, Picker} from '../picker';

type DisplayType = 'info' | 'secondary' | 'success' | 'warning';

type Item = {
export type Item = {
id: string;
label: string;
name?: string;
Expand Down Expand Up @@ -128,23 +129,25 @@
defaultLocaleId: React.Key;
localeId: React.Key;
messages: Messages;
translation: Translation;
translation?: Translation;
}) => {
let displayType: DisplayType = 'warning';
let label: string = messages.untranslated;

if (localeId === defaultLocaleId) {
displayType = 'info';
label = messages.default;
} else if (translation) {
if (translation) {
Copy link
Member

Choose a reason for hiding this comment

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

Since we are now checking if translation exists, we should make it an optional prop. translation?

const {total, translated} = translation;

if (total && total === translated) {
displayType = 'success';
label = messages.translated;
} else {
displayType = 'secondary';
label = sub(messages.translating, [translated, total]);
if (translated !== 0) {
if (localeId === defaultLocaleId) {
displayType = 'info';
label = messages.default;
} else if (total === translated) {
displayType = 'success';
label = messages.translated;
} else {
displayType = 'secondary';
label = sub(messages.translating, [translated, total]);
}
}
}

Expand Down Expand Up @@ -173,35 +176,38 @@
);

return (
<button
{...otherProps}
aria-label={sub(triggerMessage, [
selectedItem?.name || selectedItem?.label,
])}
className={classNames(
classNamesTrigger,
'form-control form-control-select form-control-select-secondary',
{
'form-control-shrink': triggerShrink,
'form-control-sm': small,
'hidden-label': hideTriggerText,
}
)}
ref={ref}
>
<span className="inline-item-before">
<ClayIcon
spritemap={spritemap}
symbol={selectedItem.symbol}
/>
</span>

{!hideTriggerText ? (
<ClayTooltipProvider>
<button
{...otherProps}
aria-label={sub(triggerMessage, [
selectedItem?.name || selectedItem?.label,
])}
className={classNames(
classNamesTrigger,
'form-control form-control-select form-control-select-secondary',
{
'form-control-shrink': triggerShrink,
'form-control-sm': small,
'hidden-label': hideTriggerText,
}
)}
title={hideTriggerText ? selectedItem.label : null}
ref={ref}

Check failure on line 195 in packages/clay-core/src/language-picker/LanguagePicker.tsx

View workflow job for this annotation

GitHub Actions / test

Props should be sorted alphabetically
>
<span className="inline-item-before">
{selectedItem.label}
<ClayIcon
spritemap={spritemap}
symbol={selectedItem.symbol}
/>
</span>
) : null}
</button>

{!hideTriggerText ? (
<span className="inline-item-before">
{selectedItem.label}
</span>
) : null}
</button>
</ClayTooltipProvider>
);
}
);
Expand Down
132 changes: 46 additions & 86 deletions packages/clay-localized-input/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

import ClayButton from '@clayui/button';
import ClayDropDown from '@clayui/drop-down';
import type {Item} from '@clayui/core';

Check failure on line 6 in packages/clay-localized-input/src/index.tsx

View workflow job for this annotation

GitHub Actions / test

'Item' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 6 in packages/clay-localized-input/src/index.tsx

View workflow job for this annotation

GitHub Actions / test

imports must be sorted by module name (expected: "@clayui/core" << "@clayui/form" << "react" << (type) "@clayui/core")

import {LanguagePicker} from '@clayui/core';
import ClayForm, {ClayInput} from '@clayui/form';
import ClayIcon from '@clayui/icon';
import ClayLabel from '@clayui/label';
import ClayLayout from '@clayui/layout';
import React from 'react';
import React, {useMemo} from 'react';

interface IItem {
id?: string;
label: string;
name?: string;
symbol: string;
}

Expand Down Expand Up @@ -85,7 +85,7 @@
const LocalizedInput = React.forwardRef<HTMLInputElement, IProps>(
(
{
ariaLabels = {

Check failure on line 88 in packages/clay-localized-input/src/index.tsx

View workflow job for this annotation

GitHub Actions / test

'ariaLabels' is assigned a value but never used. Allowed unused vars must match /^_/u
default: 'Default',
openLocalizations: 'Open Localizations',
translated: 'Translated',
Expand All @@ -107,10 +107,28 @@
}: IProps,
ref
) => {
const [active, setActive] = React.useState(false);

const defaultLanguage = locales[0]!;

const languagePickerLocales = useMemo(() => {
return locales.map((locale) => ({
id: locale.label,
...locale,
}));
}, []);

const languagePickerTranslations = useMemo(() => {
const languagePickerTranslations: any = {};

locales.forEach((locale) => {
languagePickerTranslations[locale.label] = {
total: 1,
translated: translations[locale.label] ? 1 : 0,
};
});

return languagePickerTranslations;
}, [locales, translations]);

return (
<ClayForm.Group>
{label && <label htmlFor={id}>{label}</label>}
Expand All @@ -130,6 +148,13 @@
<ClayInput
{...otherProps}
id={id}
onBlur={(event) => {
onTranslationsChange({
...translations,
[selectedLocale.label]:
event.target.value.trim(),
});
}}
onChange={(event) => {
onTranslationsChange({
...translations,
Expand All @@ -144,85 +169,20 @@
</ClayInput.GroupItem>

<ClayInput.GroupItem shrink>
<ClayDropDown
active={active}
onActiveChange={setActive}
trigger={
<ClayButton
displayType="secondary"
monospaced
onClick={() => setActive(!active)}
title={ariaLabels.openLocalizations}
>
<span className="inline-item">
<ClayIcon
spritemap={spritemap}
symbol={selectedLocale.symbol}
/>
</span>
<span className="btn-section">
{selectedLocale.label}
</span>
</ClayButton>
<LanguagePicker
defaultLocaleId={defaultLanguage.id}
hideTriggerText
locales={languagePickerLocales}
onSelectedLocaleChange={(localeId: any) =>
onSelectedLocaleChange(
languagePickerLocales.find(
({id}) => id === localeId
)!
)
}
>
<ClayDropDown.ItemList>
{locales.map((locale) => {
const value = translations[locale.label];

return (
<ClayDropDown.Item
key={locale.label}
onClick={() =>
onSelectedLocaleChange(locale)
}
>
<ClayLayout.ContentRow containerElement="span">
<ClayLayout.ContentCol
containerElement="span"
expand
>
<ClayLayout.ContentSection>
<ClayIcon
className="inline-item inline-item-before"
spritemap={
spritemap
}
symbol={
locale.symbol
}
/>

{locale.label}
</ClayLayout.ContentSection>
</ClayLayout.ContentCol>
<ClayLayout.ContentCol containerElement="span">
<ClayLayout.ContentSection>
<ClayLabel
displayType={
locale.label ===
defaultLanguage.label
? 'info'
: value
? 'success'
: 'warning'
}
>
{locale.label ===
defaultLanguage.label
? ariaLabels.default
: value
? ariaLabels.translated
: ariaLabels.untranslated}
</ClayLabel>
</ClayLayout.ContentSection>
</ClayLayout.ContentCol>
</ClayLayout.ContentRow>
</ClayDropDown.Item>
);
})}
</ClayDropDown.ItemList>
</ClayDropDown>
spritemap={spritemap}
translations={languagePickerTranslations}
/>
</ClayInput.GroupItem>
</ClayInput.Group>

Expand Down
Loading