|
| 1 | +import React, { Component } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import classNames from "classnames"; |
| 4 | +import uid from "../lib/uniqueId"; |
| 5 | + |
| 6 | +class StructuredListWrapper extends Component { |
| 7 | + static propTypes = { |
| 8 | + children: PropTypes.node, |
| 9 | + className: PropTypes.string, |
| 10 | + border: PropTypes.bool, |
| 11 | + selection: PropTypes.bool |
| 12 | + }; |
| 13 | + |
| 14 | + static defaultProps = { |
| 15 | + border: false, |
| 16 | + selection: false |
| 17 | + }; |
| 18 | + |
| 19 | + render() { |
| 20 | + const { children, selection, className, border, ...other } = this.props; |
| 21 | + |
| 22 | + const classes = classNames("bx--structured-list", className, { |
| 23 | + "bx--structured-list--border": border, |
| 24 | + "bx--structured-list--selection": selection |
| 25 | + }); |
| 26 | + |
| 27 | + return ( |
| 28 | + <section className={classes} {...other}> |
| 29 | + {children} |
| 30 | + </section> |
| 31 | + ); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class StructuredListHead extends Component { |
| 36 | + static propTypes = { |
| 37 | + children: PropTypes.node, |
| 38 | + className: PropTypes.string |
| 39 | + }; |
| 40 | + |
| 41 | + render() { |
| 42 | + const { children, className, ...other } = this.props; |
| 43 | + |
| 44 | + const classes = classNames("bx--structured-list-thead", className); |
| 45 | + return <div className={classes} {...other}>{children}</div>; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class StructuredListInput extends Component { |
| 50 | + static propTypes = { |
| 51 | + className: PropTypes.string, |
| 52 | + id: PropTypes.string, |
| 53 | + value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, |
| 54 | + name: PropTypes.string, |
| 55 | + title: PropTypes.string, |
| 56 | + defaultChecked: PropTypes.bool, |
| 57 | + onChange: PropTypes.func |
| 58 | + }; |
| 59 | + |
| 60 | + static defaultProps = { |
| 61 | + onChange: () => {}, |
| 62 | + value: 'value' |
| 63 | + }; |
| 64 | + |
| 65 | + componentWillMount() { |
| 66 | + this.uid = this.props.id || uid(); |
| 67 | + } |
| 68 | + |
| 69 | + render() { |
| 70 | + const { className, value, name, title, ...other } = this.props; |
| 71 | + const classes = classNames("bx--structured-list-input", className); |
| 72 | + return ( |
| 73 | + <input |
| 74 | + {...other} |
| 75 | + type="radio" |
| 76 | + tabIndex={-1} |
| 77 | + id={this.uid} |
| 78 | + className={classes} |
| 79 | + value={value} |
| 80 | + name={name} |
| 81 | + title={title} |
| 82 | + /> |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class StructuredListRow extends Component { |
| 88 | + static propTypes = { |
| 89 | + children: PropTypes.node, |
| 90 | + className: PropTypes.string, |
| 91 | + head: PropTypes.bool, |
| 92 | + label: PropTypes.bool, |
| 93 | + htmlFor: PropTypes.string, |
| 94 | + tabIndex: PropTypes.number, |
| 95 | + onKeyDown: PropTypes.func |
| 96 | + }; |
| 97 | + |
| 98 | + static defaultProps = { |
| 99 | + htmlFor: "unique id", |
| 100 | + head: false, |
| 101 | + label: false, |
| 102 | + tabIndex: 0, |
| 103 | + onKeyDown: () => {} |
| 104 | + }; |
| 105 | + |
| 106 | + render() { |
| 107 | + const { |
| 108 | + onKeyDown, |
| 109 | + tabIndex, |
| 110 | + htmlFor, |
| 111 | + children, |
| 112 | + className, |
| 113 | + head, |
| 114 | + label, |
| 115 | + ...other |
| 116 | + } = this.props; |
| 117 | + |
| 118 | + const classes = classNames("bx--structured-list-row", className, { |
| 119 | + "bx--structured-list-row--header-row": head |
| 120 | + }); |
| 121 | + |
| 122 | + return label |
| 123 | + ? <label |
| 124 | + {...other} |
| 125 | + tabIndex={tabIndex} |
| 126 | + className={classes} |
| 127 | + htmlFor={htmlFor} |
| 128 | + onKeyDown={onKeyDown} |
| 129 | + > |
| 130 | + {children} |
| 131 | + </label> |
| 132 | + : <div {...other} className={classes}> |
| 133 | + {children} |
| 134 | + </div>; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +class StructuredListBody extends Component { |
| 139 | + static propTypes = { |
| 140 | + children: PropTypes.node, |
| 141 | + className: PropTypes.string, |
| 142 | + head: PropTypes.bool, |
| 143 | + onKeyDown: PropTypes.func |
| 144 | + }; |
| 145 | + |
| 146 | + static defaultProps = { |
| 147 | + onKeyDown: () => {} |
| 148 | + }; |
| 149 | + |
| 150 | + state = { |
| 151 | + labelRows: null, |
| 152 | + rowSelected: 0 |
| 153 | + }; |
| 154 | + |
| 155 | + render() { |
| 156 | + const { children, className, ...other } = this.props; |
| 157 | + const classes = classNames("bx--structured-list-tbody", className); |
| 158 | + return <div className={classes} {...other}>{children}</div>; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +class StructuredListCell extends Component { |
| 163 | + static propTypes = { |
| 164 | + children: PropTypes.node, |
| 165 | + className: PropTypes.string, |
| 166 | + head: PropTypes.bool, |
| 167 | + noWrap: PropTypes.bool |
| 168 | + }; |
| 169 | + |
| 170 | + static defaultProps = { |
| 171 | + head: false, |
| 172 | + noWrap: false |
| 173 | + }; |
| 174 | + |
| 175 | + render() { |
| 176 | + const { children, className, head, noWrap, ...other } = this.props; |
| 177 | + |
| 178 | + const classes = classNames(className, { |
| 179 | + "bx--structured-list-th": head, |
| 180 | + "bx--structured-list-td": !head, |
| 181 | + "bx--structured-list-content--nowrap": noWrap |
| 182 | + }); |
| 183 | + |
| 184 | + return <div className={classes} {...other}>{children}</div>; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +export { |
| 189 | + StructuredListWrapper, |
| 190 | + StructuredListHead, |
| 191 | + StructuredListBody, |
| 192 | + StructuredListRow, |
| 193 | + StructuredListInput, |
| 194 | + StructuredListCell |
| 195 | +}; |
0 commit comments