Skip to content

Commit

Permalink
[RFR] Automatically add id attribute on input if not specified and ac…
Browse files Browse the repository at this point in the history
…cessibility fixes
  • Loading branch information
djhi committed Sep 25, 2018
1 parent bdeb8f4 commit 6647d59
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const Button = ({
className={classnames(classes.button, className)}
color={color}
size={size}
aria-label={label && translate(label, { _: label })}
{...rest}
>
{alignIcon === 'left' &&
Expand Down
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/button/CreateButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const CreateButton = ({
color="primary"
className={classnames(classes.floating, className)}
to={`${basePath}/create`}
aria-label={label && translate(label)}
{...rest}
>
<ContentAdd />
Expand Down
8 changes: 7 additions & 1 deletion packages/ra-ui-materialui/src/form/FormInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ export const FormInput = ({ classes, input, ...rest }) =>
)}
>
{input.props.addLabel ? (
<Labeled {...input.props} {...sanitizeRestProps(rest)}>
<Labeled
id={input.props.id || input.props.source}
{...input.props}
{...sanitizeRestProps(rest)}
>
{React.cloneElement(input, {
className: classnames(
{
[classes.input]: !input.props.fullWidth,
},
input.props.className
),
id: input.props.id || input.props.source,
...rest,
})}
</Labeled>
Expand All @@ -40,6 +45,7 @@ export const FormInput = ({ classes, input, ...rest }) =>
},
input.props.className
),
id: input.props.id || input.props.source,
...rest,
})
)}
Expand Down
4 changes: 4 additions & 0 deletions packages/ra-ui-materialui/src/input/BooleanInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class BooleanInput extends Component {
render() {
const {
className,
id,
input,
isRequired,
label,
Expand All @@ -29,8 +30,10 @@ export class BooleanInput extends Component {
return (
<FormGroup className={className} {...sanitizeRestProps(rest)}>
<FormControlLabel
htmlFor={id}
control={
<Switch
id={id}
color="primary"
checked={!!value}
onChange={this.handleChange}
Expand All @@ -54,6 +57,7 @@ export class BooleanInput extends Component {

BooleanInput.propTypes = {
className: PropTypes.string,
id: PropTypes.string,
input: PropTypes.object,
isRequired: PropTypes.bool,
label: PropTypes.string,
Expand Down
11 changes: 10 additions & 1 deletion packages/ra-ui-materialui/src/input/CheckboxGroupInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class CheckboxGroupInput extends Component {

renderCheckbox = choice => {
const {
id,
input: { value },
optionText,
optionValue,
Expand All @@ -121,6 +122,7 @@ export class CheckboxGroupInput extends Component {
: get(choice, optionText);
return (
<FormControlLabel
htmlFor={`${id}_${get(choice, optionValue)}`}
key={get(choice, optionValue)}
checked={
value
Expand All @@ -130,7 +132,13 @@ export class CheckboxGroupInput extends Component {
}
onChange={this.handleCheck}
value={String(get(choice, optionValue))}
control={<Checkbox color="primary" {...options} />}
control={
<Checkbox
id={`${id}_${get(choice, optionValue)}`}
color="primary"
{...options}
/>
}
label={
translateChoice
? translate(choiceName, { _: choiceName })
Expand Down Expand Up @@ -192,6 +200,7 @@ CheckboxGroupInput.propTypes = {
label: PropTypes.string,
source: PropTypes.string,
options: PropTypes.object,
id: PropTypes.string,
input: PropTypes.shape({
onChange: PropTypes.func.isRequired,
}),
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-ui-materialui/src/input/FileInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class FileInput extends Component {
classes: PropTypes.object,
className: PropTypes.string,
disableClick: PropTypes.bool,
id: PropTypes.string,
input: PropTypes.object,
isRequired: PropTypes.bool,
label: PropTypes.string,
Expand Down Expand Up @@ -150,19 +151,21 @@ export class FileInput extends Component {
classes = {},
className,
disableClick,
id,
isRequired,
label,
maxSize,
minSize,
multiple,
resource,
source,
options,
options = {},
...rest
} = this.props;

return (
<Labeled
id={id}
label={label}
className={classnames(classes.root, className)}
source={source}
Expand All @@ -180,6 +183,7 @@ export class FileInput extends Component {
multiple={multiple}
className={classes.dropZone}
{...options}
inputProps={{ id, ...options.inputProps }}
>
{this.label()}
</Dropzone>
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-ui-materialui/src/input/Labeled.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const Labeled = ({
classes,
className,
fullWidth,
id,
input,
isRequired,
label,
Expand All @@ -68,7 +69,7 @@ export const Labeled = ({
fullWidth={fullWidth}
error={meta && meta.touched && meta.error}
>
<InputLabel shrink className={classes.label}>
<InputLabel htmlFor={id} shrink className={classes.label}>
<FieldTitle
label={label}
source={source}
Expand All @@ -95,6 +96,7 @@ Labeled.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
fullWidth: PropTypes.bool,
id: PropTypes.string,
input: PropTypes.object,
isRequired: PropTypes.bool,
label: PropTypes.string,
Expand Down
10 changes: 9 additions & 1 deletion packages/ra-ui-materialui/src/input/RadioButtonGroupInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class RadioButtonGroupInput extends Component {

renderRadioButton = choice => {
const {
id,
optionText,
optionValue,
translate,
Expand All @@ -95,9 +96,15 @@ export class RadioButtonGroupInput extends Component {
: get(choice, optionText);
return (
<FormControlLabel
htmlFor={`${id}_${get(choice, optionValue)}`}
key={get(choice, optionValue)}
value={get(choice, optionValue)}
control={<Radio color="primary" />}
control={
<Radio
id={`${id}_${get(choice, optionValue)}`}
color="primary"
/>
}
label={
translateChoice
? translate(choiceName, { _: choiceName })
Expand Down Expand Up @@ -171,6 +178,7 @@ RadioButtonGroupInput.propTypes = {
choices: PropTypes.arrayOf(PropTypes.object),
classes: PropTypes.object,
className: PropTypes.string,
id: PropTypes.string,
input: PropTypes.object,
isRequired: PropTypes.bool,
label: PropTypes.string,
Expand Down

0 comments on commit 6647d59

Please sign in to comment.