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] Add SelectDisplayProps prop #10408

Merged
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
1 change: 1 addition & 0 deletions pages/api/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ filename: /src/Select/Select.js
| <span class="prop-name">onOpen</span> | <span class="prop-type">func | | Callback fired when the component requests to be opened. Useful in controlled mode (see open).<br><br>**Signature:**<br>`function(event: object) => void`<br>*event:* The event source of the callback |
| <span class="prop-name">open</span> | <span class="prop-type">bool | | Control `select` open state. You can only use it when the `native` property is `false` (default). |
| <span class="prop-name">renderValue</span> | <span class="prop-type">func | | Render the selected value. You can only use it when the `native` property is `false` (default). |
| <span class="prop-name">SelectDisplayProps</span> | <span class="prop-type">object | | Properties applied to the clickable div element. |
| <span class="prop-name">value</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;number&nbsp;&#124;<br>&nbsp;arrayOf<br> | | The input value, required for a controlled component. |

Any other properties supplied will be [spread to the root element](/guides/api#spread).
Expand Down
1 change: 1 addition & 0 deletions src/Select/Select.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SelectProps extends StandardProps<InputProps, SelectClassKey, '
onOpen?: (event: React.ChangeEvent<{}>) => void;
open?: boolean;
renderValue?: (value: SelectProps['value']) => React.ReactNode;
SelectDisplayProps?: React.HTMLAttributes<HTMLDivElement>;
value?: Array<string | number> | string | number;
}

Expand Down
12 changes: 9 additions & 3 deletions src/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,15 @@ function Select(props) {
onOpen,
open,
renderValue,
SelectDisplayProps,
...other
} = props;

return React.cloneElement(input, {
// Most of the logic is implemented in `SelectInput`.
// The `Select` component is a simple API wrapper to expose something better to play with.
inputComponent: SelectInput,
...other,
inputProps: {
...inputProps,
...(input ? input.props.inputProps : {}),
autoWidth,
children,
classes,
Expand All @@ -95,7 +93,11 @@ function Select(props) {
onOpen,
open,
renderValue,
SelectDisplayProps,
...inputProps,
...(input ? input.props.inputProps : {}),
},
...other,
});
}

Expand Down Expand Up @@ -172,6 +174,10 @@ Select.propTypes = {
* You can only use it when the `native` property is `false` (default).
*/
renderValue: PropTypes.func,
/**
* Properties applied to the clickable div element.
*/
SelectDisplayProps: PropTypes.object,
/**
* The input value, required for a controlled component.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Select/SelectInput.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface SelectInputProps extends StandardProps<{}, SelectInputClassKey>
open?: boolean;
readOnly?: boolean;
renderValue?: (value: SelectInputProps['value']) => React.ReactNode;
SelectDisplayProps?: React.HTMLAttributes<HTMLDivElement>;
value?: string | number | Array<string | number>;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class SelectInput extends React.Component {
open: openProp,
readOnly,
renderValue,
SelectDisplayProps,
value,
...other
} = this.props;
Expand Down Expand Up @@ -284,6 +285,7 @@ class SelectInput extends React.Component {
onBlur={this.handleBlur}
onClick={disabled || readOnly ? null : this.handleClick}
onFocus={onFocus}
{...SelectDisplayProps}
>
{/* So the vertical align positioning algorithm quicks in. */}
{/* eslint-disable-next-line react/no-danger */}
Expand Down Expand Up @@ -416,6 +418,10 @@ SelectInput.propTypes = {
* You can only use it when the `native` property is `false` (default).
*/
renderValue: PropTypes.func,
/**
* Properties applied to the clickable div element.
*/
SelectDisplayProps: PropTypes.object,
/**
* The value of the component, required for a controlled component.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Select/SelectInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ describe('<SelectInput />', () => {
});
});

describe('prop: SelectDisplayProps', () => {
it('should apply additional properties to the clickable div element', () => {
const wrapper = shallow(
<SelectInput {...defaultProps} SelectDisplayProps={{ 'data-test': 'SelectDisplay' }} />,
);

const selectDisplay = wrapper.find('[data-mui-test="SelectDisplay"]');
assert.strictEqual(selectDisplay.props()['data-test'], 'SelectDisplay');
});
});

describe('prop: displayEmpty', () => {
it('should display the selected item even if its value is empty', () => {
const wrapper = shallow(
Expand Down