-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.jsx
76 lines (65 loc) · 1.59 KB
/
index.jsx
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
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { withForwardedRef } from '@shopgate/engage/core';
import styles from './style';
/**
* The SheetItem component.
*/
class SheetItem extends PureComponent {
static propTypes = {
item: PropTypes.shape().isRequired,
forwardedRef: PropTypes.shape(),
onClick: PropTypes.func,
rightComponent: PropTypes.func,
selected: PropTypes.bool,
};
static defaultProps = {
forwardedRef: null,
onClick() { },
rightComponent: null,
selected: false,
};
/**
* @param {boolean} selectable Whether or not the item can be selected.
* @returns {string}
*/
getStyle = (selectable) => {
const { selected } = this.props;
if (selected) {
return styles.buttonSelected;
}
if (!selectable) {
return styles.buttonDisabled;
}
return styles.button;
};
/**
* @returns {Object}
*/
buildProps = () => {
const {
item, onClick, forwardedRef,
} = this.props;
return {
className: this.getStyle(item.selectable),
key: item.id,
ref: forwardedRef,
value: item.id,
'aria-hidden': !item.selectable,
...item.selectable && { onClick },
};
};
/**
* @returns {JSX}
*/
render() {
const { item, rightComponent: Right, selected } = this.props;
return (
<button {...this.buildProps()} data-test-id={item.label} aria-selected={selected} role="option" type="button">
{item.label}
{item.selectable && <Right />}
</button>
);
}
}
export default withForwardedRef(SheetItem);