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

Replace hidden text in BooleanField by tooltip #4054

Merged
merged 1 commit into from
Nov 27, 2019
Merged
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
6 changes: 3 additions & 3 deletions docs/Fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ import { BooleanField } from 'react-admin';

![BooleanField](./img/boolean-field.png)

The `BooleanField` also includes an hidden text for accessibility (or to query in end to end tests). By default, it includes the translated label and the translated value, for example `Published: false`.
The `BooleanField` also includes a tooltip text for accessibility (or to query in end to end tests). By default, it is the translated value ('true' or 'false' in English).

If you need to override it, you can use the `valueLabelTrue` and `valueLabelFalse` props which both accept a string. Those strings may be translation keys:
If you need to override it, you can use the `valueLabelTrue` and `valueLabelFalse` props, which both accept a string. These strings may be translation keys:

```jsx
// Simple texts
// English labels
<BooleanField source="published" valueLabelTrue="Has been published" valueLabelFalse="Has not been published yet" />

// Translation keys
Expand Down
42 changes: 19 additions & 23 deletions packages/ra-ui-materialui/src/field/BooleanField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,50 @@ const defaultProps = {
describe('<BooleanField />', () => {
afterEach(cleanup);
it('should display tick and truthy text if value is true', () => {
const { queryByText } = render(<BooleanField {...defaultProps} />);
expect(queryByText('ra.boolean.true')).not.toBeNull();
expect(queryByText('ra.boolean.true').nextSibling.dataset.testid).toBe(
'true'
);
expect(queryByText('ra.boolean.false')).toBeNull();
const { queryByTitle } = render(<BooleanField {...defaultProps} />);
expect(queryByTitle('ra.boolean.true')).not.toBeNull();
expect(queryByTitle('ra.boolean.true').dataset.testid).toBe('true');
expect(queryByTitle('ra.boolean.false')).toBeNull();
});

it('should use valueLabelTrue for custom truthy text', () => {
const { queryByText } = render(
const { queryByTitle } = render(
<BooleanField
{...defaultProps}
valueLabelTrue="Has been published"
/>
);
expect(queryByText('ra.boolean.true')).toBeNull();
expect(queryByText('Has been published')).not.toBeNull();
expect(queryByTitle('ra.boolean.true')).toBeNull();
expect(queryByTitle('Has been published')).not.toBeNull();
});

it('should display cross and falsy text if value is false', () => {
const { queryByText } = render(
const { queryByTitle } = render(
<BooleanField {...defaultProps} record={{ published: false }} />
);
expect(queryByText('ra.boolean.true')).toBeNull();
expect(queryByText('ra.boolean.false')).not.toBeNull();
expect(queryByText('ra.boolean.false').nextSibling.dataset.testid).toBe(
'false'
);
expect(queryByTitle('ra.boolean.true')).toBeNull();
expect(queryByTitle('ra.boolean.false')).not.toBeNull();
expect(queryByTitle('ra.boolean.false').dataset.testid).toBe('false');
});

it('should use valueLabelFalse for custom falsy text', () => {
const { queryByText } = render(
const { queryByTitle } = render(
<BooleanField
{...defaultProps}
record={{ published: false }}
valueLabelFalse="Has not been published"
/>
);
expect(queryByText('ra.boolean.false')).toBeNull();
expect(queryByText('Has not been published')).not.toBeNull();
expect(queryByTitle('ra.boolean.false')).toBeNull();
expect(queryByTitle('Has not been published')).not.toBeNull();
});

it('should not display anything if value is null', () => {
const { queryByText } = render(
const { queryByTitle } = render(
<BooleanField {...defaultProps} record={{ published: null }} />
);
expect(queryByText('ra.boolean.true')).toBeNull();
expect(queryByText('ra.boolean.false')).toBeNull();
expect(queryByTitle('ra.boolean.true')).toBeNull();
expect(queryByTitle('ra.boolean.false')).toBeNull();
});

it('should use custom className', () => {
Expand All @@ -75,13 +71,13 @@ describe('<BooleanField />', () => {
});

it('should handle deep fields', () => {
const { queryByText } = render(
const { queryByTitle } = render(
<BooleanField
{...defaultProps}
record={{ foo: { bar: true } }}
source="foo.bar"
/>
);
expect(queryByText('ra.boolean.true')).not.toBeNull();
expect(queryByTitle('ra.boolean.true')).not.toBeNull();
});
});
42 changes: 8 additions & 34 deletions packages/ra-ui-materialui/src/field/BooleanField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,14 @@ import get from 'lodash/get';
import pure from 'recompose/pure';
import FalseIcon from '@material-ui/icons/Clear';
import TrueIcon from '@material-ui/icons/Done';
import Typography, { TypographyProps } from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import { Tooltip, Typography } from '@material-ui/core';
import { TypographyProps } from '@material-ui/core/Typography';
import compose from 'recompose/compose';
import { useTranslate } from 'ra-core';

import { FieldProps, InjectedFieldProps, fieldPropTypes } from './types';
import sanitizeRestProps from './sanitizeRestProps';

const useStyles = makeStyles(
{
label: {
// Move the text out of the flow of the container.
position: 'absolute',

// Reduce its height and width to just one pixel.
height: 1,
width: 1,

// Hide any overflowing elements or text.
overflow: 'hidden',

// Clip the box to zero pixels.
clip: 'rect(0, 0, 0, 0)',

// Text won't wrap to a second line.
whiteSpace: 'nowrap',
},
},
{ name: 'RaBooleanField' }
);

interface Props extends FieldProps {
valueLabelTrue?: string;
valueLabelFalse?: string;
Expand All @@ -51,7 +28,6 @@ export const BooleanField: FunctionComponent<
valueLabelFalse,
...rest
}) => {
const classes = useStyles({ classes: classesOverride });
const translate = useTranslate();
const value = get(record, source);
let ariaLabel = value ? valueLabelTrue : valueLabelFalse;
Expand All @@ -68,10 +44,9 @@ export const BooleanField: FunctionComponent<
className={className}
{...sanitizeRestProps(rest)}
>
<span className={classes.label}>
{translate(ariaLabel, { _: ariaLabel })}
</span>
<FalseIcon data-testid="false" />
<Tooltip title={translate(ariaLabel, { _: ariaLabel })}>
<FalseIcon data-testid="false" />
</Tooltip>
</Typography>
);
}
Expand All @@ -84,10 +59,9 @@ export const BooleanField: FunctionComponent<
className={className}
{...sanitizeRestProps(rest)}
>
<span className={classes.label}>
{translate(ariaLabel, { _: ariaLabel })}
</span>
<TrueIcon data-testid="true" />
<Tooltip title={translate(ariaLabel, { _: ariaLabel })}>
<TrueIcon data-testid="true" />
</Tooltip>
</Typography>
);
}
Expand Down