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

[DropDownMenu] [SelectField] Add defaultText prop shown when there's no item with the corresponding value #4902

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions src/DropDownMenu/DropDownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class DropDownMenu extends Component {
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* Default text to be shown when there's no item with the corresponding value.
*/
defaultText: PropTypes.string,
/**
* Disables the menu.
*/
Expand Down Expand Up @@ -151,6 +155,7 @@ class DropDownMenu extends Component {
static defaultProps = {
animated: true,
autoWidth: true,
defaultText: '',
disabled: false,
openImmediately: false,
maxHeight: 500,
Expand Down Expand Up @@ -247,6 +252,7 @@ class DropDownMenu extends Component {
autoWidth,
children,
className,
defaultText,
iconStyle,
labelStyle,
listStyle,
Expand All @@ -267,9 +273,9 @@ class DropDownMenu extends Component {
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);

let displayValue;
let displayValue = defaultText;
React.Children.forEach(children, (child) => {
if (!displayValue || value === child.props.value) {
if (value === child.props.value) {
// This will need to be improved (in case primaryText is a node)
displayValue = child.props.label || child.props.primaryText;
}
Expand Down
6 changes: 3 additions & 3 deletions src/DropDownMenu/DropDownMenu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ describe('<DropDownMenu />', () => {
assert.strictEqual(wrapper.childAt(0).childAt(0).childAt(0).node, 'Every Night');
});

it('displays the text field of the first menuItems prop when value prop isn\'t found', () => {
it('displays the text field of the defaultText prop when value prop isn\'t found', () => {
const wrapper = shallowWithContext(
<DropDownMenu value={4}>
<DropDownMenu value={4} defaultText="Default value">
<div value={1} primaryText="Never" />
<div value={2} primaryText="Every Night" />
<div value={3} primaryText="Weeknights" />
</DropDownMenu>
);

assert.strictEqual(wrapper.childAt(0).childAt(0).childAt(0).node, 'Never');
assert.strictEqual(wrapper.childAt(0).childAt(0).childAt(0).node, 'Default value');
});
});
7 changes: 7 additions & 0 deletions src/SelectField/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class SelectField extends Component {
* represent the selected menu item in the rendered select field.
*/
children: PropTypes.node,
/**
* Default text to be shown when there's no item with the corresponding value.
*/
defaultText: PropTypes.string,
/**
* If true, the select field will be disabled.
*/
Expand Down Expand Up @@ -157,6 +161,7 @@ class SelectField extends Component {
underlineStyle,
errorStyle,
selectFieldRoot,
defaultText,
disabled,
floatingLabelFixed,
floatingLabelText,
Expand All @@ -180,6 +185,7 @@ class SelectField extends Component {
<TextField
{...other}
style={style}
defaultText={hintText || floatingLabelText ? '' : defaultText}
Copy link
Member

@oliviertassinari oliviertassinari Aug 4, 2016

Choose a reason for hiding this comment

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

What's the use case for this property?

Copy link
Author

Choose a reason for hiding this comment

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

Well, for all the cases I have, there's a default action when nothing is selected, but I don't want to keep the select box empty. A simple solution would be to specify the text from the first option here as well, as other people still want this select empty for their cases.

Copy link
Member

Choose a reason for hiding this comment

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

I'm asking because defaultText doesn't seems to have any impact on the TextField component. And React is throwing a warning about leaking property.

Copy link
Author

Choose a reason for hiding this comment

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

Snap, you're right. It was intended to be passed for DropDownMenu.

Does this parameter still make sense or we'll use the first option's value?

Copy link
Member

@oliviertassinari oliviertassinari Aug 5, 2016

Choose a reason for hiding this comment

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

we'll use the first option's value?

I think that it would be better without this property. It isn't going to help migrating from a <select /> to a <SelectField />, nor the other way around, right?

disabled={disabled}
floatingLabelFixed={floatingLabelFixed}
floatingLabelText={floatingLabelText}
Expand All @@ -197,6 +203,7 @@ class SelectField extends Component {
underlineFocusStyle={underlineFocusStyle}
>
<DropDownMenu
defaultText={defaultText}
disabled={disabled}
style={Object.assign(styles.dropDownMenu, selectFieldRoot, menuStyle)}
labelStyle={Object.assign(styles.label, labelStyle)}
Expand Down