dce-reactkit 3.2.2-beta.20 → 3.2.2-beta.21

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.
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Reusable nested item picker
3
3
  * @author Yuen Ler Chow
4
+ * @author Gabe Abrams
4
5
  */
5
6
  import React from 'react';
6
7
  import PickableItem from './types/PickableItem';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "3.2.2-beta.20",
3
+ "version": "3.2.2-beta.21",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Reusable nested item picker
3
3
  * @author Yuen Ler Chow
4
+ * @author Gabe Abrams
4
5
  */
5
6
 
6
7
  // Import React
@@ -43,36 +44,46 @@ type Props = {
43
44
  /* -------- State Definition -------- */
44
45
 
45
46
  type State = {
46
- // True if children are being shown
47
- isShowingItems: boolean,
47
+ // Map of children that are expanded
48
+ childExpanded: {
49
+ [k: string]: boolean
50
+ },
48
51
  };
49
52
 
50
53
  /* ------------- Actions ------------ */
51
54
 
52
55
  // Types of actions
53
56
  enum ActionType {
54
- // Toggle whether the children are being shown
55
- ToggleItems = 'toggle-items',
57
+ // Toggle whether a child are being shown
58
+ ToggleChild = 'toggle-child',
56
59
  }
57
60
 
58
61
  // Action definitions
59
62
  type Action = (
60
63
  | {
61
64
  // Action type
62
- type: ActionType.ToggleItems,
65
+ type: ActionType.ToggleChild,
66
+ // Id of the child to show
67
+ id: (string | number),
63
68
  }
64
69
  );
65
70
 
66
71
  /**
67
72
  * Reducer that executes actions
68
- * @author Yuen Ler Chow
73
+ * @author Gabe Abrams
69
74
  * @param state current state
70
75
  * @param action action to execute
71
76
  */
72
77
  const reducer = (state: State, action: Action): State => {
73
78
  switch (action.type) {
74
- case ActionType.ToggleItems: {
75
- return { isShowingItems: !state.isShowingItems };
79
+ case ActionType.ToggleChild: {
80
+ return {
81
+ ...state,
82
+ childExpanded: {
83
+ ...state.childExpanded,
84
+ [String(action.id)]: !state.childExpanded[String(action.id)],
85
+ },
86
+ };
76
87
  }
77
88
  default: {
78
89
  return state;
@@ -99,9 +110,15 @@ const NestableItemList: React.FC<Props> = (props) => {
99
110
 
100
111
  /* -------------- State ------------- */
101
112
 
113
+ // Create initial map of child expanded booleans
114
+ const initChildExpanded: { [k: string]: boolean } = {};
115
+ items.forEach((item) => {
116
+ initChildExpanded[String(item.id)] = false;
117
+ });
118
+
102
119
  // Initial state
103
120
  const initialState: State = {
104
- isShowingItems: false,
121
+ childExpanded: initChildExpanded,
105
122
  };
106
123
 
107
124
  // Initialize state
@@ -109,7 +126,7 @@ const NestableItemList: React.FC<Props> = (props) => {
109
126
 
110
127
  // Destructure common state
111
128
  const {
112
- isShowingItems,
129
+ childExpanded,
113
130
  } = state;
114
131
 
115
132
  /*------------------------------------------------------------------------*/
@@ -231,13 +248,14 @@ const NestableItemList: React.FC<Props> = (props) => {
231
248
  type="button"
232
249
  onClick={() => {
233
250
  dispatch({
234
- type: ActionType.ToggleItems,
251
+ type: ActionType.ToggleChild,
252
+ id: item.id,
235
253
  });
236
254
  }}
237
- aria-label={`${isShowingItems ? 'Hide' : 'Show'} items in ${item.name}`}
255
+ aria-label={`${childExpanded[item.id] ? 'Hide' : 'Show'} items in ${item.name}`}
238
256
  >
239
257
  <FontAwesomeIcon
240
- icon={isShowingItems ? faChevronDown : faChevronRight}
258
+ icon={childExpanded[item.id] ? faChevronDown : faChevronRight}
241
259
  />
242
260
  </button>
243
261
  )}
@@ -257,7 +275,7 @@ const NestableItemList: React.FC<Props> = (props) => {
257
275
  />
258
276
 
259
277
  {/* Children */}
260
- {item.isGroup && isShowingItems && (
278
+ {(item.isGroup && childExpanded[item.id]) && (
261
279
  <div
262
280
  className="NestableItemList-children-container"
263
281
  style={{
@@ -994,7 +994,7 @@ const LogReviewer: React.FC<Props> = (props) => {
994
994
  })
995
995
  .map((subcontext) => {
996
996
  return {
997
- id: `${context}-${subcontext}`,
997
+ id: subcontext,
998
998
  name: genHumanReadableName(subcontext),
999
999
  isGroup: false,
1000
1000
  checked: !!value[subcontext],