1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | 248x 173x 173x 173x 31x 142x 142x 247x 247x 247x 247x 247x 183x 183x 247x 247x 247x 461x 346x 346x 346x 346x 890x 890x 776x 346x 23x 23x 23x 23x 23x 23x 21x 21x 21x 21x 21x 21x 431x 431x 431x 431x 6x 1x 431x 638x 431x 89x 89x 89x 89x | import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import { omit, uniqueId } from 'lodash'; /** * @category containers * @component list-item * @variations collab-ui-react */ class ListItem extends React.Component { state = { id: this.props.id || uniqueId('cui-list-item-'), }; componentWillMount() { if (!this.props.children) return; const checkAllChildren = this.getChildrenElements(['ListItemSection', 'EventOverlay']); const checkSectionChildren = this.getChildrenElements(['ListItemSection']); if (!checkAllChildren) { return; } else Iif (!checkAllChildren.isCorrectUsage) { throw new Error( `If ListItemSection Component is used all children must use ListItemSection Components.` ); } else Iif (checkSectionChildren.length > 3) { throw new Error( `Only 3 ListItemSection components can be used as children. You've used ${ checkSectionChildren.length }` ); } } componentDidMount() { const anchorCount = this.checkElements('A'); const { focus, refName, focusOnLoad } = this.props; Iif (anchorCount.count > 1) { throw new Error( 'Only 1 primary child anchor tag may be used with ListItem component' ); } else Iif (anchorCount.count === 1 && anchorCount.children > 1) { throw new Error('Anchor tag can not have sibling'); } focusOnLoad && focus && this[refName] && this[refName].focus(); } componentDidUpdate(prevProps) { const { focus, refName } = this.props; prevProps.focus !== focus && focus && this[refName] && this[refName].focus(); } checkElements = tag => { const children = Object.values(ReactDOM.findDOMNode(this).childNodes); return this.countDOMChildren(children, tag); }; countDOMChildren = (children, tag) => children.reduce( (agg, child) => ( child.tagName === tag ? { ...agg, count: (agg.count += 1) } : agg ), { count: 0, children: children.length } ); getChildrenElements = nameArr => { const { children } = this.props; let elementCount = 0; let childrenLength = 0; React.Children.forEach(children, child => { childrenLength++; if (child.type && nameArr.includes(child.type.displayName)) { return elementCount++; } }); return ( elementCount && { isCorrectUsage: elementCount === childrenLength, length: elementCount } ); }; handleClick = e => { const { disabled, itemIndex, label, onClick, value, } = this.props; const { setSelected } = this.context; Iif(disabled) { e.preventDefault(); e.stopPropagation(); } e.persist(); setSelected && setSelected(e, itemIndex, value, label); onClick && onClick(e); } handleKeyDown = e => { const { disabled, itemIndex, onKeyDown } = this.props; const { handleListKeyDown } = this.context; Iif(disabled) { e.preventDefault(); e.stopPropagation(); } e.persist(); handleListKeyDown && handleListKeyDown(e, itemIndex); onKeyDown && onKeyDown(e); } render() { const { active, children, className, customAnchorNode, customRefProp, disabled, focus, isReadOnly, label, link, refName, role, separator, title, type, ...props } = this.props; const { id } = this.state; const otherProps = omit({...props}, [ 'focusOnLoad', 'id', 'itemIndex', 'onClick', 'onKeyDown', 'value', ]); const addRefToAnchor = node => { return React.cloneElement( node, { 'aria-current': focus, className: 'cui-list-item' + `${(type && ` cui-list-item--${type}`) || ''}` + `${(active && ` active`) || ''}` + `${(disabled && ` disabled`) || ''}` + `${(isReadOnly && ` cui-list-item--read-only`) || ''}` + `${(separator && ` cui-list-item--separator`) || ''}` + `${(className && ` ${className}`) || ''}` + `${(node.props.className && ` ${node.props.className}`) || ''}`, role: role, ...customRefProp && { [customRefProp]: ref => this[this.props.refName] = ref }, ...id && { id }, ...!isReadOnly && { onClick: this.handleClick, onKeyDown: this.handleKeyDown, tabIndex: (!disabled && focus) ? 0 : -1, }, ...otherProps, ...(title || label) && {title: title || label} }, children || node.props.children || label ); }; const customProps = { 'aria-current': focus, className: 'cui-list-item' + `${(type && ` cui-list-item--${type}`) || ''}` + `${(active && ` active`) || ''}` + `${(disabled && ` disabled`) || ''}` + `${(isReadOnly && ` cui-list-item--read-only`) || ''}` + `${(separator && ` cui-list-item--separator`) || ''}` + `${(className && ` ${className}`) || ''}`, id: id, role: role, ref: ref => (this[refName] = ref), ...!isReadOnly && { onClick: this.handleClick, onKeyDown: this.handleKeyDown, tabIndex: (!disabled && focus) ? 0 : -1, }, ...link && { href: link }, ...otherProps, ...(title || label) && { title: title || label } }; return customAnchorNode ? addRefToAnchor(customAnchorNode) : React.createElement( link ? 'a' : 'div', { ...customProps }, children || label ); } } ListItem.propTypes = { /** @prop Active prop to help determine styles | false */ active: PropTypes.bool, /** @prop Children nodes to render inside ListItem | null */ children: PropTypes.node, /** @prop Optional css class string | '' */ className: PropTypes.string, /** @prop Node in which the selection begins | null */ customAnchorNode: PropTypes.element, /** @prop ListItem Custom Prop Name for child with custom Ref | null */ customRefProp: PropTypes.string, /** @prop Disabled attribute for ListItem to determine styles | false */ disabled: PropTypes.bool, /** @prop Specifies if ListItem should automatically get focus | false */ focus: PropTypes.bool, /** @prop Specifies if ListItem should automatically get focus when page loads | false */ focusOnLoad: PropTypes.bool, /** @prop Sets ListItem id | null */ id: PropTypes.string, /** @prop Determines if ListItem is clickable | false */ isReadOnly: PropTypes.bool, /** @prop ListItem index number | null */ itemIndex: PropTypes.number, /** @prop ListItem label text | '' */ label: PropTypes.string, /** @prop external link associated input | '' */ link: PropTypes.string, /** @prop Callback function invoked by user tapping on ListItem | null */ onClick: PropTypes.func, /** @prop Callback function invoked by user pressing on a key | null */ onKeyDown: PropTypes.func, /** @prop ListItem ref name | 'navLink' */ refName: PropTypes.string, /** @prop Aria role | 'listItem' */ role: PropTypes.string, /** @prop Prop that controls whether to show separator or not | false */ separator: PropTypes.bool, /** @prop ListItem Title | '' */ title: PropTypes.string, /** @prop ListItem size | '' */ type: PropTypes.oneOf(['', 'small', 'large', 'xlarge', 'space', 'header', 36, 52, 60]), /** @prop ListItem value for OnSelect value | '' */ value: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, PropTypes.object, PropTypes.array ]), }; ListItem.defaultProps = { active: false, children: null, className: '', customAnchorNode: null, customRefProp: '', disabled: false, focus: false, focusOnLoad: false, id: null, itemIndex: null, isReadOnly: false, label: '', link: '', onClick: null, onKeyDown: null, refName: 'navLink', role: 'listItem', separator: false, title: '', type: '', value: '', }; ListItem.contextTypes = { setSelected: PropTypes.func, handleListKeyDown: PropTypes.func }; ListItem.displayName = 'ListItem'; export default ListItem; /** * @component list * @section default * @react * import { List, ListItem } from '@collab-ui/react'; export default class ListItemDefault extends React.PureComponent { render() { return( <div className="medium-4 columns"> <div> <List> <ListItem label='Default List Item 1' /> <ListItem label='Default List Item 2' /> </List> </div> <div className="cui--contrast"> <List> <ListItem label='List Item 1 (with Contrast)' /> <ListItem label='List Item 2 (with Contrast)' /> </List> </div> </div> ); } } **/ /** * @component list * @section list-item-sections * @react * import { List, ListItem, ListItemSection } from '@collab-ui/react'; export default class ListItemSections extends React.PureComponent { render() { return( <div className="medium-4 columns"> <List> <ListItem link='javascript:void(0)'> <ListItemSection position='left'> Left </ListItemSection> <ListItemSection position='right'> Right </ListItemSection> </ListItem> <ListItem link='javascript:void(0)'> <ListItemSection position='center'> Center </ListItemSection> </ListItem> <ListItem link='javascript:void(0)'> <ListItemSection position='center-align'> Center Align </ListItemSection> </ListItem> <ListItem link='javascript:void(0)'> <ListItemSection position='left'> Left </ListItemSection> <ListItemSection position='center'> Center </ListItemSection> <ListItemSection position='right'> Right </ListItemSection> </ListItem> </List> </div> ); } } **/ /** * @component list * @section list-item-separator * @react * import { List, ListItem, ListItemSection } from '@collab-ui/react'; export default class ListItemSeparator extends React.PureComponent { render() { return( <ListSeparator text='Text Color' textColor='orange' lineColor='red' /> ); } } **/ |