Skip to content
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

feat(React): update more components to use Text, improve RTL support #14679

Merged
Merged
8 changes: 7 additions & 1 deletion packages/react/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import React from 'react';
import { breakpoints } from '@carbon/layout';
import { GlobalTheme } from '../src/components/Theme';
import { Layout } from '../src/components/Layout';
import { TextDirection } from '../src/components/Text';

import theme from './theme';

Expand Down Expand Up @@ -322,7 +323,12 @@ const decorators = [
return (
<GlobalTheme theme={theme}>
<Layout size={layoutSize || null} density={layoutDensity || null}>
<Story key={randomKey} {...context} />
<TextDirection
getTextDirection={(text) => {
return dir;
}}>
<Story key={randomKey} {...context} />
</TextDirection>
</Layout>
</GlobalTheme>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports[`Accordion should render 1`] = `
</svg>
<div
class="cds--accordion__title"
dir="auto"
>
Heading A
</div>
Expand Down Expand Up @@ -68,6 +69,7 @@ exports[`Accordion should render 1`] = `
</svg>
<div
class="cds--accordion__title"
dir="auto"
>
Heading B
</div>
Expand Down Expand Up @@ -105,6 +107,7 @@ exports[`Accordion should render 1`] = `
</svg>
<div
class="cds--accordion__title"
dir="auto"
>
Heading C
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports[`AccordionItem renders as expected - Component API should render and mat
</svg>
<div
class="cds--accordion__title"
dir="auto"
>
Test title
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cx from 'classnames';
import Link from '../Link';
import { OverflowMenuHorizontal } from '@carbon/icons-react';
import { usePrefix } from '../../internal/usePrefix';
import { Text } from '../Text';

const BreadcrumbItem = React.forwardRef(function BreadcrumbItem(
{
Expand Down Expand Up @@ -60,7 +61,7 @@ const BreadcrumbItem = React.forwardRef(function BreadcrumbItem(
{children}
</Link>
) : (
<span className={`${prefix}--link`}>{children}</span>
<Text className={`${prefix}--link`}>{children}</Text>
)}
</li>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const TableBatchActions: TableBatchActionsComponent = ({
{...rest}>
<div className={batchSummaryClasses}>
<p className={`${prefix}--batch-summary__para`}>
<Text as="span">
<Text>
{totalSelected > 1 || totalSelected === 0
? t('carbon.table.batch.items.selected', { totalSelected })
: t('carbon.table.batch.item.selected', { totalSelected })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -110,6 +111,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -146,6 +148,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should not hav
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -240,6 +243,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -276,6 +280,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -312,6 +317,7 @@ exports[`DataTable behaves as expected selection -- radio buttons should render
/>
<span
class="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
</span>
Expand Down Expand Up @@ -365,7 +371,9 @@ exports[`DataTable behaves as expected selection should render and match snapsho
<p
class="cds--batch-summary__para"
>
<span>
<span
dir="auto"
>
0 items selected
</span>
</p>
Expand Down Expand Up @@ -802,7 +810,9 @@ exports[`DataTable renders as expected - Component API should render and match s
<p
class="cds--batch-summary__para"
>
<span>
<span
dir="auto"
>
0 items selected
</span>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { usePrefix } from '../../internal/usePrefix';
import { FormContext } from '../FluidForm';
import setupGetInstanceId from '../../tools/setupGetInstanceId';
import { ReactAttr } from '../../types/common';
import { Text } from '../Text';

const getInstanceId = setupGetInstanceId();
type ExcludedAttributes = 'value' | 'onChange' | 'locale' | 'children';
Expand Down Expand Up @@ -211,9 +212,9 @@ const DatePickerInput = React.forwardRef(function DatePickerInput(
return (
<div className={containerClasses}>
{labelText && (
<label htmlFor={id} className={labelClasses}>
<Text as="label" htmlFor={id} className={labelClasses}>
{labelText}
</label>
</Text>
)}
<div className={wrapperClasses}>
{input}
Expand All @@ -227,19 +228,26 @@ const DatePickerInput = React.forwardRef(function DatePickerInput(
{invalid && (
<>
{isFluid && <hr className={`${prefix}--date-picker__divider`} />}
<div className={`${prefix}--form-requirement`}>{invalidText}</div>
<Text as="div" className={`${prefix}--form-requirement`}>
{invalidText}
</Text>
</>
)}
{warn && (
<>
{isFluid && <hr className={`${prefix}--date-picker__divider`} />}
<div className={`${prefix}--form-requirement`}>{warnText}</div>
<Text as="div" className={`${prefix}--form-requirement`}>
{warnText}
</Text>
</>
)}
{helperText && (
<div id={datePickerInputHelperId} className={helperTextClasses}>
{helperText && !invalid && (
<Text
as="div"
id={datePickerInputHelperId}
className={helperTextClasses}>
{helperText}
</div>
</Text>
)}
</div>
);
Expand Down
17 changes: 11 additions & 6 deletions packages/react/src/components/FileUploader/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ButtonKinds } from '../../prop-types/types';
import { keys, matches } from '../../internal/keyboard';
import { PrefixContext } from '../../internal/usePrefix';
import { ReactAttr } from '../../types/common';
import { Text } from '../Text';

export interface FileUploaderProps extends ReactAttr<HTMLSpanElement> {
/**
Expand Down Expand Up @@ -296,13 +297,17 @@ export default class FileUploader extends React.Component<
return (
<div className={classes} {...other}>
{!labelTitle ? null : (
<p className={getHelperLabelClasses(`${prefix}--file--label`)}>
<Text
as="p"
className={getHelperLabelClasses(`${prefix}--file--label`)}>
{labelTitle}
</p>
</Text>
)}
<p className={getHelperLabelClasses(`${prefix}--label-description`)}>
<Text
as="p"
className={getHelperLabelClasses(`${prefix}--label-description`)}>
{labelDescription}
</p>
</Text>
<FileUploaderButton
innerRef={this.uploaderButton}
disabled={disabled}
Expand All @@ -324,9 +329,9 @@ export default class FileUploader extends React.Component<
className={selectedFileClasses}
ref={(node) => (this.nodes[index] = node as HTMLSpanElement)} // eslint-disable-line
{...other}>
<p className={`${prefix}--file-filename`} id={name}>
<Text as="p" className={`${prefix}--file-filename`} id={name}>
{name}
</p>
</Text>
<span className={`${prefix}--file__state-container`}>
<Filename
name={name}
Expand Down
17 changes: 11 additions & 6 deletions packages/react/src/components/FileUploader/FileUploaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { keys, matches } from '../../internal/keyboard';
import uid from '../../tools/uniqueId';
import { usePrefix } from '../../internal/usePrefix';
import { ReactAttr } from '../../types/common';
import { Text } from '../Text';

export interface FileUploaderItemProps extends ReactAttr<HTMLSpanElement> {
/**
Expand Down Expand Up @@ -87,9 +88,13 @@ function FileUploaderItem({
});
return (
<span className={classes} {...other}>
<p className={`${prefix}--file-filename`} title={name} id={name}>
<Text
as="p"
className={`${prefix}--file-filename`}
title={name}
id={name}>
{name}
</p>
</Text>
<span className={`${prefix}--file__state-container`}>
<Filename
name={name}
Expand Down Expand Up @@ -117,13 +122,13 @@ function FileUploaderItem({
className={`${prefix}--form-requirement`}
role="alert"
id={`${name}-id-error`}>
<div className={`${prefix}--form-requirement__title`}>
<Text as="div" className={`${prefix}--form-requirement__title`}>
{errorSubject}
</div>
</Text>
{errorBody && (
<p className={`${prefix}--form-requirement__supplement`}>
<Text as="p" className={`${prefix}--form-requirement__supplement`}>
{errorBody}
</p>
</Text>
)}
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ import FileUploader from '../';
import { uploadFiles } from '../test-helpers';

const iconDescription = 'test description';
const requiredProps = { iconDescription };
const requiredProps = {
iconDescription,
labelTitle: 'Upload files',
labelDescription: 'Max file size is 500mb. Only .jpg files are supported.',
};

describe('FileUploader', () => {
describe('automated accessibility tests', () => {
it.skip('should have no axe violations', async () => {
const { container } = render(<FileUploader {...requiredProps} />);
await expect(container).toHaveNoAxeViolations();
});

it.skip('should have no AC violations', async () => {
const { container } = render(<FileUploader {...requiredProps} />);
await expect(container).toHaveNoACViolations('FileUploader');
});
});

it('should support a custom class name on the root element', () => {
const { container } = render(
<FileUploader {...requiredProps} className="test" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ describe('FluidTextInput', () => {

it('should respect invalid prop', () => {
const { container } = render(
<FluidTextInput id="input-1" labelText="FluidTextInput" invalid />
<FluidTextInput
id="input-1"
labelText="FluidTextInput"
invalid
invalidText="Invalid"
/>
);

// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
Expand Down Expand Up @@ -172,7 +177,12 @@ describe('FluidTextInput', () => {

it('should respect warn prop', () => {
const { container } = render(
<FluidTextInput id="input-1" labelText="FluidTextInput label" warn />
<FluidTextInput
id="input-1"
labelText="FluidTextInput label"
warn
warnText="Warning"
/>
);

// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
Expand Down
8 changes: 5 additions & 3 deletions packages/react/src/components/FormLabel/FormLabel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ import FormLabel from '../FormLabel';

describe('FormLabel', () => {
it('should support a custom `className` prop on the outermost element', () => {
const { container } = render(<FormLabel className="test" />);
const { container } = render(<FormLabel className="test">Label</FormLabel>);
expect(container.firstChild).toHaveClass('test');
});

it('should spread extra props on the outermost element', () => {
const { container } = render(
<FormLabel aria-label="test" data-testid="test" />
<FormLabel aria-label="test" data-testid="test">
Label
</FormLabel>
);
expect(container.firstChild).toHaveAttribute('data-testid', 'test');
});

it('should support a unique id prop on the outermost element', () => {
const { container } = render(<FormLabel id="test-1" />);
const { container } = render(<FormLabel id="test-1">Label</FormLabel>);
expect(container.firstChild).toHaveProperty('htmlFor', 'test-1');
});
});
5 changes: 3 additions & 2 deletions packages/react/src/components/FormLabel/FormLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import PropTypes from 'prop-types';
import React from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix';
import { Text } from '../Text';

export interface FormLabelProps
extends Omit<React.LabelHTMLAttributes<HTMLLabelElement>, 'htmlFor'> {
Expand Down Expand Up @@ -42,9 +43,9 @@ function FormLabel({
);

return (
<label htmlFor={id} className={className} {...rest}>
<Text as="label" htmlFor={id} className={className} {...rest}>
{children}
</label>
</Text>
);
}

Expand Down
Loading
Loading