Skip to content

Commit

Permalink
✨ feat(i18n): transfrom select options
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Apr 27, 2024
1 parent 0a92d80 commit aab7a31
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/frontend/components/IconInputField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function IconInputField({ value }: { value: string }) {
required
selectData={SystemIconsList.map((icon) => ({
value: icon,
label: userFriendlyCase(icon),
label: msg`${userFriendlyCase(icon)}`,
}))}
meta={meta}
input={input}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import debounce from "lodash/debounce";
import AsyncSelect from "react-select/async";
import styled from "styled-components";
import { useAsync, useSessionStorage } from "react-use";
import { ISelectData } from "shared/types/options";
import { ILabelValue, ISelectData } from "shared/types/options";
import { useApi } from "frontend/lib/data/useApi";
import { ApiRequest } from "frontend/lib/data/makeRequest";
import { useLingui } from "@lingui/react";
Expand Down Expand Up @@ -139,7 +139,7 @@ export function AsyncFormMultiSelect({
values = [],
onChange,
}: IFormMultiSelect) {
const [cosmeticValues, setCosmeticValues] = useSessionStorage<ISelectData[]>(
const [cosmeticValues, setCosmeticValues] = useSessionStorage<ILabelValue[]>(
"cosmetic-multi-select-values",
values.map((value) => ({ value, label: value }))
);
Expand All @@ -153,9 +153,9 @@ export function AsyncFormMultiSelect({
isMulti
value={cosmeticValues}
onChange={(newValues: unknown) => {
setCosmeticValues(newValues as ISelectData[]);
setCosmeticValues(newValues as ILabelValue[]);
onChange(
(newValues as ISelectData[]).map(({ value }) => value as string)
(newValues as ILabelValue[]).map(({ value }) => value as string)
);
}}
loadOptions={(inputValue) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styled, { css } from "styled-components";

import { USE_ROOT_COLOR } from "frontend/design-system/theme/root";
import { ISelectData } from "shared/types/options";
import { useLingui } from "@lingui/react";
import { Input } from "../Styles";

interface ISimpleSelect {
Expand Down Expand Up @@ -41,6 +42,8 @@ export function SimpleSelect({
width,
ariaLabel,
}: ISimpleSelect) {
const { _ } = useLingui();

return (
<SimpleSelectStyled
as="select"
Expand All @@ -55,7 +58,7 @@ export function SimpleSelect({
>
{options.map(({ value: value$1, label }) => (
<option key={`${value$1}`} value={`${value$1}`}>
{label}
{_(label)}
</option>
))}
</SimpleSelectStyled>
Expand Down
24 changes: 14 additions & 10 deletions src/frontend/design-system/components/Form/FormSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Select from "react-select";
import styled from "styled-components";
import { ISelectData } from "shared/types/options";
import { ILabelValue, ISelectData } from "shared/types/options";
import { useLingui } from "@lingui/react";
import {
generateClassNames,
Expand Down Expand Up @@ -31,6 +31,8 @@ export function FormMultiSelect({
onChange,
ariaLabel,
}: IFormMultiSelect) {
const { _ } = useLingui();

return (
<SelectStyled
classNamePrefix={SharedSelectProps.classNamePrefix}
Expand All @@ -44,7 +46,10 @@ export function FormMultiSelect({
onChange(newValues.map(({ value }: ISelectData) => value));
}}
aria-label={ariaLabel}
options={selectData}
options={selectData.map(({ value, label }) => ({
value,
label: _(label),
}))}
/>
);
}
Expand All @@ -68,8 +73,8 @@ export const FormSelect = (props: IFormSelect) => {
value: nullable ? null : "",
label: defaultLabel || `--- Select ${_(formLabel)} ---`,
},
...selectData,
] as ISelectData[];
...selectData.map(({ value, label }) => ({ value, label: _(label) })),
];
return wrapLabelAndError(
<div data-testid={`react-select__${input.name}`}>
<SelectStyled
Expand Down Expand Up @@ -117,18 +122,17 @@ export function FormNoValueSelect({
defaultLabel,
onChange,
}: IFormNoValueSelect) {
const { _ } = useLingui();
return (
<SelectStyled
classNamePrefix={SharedSelectProps.classNamePrefix}
value={{ value: "", label: defaultLabel || "" }}
onChange={({ value, label }: any) => {
onChange={({ value, label }: ILabelValue) => {
onChange(value, label);
}}
options={
selectData.filter(
({ value }) => !disabledOptions.includes(value as string)
) as { value: string; label: string }[]
}
options={selectData
.filter(({ value }) => !disabledOptions.includes(value as string))
.map(({ value, label }) => ({ value, label: _(label) }))}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from "styled-components";
import { sluggify } from "shared/lib/strings";
import { ISelectData } from "shared/types/options";
import { useLingui } from "@lingui/react";
import { OutlineButton } from "../../Button/Button";
import { IBaseFormSelect } from "../FormSelect/types";
import { generateFormArias, wrapLabelAndError } from "../_wrapForm";
Expand Down Expand Up @@ -44,6 +45,7 @@ function BaseFormSelectButton({
sm,
disabled,
}: IProps) {
const { _ } = useLingui();
return (
<Root>
{options.map(({ value, label }, index) => {
Expand Down Expand Up @@ -71,7 +73,7 @@ function BaseFormSelectButton({
readOnly
checked={isChecked}
/>
{label}
{_(label)}
</OutlineButton>
);
})}
Expand Down
26 changes: 13 additions & 13 deletions src/frontend/design-system/components/Form/Stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ function DemoForm() {
{(formProps) => (
<FormSelectButton
selectData={[
{ label: "Wood", value: "wood" },
{ label: "Bronze", value: "bronze" },
{ label: "Silver", value: "silver" },
{ label: "Gold", value: "gold" },
{ label: fakeMessageDescriptor("Wood"), value: "wood" },
{ label: fakeMessageDescriptor("Bronze"), value: "bronze" },
{ label: fakeMessageDescriptor("Silver"), value: "silver" },
{ label: fakeMessageDescriptor("Gold"), value: "gold" },
]}
label={fakeMessageDescriptor("Example Select Button")}
{...formProps}
Expand All @@ -183,8 +183,8 @@ function DemoForm() {
{(formProps) => (
<FormSelect
selectData={[
{ label: "Foo", value: "foo" },
{ label: "Bar", value: "bar" },
{ label: fakeMessageDescriptor("Foo"), value: "foo" },
{ label: fakeMessageDescriptor("Bar"), value: "bar" },
]}
label={fakeMessageDescriptor("Example Select Input")}
{...formProps}
Expand All @@ -200,8 +200,8 @@ function DemoForm() {
{(formProps) => (
<FormSelect
selectData={[
{ label: "Foo", value: "foo" },
{ label: "Bar", value: "bar" },
{ label: fakeMessageDescriptor("Foo"), value: "foo" },
{ label: fakeMessageDescriptor("Bar"), value: "bar" },
]}
label={fakeMessageDescriptor("Disabled Select Input")}
{...formProps}
Expand All @@ -225,11 +225,11 @@ function DemoForm() {
{(formProps) => (
<FormMultiSelect
selectData={[
{ label: "Foo", value: "foo" },
{ label: "Bar", value: "bar" },
{ label: "Baz", value: "baz" },
{ label: "Noop", value: "noop" },
{ label: "Dupe", value: "dupe" },
{ label: fakeMessageDescriptor("Foo"), value: "foo" },
{ label: fakeMessageDescriptor("Bar"), value: "bar" },
{ label: fakeMessageDescriptor("Baz"), value: "baz" },
{ label: fakeMessageDescriptor("Noop"), value: "noop" },
{ label: fakeMessageDescriptor("Dupe"), value: "dupe" },
]}
values={formProps.input.value || ["foo", "bar"]}
onChange={formProps.input.onChange}
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/design-system/components/Table/_Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import styled from "styled-components";
import { USE_ROOT_COLOR } from "frontend/design-system/theme/root";
import { Stack } from "frontend/design-system/primitives/Stack";
import { Typo } from "frontend/design-system/primitives/Typo";
import { Trans } from "@lingui/macro";
import { Trans, msg } from "@lingui/macro";
import { ChevronLeft, ChevronRight } from "react-feather";
import { i18nNoop } from "shared/lib/noop";
import { SimpleSelect } from "../Form/FormSelect/Simple";
import { TABLE_PAGE_SIZES } from "./constants";

Expand Down Expand Up @@ -91,7 +92,7 @@ export function TablePagination({
width={55}
options={TABLE_PAGE_SIZES.map((option) => ({
value: `${option}`,
label: `${option}`,
label: msg`${i18nNoop(option)}`,
}))}
onChange={(value) => setPageSize(Number(value))}
value={pageSize}
Expand Down
36 changes: 30 additions & 6 deletions src/frontend/design-system/components/Table/data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ export const TABLE_COLUMNS: ITableColumn[] = [
filter: {
_type: "boolean",
bag: [
{ color: "#00ff00", label: "Yes", value: "true" },
{ color: "#ff0000", label: "No", value: "false" },
{
color: "#00ff00",
label: fakeMessageDescriptor("Yes"),
value: "true",
},
{
color: "#ff0000",
label: fakeMessageDescriptor("No"),
value: "false",
},
],
},
},
Expand All @@ -79,10 +87,26 @@ export const TABLE_COLUMNS: ITableColumn[] = [
filter: {
_type: "status",
bag: [
{ color: "#00ff00", label: "Admin", value: "admin" },
{ color: "#fff000", label: "Editor", value: "editor" },
{ color: "#fff000", label: "User", value: "user" },
{ color: "#ff00f0", label: "Developer", value: "developer" },
{
color: "#00ff00",
label: fakeMessageDescriptor("Admin"),
value: "admin",
},
{
color: "#fff000",
label: fakeMessageDescriptor("Editor"),
value: "editor",
},
{
color: "#fff000",
label: fakeMessageDescriptor("User"),
value: "user",
},
{
color: "#ff00f0",
label: fakeMessageDescriptor("Developer"),
value: "developer",
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FilterOperators, IColumnFilterBag } from "shared/types/data";
import { ISelectData } from "shared/types/options";
import { msg } from "@lingui/macro";
import { SimpleSelect } from "../../Form/FormSelect/Simple";
import { IFilterProps } from "./types";

Expand All @@ -9,7 +10,7 @@ export function FilterTableByBooleans({
}: IFilterProps<IColumnFilterBag<boolean>, ISelectData[]>) {
return (
<SimpleSelect
options={[{ label: "-- Select State --", value: "" }, ...bag]}
options={[{ label: msg`-- Select State --`, value: "" }, ...bag]}
onChange={(value: string) => {
setFilter({
operator: FilterOperators.EQUAL_TO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,7 @@ import { useEffect } from "react";
import { FilterOperators, IColumnFilterBag } from "shared/types/data";
import { Spacer } from "frontend/design-system/primitives/Spacer";
import { SimpleSelect } from "../../Form/FormSelect/Simple";

const FILTER_OPERATOR_LABELS: Record<FilterOperators, string> = {
[FilterOperators.EQUAL_TO]: "Equal To",
[FilterOperators.GREATER_THAN]: "Greater Than",
[FilterOperators.LESS_THAN]: "Less Than",
[FilterOperators.NOT_IN]: "Not In",
[FilterOperators.IN]: "In",
[FilterOperators.DATE]: "Date",
[FilterOperators.BETWEEN]: "Between",
[FilterOperators.CONTAINS]: "Contains",
[FilterOperators.NOT_EQUAL]: "Not Equal To",
[FilterOperators.IS_NULL]: "Is Null",
[FilterOperators.IS_NOT_NULL]: "Is Not Null",
};
import { FILTER_OPERATOR_CONFIG } from "./constants";

interface IProps<T> {
operators: FilterOperators[];
Expand All @@ -42,7 +29,7 @@ export function RenderFilterOperator<T>({
options={[
...operators.map((operator) => ({
value: operator,
label: FILTER_OPERATOR_LABELS[operator],
label: FILTER_OPERATOR_CONFIG[operator].label,
})),
]}
ariaLabel="Select Filter Operator"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "shared/types/data";
import { ApplicationRoot } from "frontend/components/ApplicationRoot";
import { USE_ROUTER_PARAMS } from "__tests__/_/constants";
import { fakeMessageDescriptor } from "translations/fake";
import { TableFilter } from "..";

const setFilterValueJestFn = jest.fn();
Expand Down Expand Up @@ -303,11 +304,11 @@ describe("Table Filters", () => {
_type: "boolean",
bag: [
{
label: "True Option",
label: fakeMessageDescriptor("True Option"),
value: true,
},
{
label: "False Option",
label: fakeMessageDescriptor("False Option"),
value: false,
},
],
Expand Down Expand Up @@ -415,15 +416,15 @@ describe("Table Filters", () => {
_type: "status",
bag: [
{
label: "Option 1 Label",
label: fakeMessageDescriptor("Option 1 Label"),
value: "option-1",
},
{
label: "Option 2 Label",
label: fakeMessageDescriptor("Option 2 Label"),
value: "option-2",
},
{
label: "Option 3 Label",
label: fakeMessageDescriptor("Option 3 Label"),
value: "option-3",
},
],
Expand Down
Loading

0 comments on commit aab7a31

Please sign in to comment.