@bitrise/bitkit 13.270.0 → 13.271.0

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bitrise/bitkit",
3
3
  "description": "Bitrise React component library",
4
- "version": "13.270.0",
4
+ "version": "13.271.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+ssh://git@github.com/bitrise-io/bitkit.git"
@@ -146,6 +146,26 @@ function findOption<T>(
146
146
  return null;
147
147
  }
148
148
 
149
+ const DropdownLabelCacheContext = createContext<Map<unknown, ReactNode> | null>(null);
150
+
151
+ const useLabelCache = () => {
152
+ const contextCache = useContext(DropdownLabelCacheContext);
153
+ const localCache = useRef<Map<unknown, ReactNode> | null>(null);
154
+ if (contextCache) {
155
+ return contextCache;
156
+ }
157
+ if (!localCache.current) {
158
+ localCache.current = new Map();
159
+ }
160
+ return localCache.current;
161
+ };
162
+
163
+ export const DropdownLabelCache = ({ children }: { children: ReactNode }) => {
164
+ const value = useMemo(() => new Map(), []);
165
+
166
+ return <DropdownLabelCacheContext.Provider value={value}>{children}</DropdownLabelCacheContext.Provider>;
167
+ };
168
+
149
169
  function useDropdown<T>({
150
170
  children,
151
171
  defaultValue,
@@ -208,15 +228,13 @@ function useDropdown<T>({
208
228
  onSearch: () => setActiveIndex(null),
209
229
  });
210
230
 
211
- // this can just be a ref as it will always be updated when formValue is, which will cause a re-render
212
- const labelMapRef = useRef<Map<T, ReactNode>>();
213
- if (!labelMapRef.current) labelMapRef.current = new Map();
231
+ const labelCache = useLabelCache();
214
232
 
215
233
  // clear map when value is changed from the outside
216
234
  useEffect(() => {
217
- if (labelMapRef.current && Array.isArray(value)) {
218
- const removed = [...labelMapRef.current.keys()].filter((key) => !value.includes(key));
219
- removed.forEach((remove) => labelMapRef.current?.delete(remove));
235
+ if (Array.isArray(value)) {
236
+ const removed = [...labelCache.keys()].filter((key) => !value.includes(key));
237
+ removed.forEach((remove) => labelCache.delete(remove));
220
238
  }
221
239
  }, [value]);
222
240
 
@@ -225,10 +243,10 @@ function useDropdown<T>({
225
243
  setFormValue((previous) => {
226
244
  if (Array.isArray(previous)) {
227
245
  if (previous.includes(args.value)) {
228
- labelMapRef.current?.delete(args.value);
246
+ labelCache.delete(args.value);
229
247
  return previous.filter((aPrevious) => aPrevious !== args.value) as T;
230
248
  }
231
- labelMapRef.current?.set(args.value, args.label);
249
+ labelCache.set(args.value, args.label);
232
250
 
233
251
  return [...previous, args.value] as T;
234
252
  }
@@ -312,7 +330,7 @@ function useDropdown<T>({
312
330
  }}
313
331
  size="sm"
314
332
  >
315
- {labelMapRef.current?.get(formValueItem) || formValueItem}
333
+ {labelCache.get(formValueItem) || formValueItem}
316
334
  </Tag>
317
335
  );
318
336
  })}
package/src/index.ts CHANGED
@@ -149,6 +149,7 @@ export type { DropdownProps } from './Components/Dropdown/DropdownProps';
149
149
  export {
150
150
  default as Dropdown,
151
151
  MultiSelectDropdown,
152
+ DropdownLabelCache,
152
153
  DropdownOption,
153
154
  DropdownGroup,
154
155
  DropdownSearch,