-
Notifications
You must be signed in to change notification settings - Fork 181
/
title.tsx
112 lines (93 loc) · 2.72 KB
/
title.tsx
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
import React, {PureComponent, ReactNode, ComponentType} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import focusSensorHOC, {FocusSensorAddProps} from '../global/focus-sensor-hoc';
import Checkbox from '../checkbox/checkbox';
import getUID from '../global/get-uid';
import {refObject} from '../global/prop-types';
import styles from './data-list.css';
export interface TitleProps extends FocusSensorAddProps<HTMLDivElement> {
onSelect: (selected: boolean) => void
selectable?: boolean | undefined
selected?: boolean | undefined
showFocus?: boolean | undefined
offset?: number | undefined
className?: string | null | undefined
title?: ReactNode
collapserExpander?: ReactNode
}
class Title extends PureComponent<TitleProps> {
static defaultProps = {
selectable: false,
selected: false,
showFocus: false
};
id = getUID('data-list-title');
onCheckboxFocus = () => {
this.props.onFocusRestore();
};
onCheckboxChange = () => {
this.toggleSelection();
};
toggleSelection() {
const {selectable, selected, onSelect} = this.props;
if (selectable) {
onSelect(!selected);
}
}
render() {
const {
className, title, offset, showFocus, innerRef,
selectable, selected, collapserExpander
} = this.props;
const classes = classNames(className, {
[styles.title]: true,
[styles.titleFocused]: showFocus,
[styles.titleSelected]: selected
});
return (
<div
id={this.id}
className={classes}
style={{paddingLeft: offset}}
ref={innerRef}
>
<div className={styles.boxes}>
{selectable &&
(
<div className={styles.checkboxBox}>
<Checkbox
aria-labelledby={this.id}
className={showFocus ? 'ring-checkbox_focus' : ''}
checked={selected}
onFocus={this.onCheckboxFocus}
onChange={this.onCheckboxChange}
tabIndex={-1}
/>
</div>
)
}
{collapserExpander}
</div>
{title}
</div>
);
}
}
(Title as ComponentType<unknown>).propTypes = {
className: PropTypes.string,
title: PropTypes.node,
offset: PropTypes.number,
selectable: PropTypes.bool,
selected: PropTypes.bool,
onSelect: PropTypes.func.isRequired,
showFocus: PropTypes.bool,
collapserExpander: PropTypes.node,
innerRef: PropTypes.oneOfType([
refObject(PropTypes.instanceOf(HTMLDivElement)),
PropTypes.func
]),
// focusSensorHOC
onFocusRestore: PropTypes.func.isRequired
};
export default focusSensorHOC<HTMLDivElement, TitleProps, typeof Title>(Title);