Skip to content

Commit

Permalink
Add translations for ImageInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Nov 25, 2024
1 parent 38fe035 commit 9b6b3ef
Show file tree
Hide file tree
Showing 46 changed files with 229 additions and 45 deletions.
16 changes: 8 additions & 8 deletions packages/circuit-ui/components/ImageInput/ImageInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { ImageInput, type ImageInputProps } from './ImageInput.js';

const defaultProps: ImageInputProps = {
label: 'Upload an image',
loadingLabel: 'Uploading',
clearButtonLabel: 'Clear',
onChange: () => Promise.resolve(),
onClear: () => {},
component: (props) => <Avatar {...props} alt="" />,
Expand All @@ -46,7 +44,7 @@ describe('ImageInput', () => {
.mockResolvedValue('/images/illustration-coffee.jpg');
const mockClearFn = vi.fn();

function StatefulInput() {
function StatefulInput(props: Partial<ImageInputProps>) {
const [imageUrl, setImageUrl] = useState<string>('');
const [error, setError] = useState<string>('');

Expand All @@ -68,15 +66,14 @@ describe('ImageInput', () => {

return (
<ImageInput
{...props}
label="Upload an image"
clearButtonLabel="Clear"
src={imageUrl}
onChange={onChange}
onClear={onClear}
invalid={!!error}
validationHint={error}
loadingLabel="Uploading"
component={(props) => <Avatar {...props} alt="" />}
component={(componentProps) => <Avatar {...componentProps} alt="" />}
/>
);
}
Expand Down Expand Up @@ -148,7 +145,10 @@ describe('ImageInput', () => {
});

it('should clear an uploaded image', async () => {
const { container } = render(<StatefulInput />);
const clearButtonLabel = 'Clear file';
const { container } = render(
<StatefulInput clearButtonLabel={clearButtonLabel} />,
);
const inputEl = screen.getByLabelText(defaultProps.label);

await userEvent.upload(inputEl, file);
Expand All @@ -160,7 +160,7 @@ describe('ImageInput', () => {
);

await userEvent.click(
screen.getByRole('button', { name: defaultProps.clearButtonLabel }),
screen.getByRole('button', { name: clearButtonLabel }),
);

await waitFor(() => {
Expand Down
14 changes: 0 additions & 14 deletions packages/circuit-ui/components/ImageInput/ImageInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,18 @@ export const Base = (args: ImageInputProps) => <ImageInput {...args} />;

Base.args = {
label: 'Upload an image',
clearButtonLabel: 'Clear',
onChange: () => Promise.resolve(),
onClear: () => {},
loadingLabel: 'Uploading',
component: Avatar,
disabled: false,
};

export const WithImage = () => (
<ImageInput
label="Upload an image"
clearButtonLabel="Clear"
src="/images/illustration-coffee.jpg"
onChange={() => Promise.resolve()}
onClear={() => {}}
loadingLabel="Uploading"
component={(props) => <Avatar {...props} alt="" />}
/>
);
Expand All @@ -55,13 +51,11 @@ export const Invalid = (args: ImageInputProps) => (
<ImageInput
{...args}
label="Upload an image"
clearButtonLabel="Clear"
src="/images/illustration-coffee.jpg"
onChange={() => Promise.resolve()}
onClear={() => {}}
invalid
validationHint="The uploaded image exceeds the maximum allowed size. Please use an image with a size below 20MB."
loadingLabel="Uploading"
component={(props) => <Avatar {...props} alt="" />}
/>
);
Expand All @@ -70,11 +64,9 @@ export const Disabled = (args: ImageInputProps) => (
<ImageInput
{...args}
label="Upload an image"
clearButtonLabel="Clear"
onChange={() => Promise.resolve()}
onClear={() => {}}
disabled
loadingLabel="Uploading"
component={(props) => <Avatar {...props} alt="" />}
hideLabel={false}
/>
Expand Down Expand Up @@ -123,13 +115,11 @@ export const Stateful = () => {
return (
<ImageInput
label="Upload an image"
clearButtonLabel="Clear"
src={imageUrl}
onChange={onChange}
onClear={onClear}
invalid={!!error}
validationHint={error}
loadingLabel="Uploading"
component={(props) => <Avatar {...props} alt="" />}
/>
);
Expand All @@ -138,10 +128,8 @@ export const Stateful = () => {
export const CustomComponentImg = () => (
<ImageInput
label="Upload an image"
clearButtonLabel="Clear"
onChange={() => Promise.resolve()}
onClear={() => {}}
loadingLabel="Uploading"
component={({ src, ...props }) => (
<img
style={{
Expand All @@ -168,10 +156,8 @@ CustomComponentImg.storyName = 'Custom Component (with an img element)';
export const CustomComponentDiv = () => (
<ImageInput
label="Upload an image"
clearButtonLabel="Clear"
onChange={() => Promise.resolve()}
onClear={() => {}}
loadingLabel="Uploading"
component={({ src, ...props }) => (
<div
{...props}
Expand Down
50 changes: 27 additions & 23 deletions packages/circuit-ui/components/ImageInput/ImageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ import {
isSufficientlyLabelled,
} from '../../util/errors.js';
import { clsx } from '../../styles/clsx.js';
import { useI18n } from '../../hooks/useI18n/useI18n.js';

import classes from './ImageInput.module.css';
import { translations } from './translations/index.js';

export interface ImageInputProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'size' | 'onChange'> {
Expand All @@ -68,11 +70,11 @@ export interface ImageInputProps
/**
* An accessible label for the "clear" icon button.
*/
clearButtonLabel: string;
clearButtonLabel?: string;
/**
* An accessible label to communicate the input's loading state.
*/
loadingLabel: string;
loadingLabel?: string;
/**
* The source URL of an existing Avatar to be displayed in the ImageInput.
*/
Expand Down Expand Up @@ -104,26 +106,28 @@ export interface ImageInputProps
/**
* The ImageInput component allows users to upload images.
*/
export const ImageInput = ({
label,
src,
id: customId,
clearButtonLabel,
onChange,
onClear,
disabled,
validationHint,
required,
invalid = false,
optionalLabel,
loadingLabel,
hideLabel = true,
component: Component,
className,
style,
'aria-describedby': descriptionId,
...props
}: ImageInputProps) => {
export const ImageInput = (props: ImageInputProps) => {
const {
label,
src,
id: customId,
clearButtonLabel,
onChange,
onClear,
disabled,
validationHint,
required,
invalid = false,
optionalLabel,
loadingLabel,
hideLabel = true,
component: Component,
className,
style,
'aria-describedby': descriptionId,
...rest
} = useI18n(props, translations);

if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test'
Expand Down Expand Up @@ -264,7 +268,7 @@ export const ImageInput = ({
disabled={disabled || isLoading}
aria-invalid={invalid && 'true'}
aria-describedby={descriptionIds}
{...props}
{...rest}
/>
<FieldLabel
htmlFor={inputId}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Изчистване",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Vymazat",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Ryd",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Auswahl aufheben",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Auswahl aufheben",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Auswahl aufheben",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Auswahl aufheben",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Εκκαθάριση",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Εκκαθάριση",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Clear",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Clear",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Clear",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Clear",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Clear",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Borrar",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Tühjenda",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Tyhjennä",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Réinitialiser",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Réinitialiser",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Réinitialiser",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Réinitialiser",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Očisti",
"loadingLabel": "Uploading..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Törlés",
"loadingLabel": "Uploading..."
}
26 changes: 26 additions & 0 deletions packages/circuit-ui/components/ImageInput/translations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { transformModulesToTranslations } from '../../../util/i18n.js';

export const translations = transformModulesToTranslations<
typeof import('./en-US.json')
>(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore import.meta.glob is supported by Vite
import.meta.glob('./*.json', {
eager: true,
}),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clearButtonLabel": "Cancella",
"loadingLabel": "Uploading..."
}
Loading

0 comments on commit 9b6b3ef

Please sign in to comment.