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

[Chip] Add clickable property #11613

Merged
merged 11 commits into from
May 31, 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
7 changes: 7 additions & 0 deletions docs/src/pages/demos/chips/Chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function Chips(props) {
className={classes.chip}
deleteIcon={<DoneIcon />}
/>
<Chip
label="Clickable Link Chip"
className={classes.chip}
component="a"
href="#chip"
clickable
/>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/Chip/Chip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StandardProps } from '..';
export interface ChipProps
extends StandardProps<React.HTMLAttributes<HTMLDivElement>, ChipClassKey> {
avatar?: React.ReactElement<any>;
clickable?: boolean;
component?: React.ReactType<ChipProps>;
deleteIcon?: React.ReactElement<any>;
label?: React.ReactNode;
Expand Down
13 changes: 11 additions & 2 deletions packages/material-ui/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const styles = theme => {
cursor: 'default',
// We disable the focus ring for mouse, touch and keyboard users.
outline: 'none',
textDecoration: 'none',
border: 'none', // Remove `button` border
padding: 0, // Remove `button` padding
},
Expand Down Expand Up @@ -131,6 +132,7 @@ class Chip extends React.Component {
avatar: avatarProp,
classes,
className: classNameProp,
clickable,
component: Component,
deleteIcon: deleteIconProp,
label,
Expand All @@ -143,7 +145,7 @@ class Chip extends React.Component {

const className = classNames(
classes.root,
{ [classes.clickable]: onClick },
{ [classes.clickable]: onClick || clickable },
{ [classes.deletable]: onDelete },
classNameProp,
);
Expand Down Expand Up @@ -172,7 +174,7 @@ class Chip extends React.Component {
let tabIndex = tabIndexProp;

if (!tabIndex) {
tabIndex = onClick || onDelete ? 0 : -1;
tabIndex = onClick || onDelete || clickable ? 0 : -1;
}

return (
Expand Down Expand Up @@ -209,6 +211,12 @@ Chip.propTypes = {
* @ignore
*/
className: PropTypes.string,
/**
* If true, the chip will appear clickable, and will raise when pressed,
* even if the onClick property is not defined. This can be used, for example,
* along with the component property to indicate an anchor Chip is clickable.
*/
clickable: PropTypes.bool,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
Expand Down Expand Up @@ -242,6 +250,7 @@ Chip.propTypes = {
};

Chip.defaultProps = {
clickable: false,
Copy link
Member

@oliviertassinari oliviertassinari May 29, 2018

Choose a reason for hiding this comment

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

Should the clickable property have a higher priority than the onClick detection logic?
Meaning, what's the output of <Chip clickable={false} onClick={() => ()} />?

Copy link
Contributor Author

@vilvaathibanpb vilvaathibanpb May 29, 2018

Choose a reason for hiding this comment

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

@oliviertassinari I feel clickable should have low priority than OnClick. The output of above will still be clickable as per the below code: { [classes.clickable]: onClick || clickable }, if onclick is present, it will be clickable at any cost. Only when onClick is not present, clickable comes into the picture.

Copy link
Member

Choose a reason for hiding this comment

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

@oliviertassinari This is only affecting the styling (and the description should perhaps reflect that), but requiring the prop to be set in order for a Chip with onClick to appear clickable seems counter-intuitive to me.

Copy link
Member

Choose a reason for hiding this comment

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

but requiring the prop to be set in order for a Chip with onClick to appear clickable seems counter-intuitive to me.

@mbrookes I was asking the question in a context where defaultProps.clickable = null.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. So being able to set clickable={somethingFalse} when a Chip has an onClick() handler but other conditions mean that it won't act on the click?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oliviertassinari I feel defaultProps.clickable = null also would work fine. still false makes better sense, as we have kept props type as boolean. Also, in terms of functionality, nothing changes in either way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mbrookes Couldn't understand your question clearly. But to explain, if we have 'onClick' hander, 'clickable' doesn't affect and the chip is always clickable. when 'onClick' is not present, then the value of 'clickable' determine if the chip is clickable

Copy link
Member

@mbrookes mbrookes May 29, 2018

Choose a reason for hiding this comment

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

@vilvaathibanpb I was just clarifying with @oliviertassinari if I understood correctly the benefit of defaultProps.clickable = null combined with clickable having priority.

Let me rephrase it as a direct question: @oliviertassinari What do you see as the main benefit for null as the default?

Copy link
Member

Choose a reason for hiding this comment

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

What do you see as the main benefit for null as the default?

The advantage is that it's allowing people to control the clickable state, but still, being able to rely on the onClick detection logic if they wish. But I'm fine either way. The current approach is simple too.

component: 'div',
};

Expand Down
1 change: 1 addition & 0 deletions pages/api/chip.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Chips represent complex entities in small blocks, such as a contact.
|:-----|:-----|:--------|:------------|
| <span class="prop-name">avatar</span> | <span class="prop-type">element | | Avatar element. |
| <span class="prop-name">classes</span> | <span class="prop-type">object | | Override or extend the styles applied to the component. See [CSS API](#css-api) below for more details. |
| <span class="prop-name">clickable</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If true, the chip will appear clickable, and will raise when pressed, even if the onClick property is not defined. This can be used, for example, along with the component property to indicate an anchor Chip is clickable. |
| <span class="prop-name">component</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;func<br> | <span class="prop-default">'div'</span> | The component used for the root node. Either a string to use a DOM element or a component. |
| <span class="prop-name">deleteIcon</span> | <span class="prop-type">element | | Override the default delete icon element. Shown only if `onDelete` is set. |
| <span class="prop-name">label</span> | <span class="prop-type">node | | The content of the label. |
Expand Down