-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[RFR] BooleanField accessibility #2744
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import inflection from 'inflection'; | ||
|
|
||
| /** | ||
| * Returns an array of arguments to use with the translate function for the label of a field. | ||
| * The label will be the one specified by the label prop or one computed from the resource and source props. | ||
| * | ||
| * Usage: | ||
| * <span> | ||
| * {translate(...getFieldLabelTranslationArgs(label, resource, source))} | ||
| * </span> | ||
| */ | ||
| export default options => { | ||
| if (!options) { | ||
| return ['']; | ||
| } | ||
|
|
||
| const { label, resource, source } = options; | ||
|
|
||
| return typeof label !== 'undefined' | ||
| ? [label, { _: label }] | ||
| : typeof source !== 'undefined' | ||
| ? [`resources.${resource}.fields.${source}`, { | ||
| _: inflection.transform(source, [ | ||
| 'underscore', | ||
| 'humanize', | ||
| ]) | ||
| }] | ||
| : ['']; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import assert from 'assert'; | ||
| import getFieldLabelTranslationArgs from './getFieldLabelTranslationArgs'; | ||
|
|
||
| describe('getFieldLabelTranslationArgs', () => { | ||
| it('should return empty span by default', () => | ||
| assert.deepEqual(getFieldLabelTranslationArgs(), [''])); | ||
|
|
||
| it('should return the label when given', () => | ||
| assert.deepEqual( | ||
| getFieldLabelTranslationArgs({ | ||
| label: 'foo', | ||
| resource: 'posts', | ||
| source: 'title', | ||
| }), | ||
| ['foo', { _: 'foo' }] | ||
| )); | ||
|
|
||
| it('should return the humanized source when given', () => { | ||
| assert.deepEqual( | ||
| getFieldLabelTranslationArgs({ | ||
| resource: 'posts', | ||
| source: 'title', | ||
| }), | ||
| [`resources.posts.fields.title`, { _: 'Title' }] | ||
| ); | ||
|
|
||
| assert.deepEqual( | ||
| getFieldLabelTranslationArgs({ | ||
| resource: 'posts', | ||
| source: 'title_with_underscore', | ||
| }), | ||
| [ | ||
| `resources.posts.fields.title_with_underscore`, | ||
| { _: 'Title with underscore' }, | ||
| ] | ||
| ); | ||
|
|
||
| assert.deepEqual( | ||
| getFieldLabelTranslationArgs({ | ||
| resource: 'posts', | ||
| source: 'titleWithCamelCase', | ||
| }), | ||
| [ | ||
| `resources.posts.fields.titleWithCamelCase`, | ||
| { _: 'Title with camel case' }, | ||
| ] | ||
| ); | ||
| }); | ||
|
|
||
| it('should return the source and resource as translate key', () => | ||
| assert.deepEqual( | ||
| getFieldLabelTranslationArgs({ | ||
| resource: 'posts', | ||
| source: 'title', | ||
| }), | ||
| [`resources.posts.fields.title`, { _: 'Title' }] | ||
| )); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,31 +5,76 @@ import pure from 'recompose/pure'; | |
| import FalseIcon from '@material-ui/icons/Clear'; | ||
| import TrueIcon from '@material-ui/icons/Done'; | ||
| import Typography from '@material-ui/core/Typography'; | ||
| import { createStyles, withStyles } from '@material-ui/core/styles'; | ||
| import compose from 'recompose/compose'; | ||
| import { translate } from 'ra-core'; | ||
|
|
||
| import sanitizeRestProps from './sanitizeRestProps'; | ||
|
|
||
| export const BooleanField = ({ className, source, record = {}, ...rest }) => { | ||
| if (get(record, source) === false) { | ||
| const styles = createStyles({ | ||
| 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', | ||
| } | ||
| }); | ||
|
|
||
| export const BooleanField = ({ | ||
| className, | ||
| classes, | ||
| source, | ||
| record = {}, | ||
| translate, | ||
| valueLabelTrue, | ||
| valueLabelFalse, | ||
| ...rest | ||
| }) => { | ||
| const value = get(record, source); | ||
| let ariaLabel = value | ||
| ? valueLabelTrue | ||
| : valueLabelFalse; | ||
|
|
||
| if (!ariaLabel) { | ||
| ariaLabel = value === false | ||
| ? translate('ra.boolean.false') | ||
| : translate('ra.boolean.true'); | ||
| } | ||
|
|
||
| if (value === false) { | ||
| return ( | ||
| <Typography | ||
| component="span" | ||
| body1="body1" | ||
| className={className} | ||
| {...sanitizeRestProps(rest)} | ||
| > | ||
| <span className={classes.label}>{ariaLabel}</span> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this span since we have an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must say I'm not sure about the best practices here. I think label elements should only be used with interactive elements such as inputs or buttons. It made sense for me that it was true for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the |
||
| <FalseIcon /> | ||
| </Typography> | ||
| ); | ||
| } | ||
|
|
||
| if (get(record, source) === true) { | ||
| if (value === true) { | ||
| return ( | ||
| <Typography | ||
| component="span" | ||
| body1="body1" | ||
| className={className} | ||
| {...sanitizeRestProps(rest)} | ||
| > | ||
| <span className={classes.label}>{ariaLabel}</span> | ||
| <TrueIcon /> | ||
| </Typography> | ||
| ); | ||
|
|
@@ -55,9 +100,20 @@ BooleanField.propTypes = { | |
| record: PropTypes.object, | ||
| sortBy: PropTypes.string, | ||
| source: PropTypes.string.isRequired, | ||
| valueLabelTrue: PropTypes.string, | ||
| valueLabelFalse: PropTypes.string, | ||
| }; | ||
|
|
||
| BooleanField.defaultProps = { | ||
| classes: {}, | ||
| translate: x => x, | ||
| }; | ||
|
|
||
| const PureBooleanField = pure(BooleanField); | ||
| const PureBooleanField = compose( | ||
| pure, | ||
| withStyles(styles), | ||
| translate, | ||
| )(BooleanField); | ||
|
|
||
| PureBooleanField.defaultProps = { | ||
| addLabel: true, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.