-
Notifications
You must be signed in to change notification settings - Fork 36
/
Selector.jsx
137 lines (128 loc) · 4.58 KB
/
Selector.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) [2024] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/
// @ts-check
import React from "react";
import { _ } from "~/i18n";
import { noop } from "~/utils";
/**
* @callback onSelectionChangeCallback
* @param {Array<string>} selection - ids of selected options
*/
/**
* Agama component for building a selector
*
* @example <caption>Usage example</caption>
* const options = [
* { id: "es_ES", country: "Spain", label: "Spanish" },
* { id: "cs_CZ", country: "Czechia", label: "Czech" },
* { id: "de_DE", country: "Germany", label: "German" },
* { id: "en_GB", country: "United Kingdom", label: "English" }
* ];
*
* const selectedIds = ["es_ES", "en_GB"];
*
* const renderFn = ({ label, country }) => <div>{label} - {country}</div>;
*
* return (
* <Selector
* isMultiple
* aria-label="Available locales"
* selectedIds={selectedIds}
* options={options}
* renderOption={renderFn}
* onSelectionChange={(selection) => changePreferredLocales(selection)}
* />
* );
*
* @param {object} props - component props
* @param {string} [props.id] - Id attribute for selector.
* @param {boolean} [props.isMultiple=false] - Whether the selector should allow multiple selection.
* @param {Array<object>} props.options=[] - Item objects to build options.
* @param {function} props.renderOption=noop - Function used for rendering options.
* @param {string} [props.optionIdKey="id"] - Key used for retrieve options id.
* @param {Array<*>} [props.selectedIds=[]] - Identifiers for selected options.
* @param {function} props.autoSelectionCheck=noop - Function used to check if option should be marked as auto selected.
* @param {onSelectionChangeCallback} [props.onSelectionChange=noop] - Callback to be called when the selection changes.
* @param {function|undefined} [props.onOptionClick] - Callback to be called when the selection changes.
* @param {object} [props.props] - Other props sent to the internal selector <ul> component
*/
const Selector = ({
id = crypto.randomUUID(),
isMultiple = false,
options = [],
renderOption = noop,
optionIdKey = "id",
selectedIds = [],
autoSelectionCheck = noop,
onSelectionChange = noop,
onOptionClick,
...props
}) => {
const onOptionClicked = (optionId) => {
if (typeof onOptionClick === "function") return onOptionClick(optionId);
const alreadySelected = selectedIds.includes(optionId);
if (!isMultiple) {
!alreadySelected && onSelectionChange([optionId]);
return;
}
if (alreadySelected) {
onSelectionChange(selectedIds.filter((id) => id !== optionId));
} else {
onSelectionChange([...selectedIds, optionId]);
}
};
return (
<ul {...props} id={id} data-type="agama/list" role="grid">
{options.map((option) => {
const optionId = option[optionIdKey];
const optionHtmlId = `${id}-option-${optionId}`;
const isSelected = selectedIds.includes(optionId);
const isAutoSelected = isSelected && autoSelectionCheck(option);
const onClick = () => onOptionClicked(optionId);
return (
<li
key={optionId}
id={optionHtmlId}
role="row"
onClick={onClick}
aria-selected={isSelected || undefined}
data-auto-selected={isAutoSelected || undefined}
>
<div role="gridcell">
<div>
<input
type={isMultiple ? "checkbox" : "radio"}
checked={isSelected}
onChange={onClick}
aria-labelledby={optionHtmlId}
data-auto-selected={isAutoSelected || undefined}
/>
{isAutoSelected && <div><span>{_("auto selected")}</span></div>}
</div>
{renderOption(option)}
</div>
</li>
);
})}
</ul>
);
};
export default Selector;