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

[Select] Support object value #13661

Merged
merged 2 commits into from
Nov 23, 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
15 changes: 13 additions & 2 deletions packages/material-ui/src/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ Input.propTypes = {
/**
* The default input value, useful when not controlling the component.
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
defaultValue: PropTypes.oneOfType([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will voice my concerns once again because this is increasingly magic since the value will be cast to string. People should do that explicitly so that the component appears to be less magic. Allowing numbers is already bad as it is since numbers will go in but strings will come out.

Especially for something like objects it doesn't make much sense to allow them since most of them will generate the same value ([object Object]).

Copy link
Member

@oliviertassinari oliviertassinari Nov 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan either, but many people ask for it. At this point, we might just use any for the propType, like React is doing for <input>. Now, most people use an XHR to send the form values to the server. They don't care about the [object Object] DOM casting. Also, as we don't cast objet to string in the non native select implementation, this works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if you access event.target.value then you should care about casting. I was hoping we could add a more predictable wrapper around input since type coercion is imo one of the bigger issues in JS since it can't be addressed with linters.

But since we already allowed this with numbers we might as well be consistent. PropTypes.any it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having object select support definitely comes with a cost :/. Does it worth it?

PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.object,
PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool, PropTypes.object]),
),
]),
/**
* If `true`, the input will be disabled.
*/
Expand Down Expand Up @@ -228,7 +236,10 @@ Input.propTypes = {
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])),
PropTypes.object,
PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool, PropTypes.object]),
),
]),
};

Expand Down
4 changes: 2 additions & 2 deletions packages/material-ui/src/InputBase/InputBase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface InputBaseProps
> {
autoComplete?: string;
autoFocus?: boolean;
defaultValue?: string | number;
defaultValue?: Array<string | number | boolean | object> | string | number | boolean | object;
disabled?: boolean;
disableUnderline?: boolean;
endAdornment?: React.ReactNode;
Expand Down Expand Up @@ -40,7 +40,7 @@ export interface InputBaseProps
rowsMax?: string | number;
startAdornment?: React.ReactNode;
type?: string;
value?: Array<string | number | boolean> | string | number | boolean;
value?: Array<string | number | boolean | object> | string | number | boolean | object;
onFilled?: () => void;
/**
* `onChange`, `onKeyUp` + `onKeyDown` are applied to the inner `InputComponent`,
Expand Down
15 changes: 13 additions & 2 deletions packages/material-ui/src/InputBase/InputBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,15 @@ InputBase.propTypes = {
/**
* The default input value, useful when not controlling the component.
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
defaultValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.object,
PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool, PropTypes.object]),
),
]),
/**
* If `true`, the input will be disabled.
*/
Expand Down Expand Up @@ -567,7 +575,10 @@ InputBase.propTypes = {
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])),
PropTypes.object,
PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool, PropTypes.object]),
),
]),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/Select/Select.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface SelectProps
open?: boolean;
renderValue?: (value: SelectProps['value']) => React.ReactNode;
SelectDisplayProps?: React.HTMLAttributes<HTMLDivElement>;
value?: Array<string | number | boolean> | string | number | boolean;
value?: Array<string | number | boolean | object> | string | number | boolean | object;
variant?: 'standard' | 'outlined' | 'filled';
}

Expand Down
17 changes: 17 additions & 0 deletions packages/material-ui/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,21 @@ describe('<Select />', () => {
assert.strictEqual(React.isValidElement(selected), true);
});
});

describe('prop: value', () => {
it('should be able to use an object', () => {
const value = {};
const wrapper = mount(
<Select {...defaultProps} value={value}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={value}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>,
);
assert.strictEqual(wrapper.find(`.${classes.select}`).text(), 'Twenty');
});
});
});
4 changes: 2 additions & 2 deletions pages/api/input-base.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It contains a load of style reset and some state logic.
| <span class="prop-name">autoFocus</span> | <span class="prop-type">bool</span> |   | If `true`, the input will be focused during the first mount. |
| <span class="prop-name">classes</span> | <span class="prop-type">object</span> |   | Override or extend the styles applied to the component. See [CSS API](#css-api) below for more details. |
| <span class="prop-name">className</span> | <span class="prop-type">string</span> |   | The CSS class name of the wrapper element. |
| <span class="prop-name">defaultValue</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br></span> |   | The default input value, useful when not controlling the component. |
| <span class="prop-name">defaultValue</span> | <span class="prop-type">union:&nbsp;string, number, bool, object, arrayOf<br></span> |   | The default input value, useful when not controlling the component. |
| <span class="prop-name">disabled</span> | <span class="prop-type">bool</span> |   | If `true`, the input will be disabled. |
| <span class="prop-name">endAdornment</span> | <span class="prop-type">node</span> |   | End `InputAdornment` for this component. |
| <span class="prop-name">error</span> | <span class="prop-type">bool</span> |   | If `true`, the input will indicate an error. This is normally obtained via context from FormControl. |
Expand All @@ -44,7 +44,7 @@ It contains a load of style reset and some state logic.
| <span class="prop-name">rowsMax</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br></span> |   | Maximum number of rows to display when multiline option is set to true. |
| <span class="prop-name">startAdornment</span> | <span class="prop-type">node</span> |   | Start `InputAdornment` for this component. |
| <span class="prop-name">type</span> | <span class="prop-type">string</span> | <span class="prop-default">'text'</span> | Type of the input element. It should be a valid HTML5 input type. |
| <span class="prop-name">value</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number&nbsp;&#124;<br>&nbsp;bool&nbsp;&#124;<br>&nbsp;arrayOf<br></span> |   | The input value, required for a controlled component. |
| <span class="prop-name">value</span> | <span class="prop-type">union:&nbsp;string, number, bool, object, arrayOf<br></span> |   | The input value, required for a controlled component. |

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

Expand Down
4 changes: 2 additions & 2 deletions pages/api/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Input from '@material-ui/core/Input';
| <span class="prop-name">autoFocus</span> | <span class="prop-type">bool</span> |   | If `true`, the input will be focused during the first mount. |
| <span class="prop-name">classes</span> | <span class="prop-type">object</span> |   | Override or extend the styles applied to the component. See [CSS API](#css-api) below for more details. |
| <span class="prop-name">className</span> | <span class="prop-type">string</span> |   | The CSS class name of the wrapper element. |
| <span class="prop-name">defaultValue</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br></span> |   | The default input value, useful when not controlling the component. |
| <span class="prop-name">defaultValue</span> | <span class="prop-type">union:&nbsp;string, number, bool, object, arrayOf<br></span> |   | The default input value, useful when not controlling the component. |
| <span class="prop-name">disabled</span> | <span class="prop-type">bool</span> |   | If `true`, the input will be disabled. |
| <span class="prop-name">disableUnderline</span> | <span class="prop-type">bool</span> |   | If `true`, the input will not have an underline. |
| <span class="prop-name">endAdornment</span> | <span class="prop-type">node</span> |   | End `InputAdornment` for this component. |
Expand All @@ -43,7 +43,7 @@ import Input from '@material-ui/core/Input';
| <span class="prop-name">rowsMax</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number<br></span> |   | Maximum number of rows to display when multiline option is set to true. |
| <span class="prop-name">startAdornment</span> | <span class="prop-type">node</span> |   | Start `InputAdornment` for this component. |
| <span class="prop-name">type</span> | <span class="prop-type">string</span> |   | Type of the input element. It should be a valid HTML5 input type. |
| <span class="prop-name">value</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number&nbsp;&#124;<br>&nbsp;bool&nbsp;&#124;<br>&nbsp;arrayOf<br></span> |   | The input value, required for a controlled component. |
| <span class="prop-name">value</span> | <span class="prop-type">union:&nbsp;string, number, bool, object, arrayOf<br></span> |   | The input value, required for a controlled component. |

Any other properties supplied will be spread to the root element ([InputBase](/api/input-base/)).

Expand Down