-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Support multiple dropdown type #254
Changes from all commits
e8bdd84
be277ef
a107fa7
58d93e7
8adc585
b236294
2b00efc
f4f064b
20352c6
2dd7e80
a3993db
dd3b171
ed58000
868756c
1ff2d3d
511a51d
d01a72c
d88ddfa
6346739
2ad2c81
4cd81a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/* eslint-disable valid-jsdoc, complexity */ | ||
import cx from 'classnames' | ||
import React, { createElement, PropTypes } from 'react' | ||
import React, { PropTypes } from 'react' | ||
import createFragment from 'react-addons-create-fragment' | ||
|
||
import META from '../../utils/Meta' | ||
import * as sui from '../../utils/semanticUtils' | ||
import { someChildType } from '../../utils/childrenUtils' | ||
import { | ||
getUnhandledProps, | ||
iconPropRenderer, | ||
imagePropRenderer, | ||
useKeyOnly, | ||
|
@@ -21,16 +21,13 @@ import Image from '../Image/Image' | |
*/ | ||
function Label(props) { | ||
const { | ||
attached, children, color, corner, className, circular, detail, detailLink, floating, horizontal, icon, | ||
image, link, onClick, onClickDetail, onClickRemove, pointing, removable, ribbon, size, tag, text, | ||
...rest, | ||
attached, children, color, corner, className, circular, detail, detailLink, floating, horizontal, | ||
icon, image, link, onClick, onDetailClick, onRemove, pointing, removable, ribbon, size, tag, text, | ||
} = props | ||
|
||
const handleClick = e => onClick && onClick(e, props) | ||
const handleClickRemove = e => onClickRemove && onClickRemove(e, props) | ||
const handleClickDetail = e => onClickDetail && onClickDetail(e, props) | ||
|
||
const _component = image || link || onClick ? 'a' : 'div' | ||
const handleRemove = e => onRemove && onRemove(e, props) | ||
const handleDetailClick = e => onDetailClick && onDetailClick(e, props) | ||
|
||
const classes = cx('sd-label ui', | ||
size, | ||
|
@@ -49,27 +46,29 @@ function Label(props) { | |
className | ||
) | ||
|
||
const _props = { | ||
className: classes, | ||
onClick: handleClick, | ||
...rest, | ||
} | ||
|
||
const _detail = detail || detailLink | ||
const detailComponent = (detailLink || onClickDetail) && 'a' || detail && 'div' | ||
const DetailComponent = (detailLink || onDetailClick) && 'a' || 'div' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to wrap the latter check in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not seeing where the extra parens can be added here. Here is another way of writing this: let DetailComponent = 'div'
if (detailLink || onDetailClick) DetailComponent = 'a' |
||
|
||
const _children = createFragment({ | ||
icon: iconPropRenderer(icon), | ||
image: imagePropRenderer(image), | ||
text, | ||
children, | ||
detail: _detail && createElement(detailComponent, { className: 'detail', onClick: handleClickDetail }, _detail), | ||
remove: (removable || onClickRemove) && <Icon className='delete' onClick={handleClickRemove} />, | ||
detail: detail && ( | ||
<DetailComponent className='detail' onClick={handleDetailClick}>{detail}</DetailComponent> | ||
), | ||
remove: (removable || onRemove) && ( | ||
<Icon className='delete' onClick={handleRemove} /> | ||
), | ||
}) | ||
|
||
// Do not destructure createElement import | ||
// react-docgen only recognizes a stateless component when React.createElement is returned | ||
return React.createElement(_component, _props, _children) | ||
const LabelComponent = image || link || onClick ? 'a' : 'div' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just stating that this is not immediately clear to me, or at least leaves me questioning if it works exactly as I expect. Take that as you will.
|
||
const rest = getUnhandledProps(Label, props) | ||
|
||
return ( | ||
<LabelComponent className={classes} onClick={handleClick} {...rest}> | ||
{_children} | ||
</LabelComponent> | ||
) | ||
} | ||
|
||
Label._meta = { | ||
|
@@ -106,7 +105,7 @@ Label.propTypes = { | |
/** Additional text with less emphasis. */ | ||
detail: PropTypes.string, | ||
|
||
/** Same as detail but formatted as a link. */ | ||
/** Format the detail as a link. */ | ||
detailLink: PropTypes.string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this behavior to match the |
||
|
||
/** Format a label to align better alongside text. */ | ||
|
@@ -136,13 +135,13 @@ Label.propTypes = { | |
/** Adds the link style when present, called with (event, props). */ | ||
onClick: PropTypes.func, | ||
|
||
/** Click callback for detail, called with (event, props). */ | ||
onClickDetail: PropTypes.func, | ||
/** Click callback for detail, called with (event, props). Formats the detail as a link. */ | ||
onDetailClick: PropTypes.func, | ||
|
||
/** Adds an "x" icon, called with (event, props) when "x" is clicked. */ | ||
onClickRemove: PropTypes.func, | ||
onRemove: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated callbacks and click handlers to make more sense and be consistent with our others ( |
||
|
||
/** Add an "x" icon that calls onClickRemove when clicked. */ | ||
/** Add an "x" icon that calls onRemove when clicked. */ | ||
removable: PropTypes.bool, | ||
|
||
/** Point to content next to it. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this example allows toggling the
multiple
prop, we need to convert the value to and from an array corresponding to the dropdown type.