Skip to content

Commit 18aa629

Browse files
sakulstraoliviertassinari
authored andcommitted
[input] Forward required, readOnly and autoFocus (#12234)
* forward required, readonly and autofocus to switchBase * let's merge
1 parent e506d41 commit 18aa629

File tree

11 files changed

+51
-21
lines changed

11 files changed

+51
-21
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ module.exports = {
106106

107107
'prettier/prettier': 'error',
108108

109-
'jsx-a11y/label-has-for': 'off',
110109
'jsx-a11y/label-has-associated-control': 'error',
110+
'jsx-a11y/label-has-for': 'off',
111+
'jsx-a11y/no-autofocus': 'off', // We are a library, people do what they want.
111112
},
112113
};

packages/material-ui-lab/src/ToggleButton/ToggleButtonGroup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ ToggleButtonGroup.propTypes = {
111111
*/
112112
className: PropTypes.string,
113113
/**
114-
* If `true` only allow one of the child ToggleButton values to be selected.
114+
* If `true`, only allow one of the child ToggleButton values to be selected.
115115
*/
116116
exclusive: PropTypes.bool,
117117
/**
@@ -123,7 +123,7 @@ ToggleButtonGroup.propTypes = {
123123
*/
124124
onChange: PropTypes.func,
125125
/**
126-
* If `true` render the group in a selected state. If `auto` (the default)
126+
* If `true`, render the group in a selected state. If `auto` (the default)
127127
* render in a selected state if `value` is not empty.
128128
*/
129129
selected: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['auto'])]),

packages/material-ui/src/Input/Input.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface InputProps
2323
multiline?: boolean;
2424
name?: string;
2525
placeholder?: string;
26+
required?: boolean;
2627
rows?: string | number;
2728
rowsMax?: string | number;
2829
startAdornment?: React.ReactNode;

packages/material-ui/src/Input/Input.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,28 @@ function formControlState(props, context) {
207207
let disabled = props.disabled;
208208
let error = props.error;
209209
let margin = props.margin;
210+
let required = props.required;
210211

211212
if (context && context.muiFormControl) {
212213
if (typeof disabled === 'undefined') {
213214
disabled = context.muiFormControl.disabled;
214215
}
215-
216216
if (typeof error === 'undefined') {
217217
error = context.muiFormControl.error;
218218
}
219-
220219
if (typeof margin === 'undefined') {
221220
margin = context.muiFormControl.margin;
222221
}
222+
if (typeof required === 'undefined') {
223+
required = context.muiFormControl.required;
224+
}
223225
}
224226

225227
return {
226228
disabled,
227229
error,
228230
margin,
231+
required,
229232
};
230233
}
231234

@@ -422,7 +425,7 @@ class Input extends React.Component {
422425
} = this.props;
423426

424427
const { muiFormControl } = this.context;
425-
const { disabled, error, margin } = formControlState(this.props, this.context);
428+
const { disabled, error, margin, required } = formControlState(this.props, this.context);
426429

427430
const className = classNames(
428431
classes.root,
@@ -450,8 +453,6 @@ class Input extends React.Component {
450453
inputPropsClassName,
451454
);
452455

453-
const required = muiFormControl && muiFormControl.required === true;
454-
455456
let InputComponent = 'input';
456457
let inputProps = {
457458
...inputPropsProp,
@@ -500,7 +501,7 @@ class Input extends React.Component {
500501
onKeyUp={onKeyUp}
501502
placeholder={placeholder}
502503
readOnly={readOnly}
503-
required={required ? true : undefined}
504+
required={required}
504505
rows={rows}
505506
type={type}
506507
value={value}
@@ -624,9 +625,14 @@ Input.propTypes = {
624625
*/
625626
placeholder: PropTypes.string,
626627
/**
627-
* @ignore
628+
* It prevents the user from changing the value of the field
629+
* (not from interacting with the field).
628630
*/
629631
readOnly: PropTypes.bool,
632+
/**
633+
* If `true`, the input will be required.
634+
*/
635+
required: PropTypes.bool,
630636
/**
631637
* Number of rows to display when multiline option is set to true.
632638
*/

packages/material-ui/src/Modal/Modal.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable jsx-a11y/no-autofocus */
2-
31
import React from 'react';
42
import { assert } from 'chai';
53
import { spy, stub } from 'sinon';

packages/material-ui/src/TextField/TextField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ TextField.propTypes = {
232232
*/
233233
placeholder: PropTypes.string,
234234
/**
235-
* If `true`, the label is displayed as required.
235+
* If `true`, the label is displayed as required and the input will be required.
236236
*/
237237
required: PropTypes.bool,
238238
/**

packages/material-ui/src/internal/SwitchBase.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { IconButtonProps } from '../IconButton';
44

55
export interface SwitchBaseProps
66
extends StandardProps<IconButtonProps, SwitchBaseClassKey, 'onChange'> {
7+
autoFocus?: boolean;
78
checked?: boolean | string;
89
checkedIcon: React.ReactNode;
910
defaultChecked?: boolean;
@@ -16,6 +17,8 @@ export interface SwitchBaseProps
1617
inputRef?: React.Ref<any>;
1718
name?: string;
1819
onChange?: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;
20+
readOnly?: boolean;
21+
required?: boolean;
1922
tabIndex?: number;
2023
value?: string;
2124
}

packages/material-ui/src/internal/SwitchBase.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class SwitchBase extends React.Component {
8787

8888
render() {
8989
const {
90+
autoFocus,
9091
checked: checkedProp,
9192
checkedIcon,
9293
classes,
@@ -100,6 +101,8 @@ class SwitchBase extends React.Component {
100101
onBlur,
101102
onChange,
102103
onFocus,
104+
readOnly,
105+
required,
103106
tabIndex,
104107
type,
105108
value,
@@ -138,16 +141,19 @@ class SwitchBase extends React.Component {
138141
>
139142
{checked ? checkedIcon : icon}
140143
<input
141-
id={hasLabelFor && id}
142-
type={type}
143-
name={name}
144+
autoFocus={autoFocus}
144145
checked={checked}
145-
onChange={this.handleInputChange}
146146
className={classes.input}
147147
disabled={disabled}
148+
id={hasLabelFor && id}
149+
name={name}
150+
onChange={this.handleInputChange}
151+
readOnly={readOnly}
152+
ref={inputRef}
153+
required={required}
148154
tabIndex={tabIndex}
155+
type={type}
149156
value={value}
150-
ref={inputRef}
151157
{...inputProps}
152158
/>
153159
</IconButton>
@@ -158,6 +164,10 @@ class SwitchBase extends React.Component {
158164
// NB: If changed, please update Checkbox, Switch and Radio
159165
// so that the API documentation is updated.
160166
SwitchBase.propTypes = {
167+
/**
168+
* If `true`, the input will be focused during the first mount.
169+
*/
170+
autoFocus: PropTypes.bool,
161171
/**
162172
* If `true`, the component is checked.
163173
*/
@@ -231,6 +241,15 @@ SwitchBase.propTypes = {
231241
* @ignore
232242
*/
233243
onFocus: PropTypes.func,
244+
/**
245+
* It prevents the user from changing the value of the field
246+
* (not from interacting with the field).
247+
*/
248+
readOnly: PropTypes.bool,
249+
/**
250+
* If `true`, the input will be required.
251+
*/
252+
required: PropTypes.bool,
234253
/**
235254
* @ignore
236255
*/

pages/api/input.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ title: Input API
3434
| <span class="prop-name">name</span> | <span class="prop-type">string |   | Name attribute of the `input` element. |
3535
| <span class="prop-name">onChange</span> | <span class="prop-type">func |   | Callback fired when the value is changed.<br><br>**Signature:**<br>`function(event: object) => void`<br>*event:* The event source of the callback. You can pull out the new value by accessing `event.target.value`. |
3636
| <span class="prop-name">placeholder</span> | <span class="prop-type">string |   | The short hint displayed in the input before the user enters a value. |
37+
| <span class="prop-name">readOnly</span> | <span class="prop-type">bool |   | It prevents the user from changing the value of the field (not from interacting with the field). |
38+
| <span class="prop-name">required</span> | <span class="prop-type">bool |   | If `true`, the input will be required. |
3739
| <span class="prop-name">rows</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br> |   | Number of rows to display when multiline option is set to true. |
3840
| <span class="prop-name">rowsMax</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br> |   | Maximum number of rows to display when multiline option is set to true. |
3941
| <span class="prop-name">startAdornment</span> | <span class="prop-type">node |   | Start `InputAdornment` for this component. |

pages/api/text-field.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ For advanced cases, please look at the source of TextField by clicking on the
5959
| <span class="prop-name">name</span> | <span class="prop-type">string |   | Name attribute of the `input` element. |
6060
| <span class="prop-name">onChange</span> | <span class="prop-type">func |   | Callback fired when the value is changed.<br><br>**Signature:**<br>`function(event: object) => void`<br>*event:* The event source of the callback. You can pull out the new value by accessing `event.target.value`. |
6161
| <span class="prop-name">placeholder</span> | <span class="prop-type">string |   | The short hint displayed in the input before the user enters a value. |
62-
| <span class="prop-name">required</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the label is displayed as required. |
62+
| <span class="prop-name">required</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the label is displayed as required and the input will be required. |
6363
| <span class="prop-name">rows</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br> |   | Number of rows to display when multiline option is set to true. |
6464
| <span class="prop-name">rowsMax</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br> |   | Maximum number of rows to display when multiline option is set to true. |
6565
| <span class="prop-name">select</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | Render a `Select` element while passing the `Input` element to `Select` as `input` parameter. If this option is set you must pass the options of the select as children. |

0 commit comments

Comments
 (0)