This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
/
ListBoxSelection.js
90 lines (80 loc) · 2.4 KB
/
ListBoxSelection.js
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
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import Close16 from '@carbon/icons-react/lib/close/16';
import { settings } from 'carbon-components';
const { prefix } = settings;
/**
* `ListBoxSelection` is used to provide controls for clearing a selection, in
* addition to conditionally rendering a badge if the control has more than one
* selection.
*/
const ListBoxSelection = ({
clearSelection,
selectionCount,
translateWithId: t,
}) => {
const className = cx({
[`${prefix}--list-box__selection`]: true,
[`${prefix}--list-box__selection--multi`]: selectionCount,
});
const handleOnClick = event => {
event.stopPropagation();
clearSelection(event);
};
const handleOnKeyDown = event => {
// When a user hits ENTER, we'll clear the selection
if (event.keyCode === 13) {
clearSelection(event);
}
};
const description = selectionCount ? t('clear.all') : t('clear.selection');
return (
<div
role="button"
className={className}
tabIndex="0"
onClick={handleOnClick}
onKeyDown={handleOnKeyDown}
title={description}>
{selectionCount}
<Close16 role="img" />
</div>
);
};
export const translationIds = {
'clear.all': 'clear.all',
'clear.selection': 'clear.selection',
};
const defaultTranslations = {
[translationIds['clear.all']]: 'Clear all selected items',
[translationIds['clear.selection']]: 'Clear selected item',
};
ListBoxSelection.propTypes = {
/**
* Specify a function to be invoked when a user interacts with the clear
* selection element.
*/
clearSelection: PropTypes.func.isRequired,
/**
* Specify an optional `selectionCount` value that will be used to determine
* whether the selection should display a badge or a single clear icon.
*/
selectionCount: PropTypes.number,
/**
* i18n hook used to provide the appropriate description for the given menu
* icon. This function takes in an id defined in `translationIds` and should
* return a string message for that given message id.
*/
translateWithId: PropTypes.func.isRequired,
};
ListBoxSelection.defaultProps = {
translateWithId: id => defaultTranslations[id],
};
export default ListBoxSelection;