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

[input] Forward required, readOnly and autoFocus #12234

Merged
merged 2 commits into from
Jul 22, 2018
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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ module.exports = {

'prettier/prettier': 'error',

'jsx-a11y/label-has-for': 'off',
'jsx-a11y/label-has-associated-control': 'error',
'jsx-a11y/label-has-for': 'off',
'jsx-a11y/no-autofocus': 'off', // We are a library, people do what they want.
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ToggleButtonGroup.propTypes = {
*/
className: PropTypes.string,
/**
* If `true` only allow one of the child ToggleButton values to be selected.
* If `true`, only allow one of the child ToggleButton values to be selected.
*/
exclusive: PropTypes.bool,
/**
Expand All @@ -123,7 +123,7 @@ ToggleButtonGroup.propTypes = {
*/
onChange: PropTypes.func,
/**
* If `true` render the group in a selected state. If `auto` (the default)
* If `true`, render the group in a selected state. If `auto` (the default)
* render in a selected state if `value` is not empty.
*/
selected: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['auto'])]),
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/Input/Input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface InputProps
multiline?: boolean;
name?: string;
placeholder?: string;
required?: boolean;
rows?: string | number;
rowsMax?: string | number;
startAdornment?: React.ReactNode;
Expand Down
20 changes: 13 additions & 7 deletions packages/material-ui/src/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,28 @@ function formControlState(props, context) {
let disabled = props.disabled;
let error = props.error;
let margin = props.margin;
let required = props.required;

if (context && context.muiFormControl) {
if (typeof disabled === 'undefined') {
disabled = context.muiFormControl.disabled;
}

if (typeof error === 'undefined') {
error = context.muiFormControl.error;
}

if (typeof margin === 'undefined') {
margin = context.muiFormControl.margin;
}
if (typeof required === 'undefined') {
required = context.muiFormControl.required;
}
}

return {
disabled,
error,
margin,
required,
};
}

Expand Down Expand Up @@ -422,7 +425,7 @@ class Input extends React.Component {
} = this.props;

const { muiFormControl } = this.context;
const { disabled, error, margin } = formControlState(this.props, this.context);
const { disabled, error, margin, required } = formControlState(this.props, this.context);

const className = classNames(
classes.root,
Expand Down Expand Up @@ -450,8 +453,6 @@ class Input extends React.Component {
inputPropsClassName,
);

const required = muiFormControl && muiFormControl.required === true;

let InputComponent = 'input';
let inputProps = {
...inputPropsProp,
Expand Down Expand Up @@ -500,7 +501,7 @@ class Input extends React.Component {
onKeyUp={onKeyUp}
placeholder={placeholder}
readOnly={readOnly}
required={required ? true : undefined}
required={required}
rows={rows}
type={type}
value={value}
Expand Down Expand Up @@ -624,9 +625,14 @@ Input.propTypes = {
*/
placeholder: PropTypes.string,
/**
* @ignore
* It prevents the user from changing the value of the field
* (not from interacting with the field).
*/
readOnly: PropTypes.bool,
/**
* If `true`, the input will be required.
*/
required: PropTypes.bool,
/**
* Number of rows to display when multiline option is set to true.
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/material-ui/src/Modal/Modal.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable jsx-a11y/no-autofocus */

import React from 'react';
import { assert } from 'chai';
import { spy, stub } from 'sinon';
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ TextField.propTypes = {
*/
placeholder: PropTypes.string,
/**
* If `true`, the label is displayed as required.
* If `true`, the label is displayed as required and the input will be required.
*/
required: PropTypes.bool,
/**
Expand Down
3 changes: 3 additions & 0 deletions packages/material-ui/src/internal/SwitchBase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IconButtonProps } from '../IconButton';

export interface SwitchBaseProps
extends StandardProps<IconButtonProps, SwitchBaseClassKey, 'onChange'> {
autoFocus?: boolean;
checked?: boolean | string;
checkedIcon: React.ReactNode;
defaultChecked?: boolean;
Expand All @@ -16,6 +17,8 @@ export interface SwitchBaseProps
inputRef?: React.Ref<any>;
name?: string;
onChange?: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;
readOnly?: boolean;
required?: boolean;
tabIndex?: number;
value?: string;
}
Expand Down
29 changes: 24 additions & 5 deletions packages/material-ui/src/internal/SwitchBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SwitchBase extends React.Component {

render() {
const {
autoFocus,
checked: checkedProp,
checkedIcon,
classes,
Expand All @@ -100,6 +101,8 @@ class SwitchBase extends React.Component {
onBlur,
onChange,
onFocus,
readOnly,
required,
tabIndex,
type,
value,
Expand Down Expand Up @@ -138,16 +141,19 @@ class SwitchBase extends React.Component {
>
{checked ? checkedIcon : icon}
<input
id={hasLabelFor && id}
type={type}
name={name}
autoFocus={autoFocus}
checked={checked}
onChange={this.handleInputChange}
className={classes.input}
disabled={disabled}
id={hasLabelFor && id}
name={name}
onChange={this.handleInputChange}
readOnly={readOnly}
ref={inputRef}
required={required}
tabIndex={tabIndex}
type={type}
value={value}
ref={inputRef}
{...inputProps}
/>
</IconButton>
Expand All @@ -158,6 +164,10 @@ class SwitchBase extends React.Component {
// NB: If changed, please update Checkbox, Switch and Radio
// so that the API documentation is updated.
SwitchBase.propTypes = {
/**
* If `true`, the input will be focused during the first mount.
*/
autoFocus: PropTypes.bool,
/**
* If `true`, the component is checked.
*/
Expand Down Expand Up @@ -231,6 +241,15 @@ SwitchBase.propTypes = {
* @ignore
*/
onFocus: PropTypes.func,
/**
* It prevents the user from changing the value of the field
* (not from interacting with the field).
*/
readOnly: PropTypes.bool,
/**
* If `true`, the input will be required.
*/
required: PropTypes.bool,
/**
* @ignore
*/
Expand Down
2 changes: 2 additions & 0 deletions pages/api/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ title: Input API
| <span class="prop-name">name</span> | <span class="prop-type">string |   | Name attribute of the `input` element. |
| <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`. |
| <span class="prop-name">placeholder</span> | <span class="prop-type">string |   | The short hint displayed in the input before the user enters a value. |
| <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). |
| <span class="prop-name">required</span> | <span class="prop-type">bool |   | If `true`, the input will be required. |
| <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. |
| <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. |
| <span class="prop-name">startAdornment</span> | <span class="prop-type">node |   | Start `InputAdornment` for this component. |
Expand Down
2 changes: 1 addition & 1 deletion pages/api/text-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ For advanced cases, please look at the source of TextField by clicking on the
| <span class="prop-name">name</span> | <span class="prop-type">string |   | Name attribute of the `input` element. |
| <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`. |
| <span class="prop-name">placeholder</span> | <span class="prop-type">string |   | The short hint displayed in the input before the user enters a value. |
| <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. |
| <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. |
| <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. |
| <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. |
| <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. |
Expand Down
4 changes: 2 additions & 2 deletions pages/lab/api/toggle-button-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ title: ToggleButtonGroup API
|:-----|:-----|:--------|:------------|
| <span class="prop-name required">children *</span> | <span class="prop-type">node |   | The content of the button. |
| <span class="prop-name">classes</span> | <span class="prop-type">object |   | Useful to extend the style applied to components. |
| <span class="prop-name">exclusive</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true` only allow one of the child ToggleButton values to be selected. |
| <span class="prop-name">exclusive</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, only allow one of the child ToggleButton values to be selected. |
| <span class="prop-name">onChange</span> | <span class="prop-type">func |   | Callback fired when the value changes.<br><br>**Signature:**<br>`function(event: object, value: object) => void`<br>*event:* The event source of the callback<br>*value:* of the selected buttons. When `exclusive` is true this is a single value; when false an array of selected values. |
| <span class="prop-name">selected</span> | <span class="prop-type">union:&nbsp;bool&nbsp;&#124;<br>&nbsp;enum:&nbsp;'auto'<br><br> | <span class="prop-default">'auto'</span> | If `true` render the group in a selected state. If `auto` (the default) render in a selected state if `value` is not empty. |
| <span class="prop-name">selected</span> | <span class="prop-type">union:&nbsp;bool&nbsp;&#124;<br>&nbsp;enum:&nbsp;'auto'<br><br> | <span class="prop-default">'auto'</span> | If `true`, render the group in a selected state. If `auto` (the default) render in a selected state if `value` is not empty. |
| <span class="prop-name">value</span> | <span class="prop-type">any | <span class="prop-default">null</span> | The currently selected value within the group or an array of selected values when `exclusive` is false. |

Any other properties supplied will be spread to the root element (native element).
Expand Down