dce-reactkit 5.0.5 → 5.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,7 @@ type Props = {
18
18
  small?: boolean;
19
19
  dashed?: boolean;
20
20
  useComplexFormatting?: boolean;
21
+ minSizeToShowLabel?: 'sm' | 'md' | 'lg' | 'xl';
21
22
  };
22
23
  declare const CheckboxButton: React.FC<Props>;
23
24
  export default CheckboxButton;
@@ -13,6 +13,7 @@ type Props = {
13
13
  * values updated
14
14
  */
15
15
  onChanged: (updatedItems: PickableItem[]) => void;
16
+ startWithGroupsExpanded?: boolean;
16
17
  };
17
18
  declare const NestableItemList: React.FC<Props>;
18
19
  export default NestableItemList;
@@ -15,6 +15,7 @@ type Props = {
15
15
  onChanged: (updatedItems: PickableItem[]) => void;
16
16
  noBottomMargin?: boolean;
17
17
  hideSelectAllOrNoneButtons?: boolean;
18
+ startWithGroupsExpanded?: boolean;
18
19
  };
19
20
  declare const ItemPicker: React.FC<Props>;
20
21
  export default ItemPicker;
@@ -1,11 +1,14 @@
1
+ /// <reference types="react" />
1
2
  /**
2
3
  * An item that can be chosen (for use within ItemPicker)
3
4
  * @author Gabe Abrams
4
5
  */
5
6
  type PickableItem = ({
6
7
  id: number | string;
7
- name: string;
8
+ name: React.ReactNode;
9
+ ariaLabel?: string;
8
10
  link?: string;
11
+ tooltip?: string;
9
12
  } & ({
10
13
  isGroup: false;
11
14
  checked: boolean;
package/dist/index.d.ts CHANGED
@@ -278,6 +278,7 @@ type Props$l = {
278
278
  small?: boolean;
279
279
  dashed?: boolean;
280
280
  useComplexFormatting?: boolean;
281
+ minSizeToShowLabel?: 'sm' | 'md' | 'lg' | 'xl';
281
282
  };
282
283
  declare const CheckboxButton: React$1.FC<Props$l>;
283
284
 
@@ -441,8 +442,10 @@ declare const CopiableBox: React$1.FC<Props$c>;
441
442
  */
442
443
  type PickableItem = ({
443
444
  id: number | string;
444
- name: string;
445
+ name: React.ReactNode;
446
+ ariaLabel?: string;
445
447
  link?: string;
448
+ tooltip?: string;
446
449
  } & ({
447
450
  isGroup: false;
448
451
  checked: boolean;
@@ -467,6 +470,7 @@ type Props$b = {
467
470
  onChanged: (updatedItems: PickableItem[]) => void;
468
471
  noBottomMargin?: boolean;
469
472
  hideSelectAllOrNoneButtons?: boolean;
473
+ startWithGroupsExpanded?: boolean;
470
474
  };
471
475
  declare const ItemPicker: React$1.FC<Props$b>;
472
476
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -48,6 +48,8 @@ type Props = {
48
48
  dashed?: boolean,
49
49
  // If true, use complex formatting (tabs, newlines) in the text
50
50
  useComplexFormatting?: boolean,
51
+ // Min bootstrap size to show the label
52
+ minSizeToShowLabel?: 'sm' | 'md' | 'lg' | 'xl',
51
53
  };
52
54
 
53
55
  /*------------------------------------------------------------------------*/
@@ -83,6 +85,7 @@ const CheckboxButton: React.FC<Props> = (props) => {
83
85
  small,
84
86
  dashed,
85
87
  useComplexFormatting,
88
+ minSizeToShowLabel,
86
89
  } = props;
87
90
 
88
91
  /*------------------------------------------------------------------------*/
@@ -118,30 +121,32 @@ const CheckboxButton: React.FC<Props> = (props) => {
118
121
  }}
119
122
  >
120
123
  <div className="d-flex align-items-center">
121
- <div className="me-1">
124
+ <div>
122
125
  <FontAwesomeIcon
123
126
  icon={icon}
124
127
  />
125
128
  </div>
126
- {
127
- useComplexFormatting
128
- ? (
129
- <pre
130
- className="ps-1 text-start text-break m-0"
131
- style={{
132
- whiteSpace: 'pre-wrap',
133
- tabSize: 2,
134
- }}
135
- >
136
- {text}
137
- </pre>
138
- )
139
- : (
140
- <div className="flex-grow-1 text-start text-break">
141
- {text}
142
- </div>
143
- )
144
- }
129
+ <div className={`text-start flex-grow-1 ms-1 ${minSizeToShowLabel ? `d-none d-${minSizeToShowLabel}-block` : ''}`}>
130
+ {
131
+ useComplexFormatting
132
+ ? (
133
+ <pre
134
+ className="ps-1 text-break m-0"
135
+ style={{
136
+ whiteSpace: 'pre-wrap',
137
+ tabSize: 2,
138
+ }}
139
+ >
140
+ {text}
141
+ </pre>
142
+ )
143
+ : (
144
+ <div className="text-break">
145
+ {text}
146
+ </div>
147
+ )
148
+ }
149
+ </div>
145
150
  </div>
146
151
  </button>
147
152
  );
@@ -16,6 +16,7 @@ import {
16
16
 
17
17
  // Import shared components
18
18
  import CheckboxButton from '../CheckboxButton';
19
+ import Tooltip from '../Tooltip';
19
20
  import Variant from '../../types/Variant';
20
21
 
21
22
  // Import types
@@ -35,6 +36,8 @@ type Props = {
35
36
  * values updated
36
37
  */
37
38
  onChanged: (updatedItems: PickableItem[]) => void,
39
+ // If true, groups start expanded instead of collapsed
40
+ startWithGroupsExpanded?: boolean,
38
41
  };
39
42
 
40
43
  /*------------------------------------------------------------------------*/
@@ -106,6 +109,7 @@ const NestableItemList: React.FC<Props> = (props) => {
106
109
  const {
107
110
  items,
108
111
  onChanged,
112
+ startWithGroupsExpanded,
109
113
  } = props;
110
114
 
111
115
  /* -------------- State ------------- */
@@ -113,7 +117,7 @@ const NestableItemList: React.FC<Props> = (props) => {
113
117
  // Create initial map of child expanded booleans
114
118
  const initChildExpanded: { [k: string]: boolean } = {};
115
119
  items.forEach((item) => {
116
- initChildExpanded[String(item.id)] = false;
120
+ initChildExpanded[String(item.id)] = !!startWithGroupsExpanded;
117
121
  });
118
122
 
119
123
  // Initial state
@@ -229,6 +233,29 @@ const NestableItemList: React.FC<Props> = (props) => {
229
233
  <div>
230
234
  {
231
235
  items.map((item) => {
236
+ // Human-readable label for accessibility. Prefer the explicit
237
+ // ariaLabel, then fall back to the name if it is a plain string
238
+ const accessibleName = (
239
+ item.ariaLabel
240
+ ?? (typeof item.name === 'string' ? item.name : '')
241
+ );
242
+
243
+ // Checkbox and Text
244
+ const checkbox = (
245
+ <CheckboxButton
246
+ className={`NestableItemList-CheckboxButton-${item.id}`}
247
+ text={item.name}
248
+ checked={item.isGroup ? allChecked(item.children) : item.checked}
249
+ dashed={item.isGroup ? !noneChecked(item.children) : false}
250
+ onChanged={(checked) => {
251
+ onChanged(changeChecked(item.id, checked, items));
252
+ }}
253
+ ariaLabel={`Select ${accessibleName}`}
254
+ checkedVariant={Variant.Light}
255
+ uncheckedVariant={Variant.Light}
256
+ />
257
+ );
258
+
232
259
  return (
233
260
  <div key={item.id}>
234
261
  {/* Dropdown Button */}
@@ -252,7 +279,7 @@ const NestableItemList: React.FC<Props> = (props) => {
252
279
  id: item.id,
253
280
  });
254
281
  }}
255
- aria-label={`${childExpanded[item.id] ? 'Hide' : 'Show'} items in ${item.name}`}
282
+ aria-label={`${childExpanded[item.id] ? 'Hide' : 'Show'} items in ${accessibleName}`}
256
283
  >
257
284
  <FontAwesomeIcon
258
285
  icon={childExpanded[item.id] ? faChevronDown : faChevronRight}
@@ -261,19 +288,18 @@ const NestableItemList: React.FC<Props> = (props) => {
261
288
  )}
262
289
  </span>
263
290
 
264
- {/* Checkbox and Text */}
265
- <CheckboxButton
266
- className={`NestableItemList-CheckboxButton-${item.id}`}
267
- text={item.name}
268
- checked={item.isGroup ? allChecked(item.children) : item.checked}
269
- dashed={item.isGroup ? !noneChecked(item.children) : false}
270
- onChanged={(checked) => {
271
- onChanged(changeChecked(item.id, checked, items));
272
- }}
273
- ariaLabel={`Select ${item.name}`}
274
- checkedVariant={Variant.Light}
275
- uncheckedVariant={Variant.Light}
276
- />
291
+ {/* Checkbox and Text (optionally wrapped in a tooltip) */}
292
+ {
293
+ item.tooltip
294
+ ? (
295
+ <Tooltip text={item.tooltip}>
296
+ <span className="d-inline-block">
297
+ {checkbox}
298
+ </span>
299
+ </Tooltip>
300
+ )
301
+ : checkbox
302
+ }
277
303
 
278
304
  {/* Children */}
279
305
  {(item.isGroup && childExpanded[item.id]) && (
@@ -288,6 +314,7 @@ const NestableItemList: React.FC<Props> = (props) => {
288
314
  onChanged={(updatedItems) => {
289
315
  onChanged(changeItems(item.id, updatedItems));
290
316
  }}
317
+ startWithGroupsExpanded={startWithGroupsExpanded}
291
318
  />
292
319
  </div>
293
320
  )}
@@ -35,6 +35,8 @@ type Props = {
35
35
  noBottomMargin?: boolean,
36
36
  // If true, hide select all or none buttons
37
37
  hideSelectAllOrNoneButtons?: boolean,
38
+ // If true, groups start expanded instead of collapsed
39
+ startWithGroupsExpanded?: boolean,
38
40
  };
39
41
 
40
42
  /*------------------------------------------------------------------------*/
@@ -55,6 +57,7 @@ const ItemPicker: React.FC<Props> = (props) => {
55
57
  onChanged,
56
58
  noBottomMargin,
57
59
  hideSelectAllOrNoneButtons,
60
+ startWithGroupsExpanded,
58
61
  } = props;
59
62
 
60
63
  /*------------------------------------------------------------------------*/
@@ -171,6 +174,7 @@ const ItemPicker: React.FC<Props> = (props) => {
171
174
  <NestableItemList
172
175
  items={items}
173
176
  onChanged={onChanged}
177
+ startWithGroupsExpanded={startWithGroupsExpanded}
174
178
  />
175
179
  </div>
176
180
  </TabBox>
@@ -7,9 +7,15 @@ type PickableItem = (
7
7
  // Unique id of the item
8
8
  id: number | string,
9
9
  // Name of the item (human readable)
10
- name: string,
10
+ name: React.ReactNode,
11
+ // Accessible label for the item, used for screen readers. Required when
12
+ // "name" is not a plain string (e.g. a custom ReactNode); when "name" is
13
+ // a string, it is used as the label if this is not provided
14
+ ariaLabel?: string,
11
15
  // Link to view the item in the browser
12
16
  link?: string,
17
+ // If included, show this text in a tooltip when hovering over the item
18
+ tooltip?: string,
13
19
  }
14
20
  & (
15
21
  | {