forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectedValuesList.js
More file actions
112 lines (103 loc) · 3.21 KB
/
Copy pathSelectedValuesList.js
File metadata and controls
112 lines (103 loc) · 3.21 KB
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 from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import ValueChip from './ValueChip';
import css from './MultiSelect.css';
class SelectedValuesList extends React.Component {
static propTypes = {
atSmallMedia: PropTypes.bool,
formatter: PropTypes.func,
getRemoveButtonProps: PropTypes.func,
id: PropTypes.string,
inputRef: PropTypes.object,
itemToString: PropTypes.func,
selectedItems: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(PropTypes.object)
]),
valueDescriptionId: PropTypes.string,
valueLabelId: PropTypes.string,
}
// componentDidUpdate(prevProps) {
// if (prevProps.removeButtonUsed === -1 && this.props.removeButtonUsed !== -1) {
// this.handleFocusAfterRemoval(this.props.removeButtonUsed);
// }
// }
valueList = React.createRef();
handleRemoveButtonKeyDown = e => {
switch (e.keyCode) {
case 35: // end
this.valueList.current.querySelector('li:last-child button').focus();
break;
case 36: // home
this.valueList.current.querySelector('li:first-child button').focus();
break;
default:
}
};
handleFocusAfterRemoval = (index, remaining) => {
if (remaining === 0 && !this.props.atSmallMedia) {
if (this.props.inputRef.current) {
this.props.inputRef.current.focus();
}
} else {
setTimeout(() => {
let neighbor = index;
if (index === 0) {
neighbor = 1;
}
if (this.valueList.current) {
const targetButton = this.valueList.current.querySelector(`li:nth-child(${neighbor}n) button`);
targetButton.focus();
}
}, 20);
}
};
render() {
const {
selectedItems,
getRemoveButtonProps,
valueLabelId,
valueDescriptionId,
itemToString,
id,
formatter,
} = this.props;
if (selectedItems && selectedItems.length > 0) {
return (
<div className={css.multiSelectValueListContainer} id={id}>
<span className="sr-only" id={valueLabelId}>
<FormattedMessage id="stripes-components.multiSelection.removeSelectedButtonLabel" />
</span>
<span className="sr-only" id={valueDescriptionId}>
<FormattedMessage id="stripes-components.multiSelection.removeSelectedButtonDescription" />
</span>
<ul
className={css.multiSelectValueList}
ref={this.valueList}
>
{selectedItems.map((item, index) => (
<ValueChip
key={`${itemToString(item)}-${index}`}
id={`${itemToString(item)}-${index}`}
controlLabelId={valueLabelId}
descriptionId={valueDescriptionId}
removeButtonProps={
getRemoveButtonProps({
item,
index,
onKeyDown: this.handleRemoveButtonKeyDown,
})}
>
{formatter({ option: item })}
</ValueChip>
))}
</ul>
</div>
);
}
return null;
}
}
export default SelectedValuesList;