@swan-io/lake 9.1.0 → 9.1.1

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-io/lake",
3
- "version": "9.1.0",
3
+ "version": "9.1.1",
4
4
  "engines": {
5
5
  "node": ">=20.9.0",
6
6
  "yarn": "^1.22.0"
@@ -1,24 +1,24 @@
1
1
  import { StyleProp, ViewStyle } from "react-native";
2
2
  import { ColorVariants } from "../constants/design";
3
- export type MultiSelectItem = {
3
+ export type MultiSelectItem<Value> = {
4
4
  disabled?: boolean;
5
5
  group: string;
6
6
  label: string;
7
- value: string;
7
+ value: Value;
8
8
  };
9
- export type MultiSelectProps<I> = {
9
+ export type MultiSelectProps<Value> = {
10
10
  color?: ColorVariants;
11
11
  disabled?: boolean;
12
12
  error?: string;
13
13
  emptyResultText?: string;
14
14
  enableGroups?: boolean;
15
15
  filterPlaceholder?: string;
16
- items: I[];
17
- onValueChange: (values: string[]) => void;
16
+ items: MultiSelectItem<Value>[];
17
+ onValueChange: (values: Value[]) => void;
18
18
  placeholder?: string;
19
- renderTagGroup?: (items: readonly MultiSelectItem[]) => string;
19
+ renderTagGroup?: (items: readonly MultiSelectItem<Value>[]) => string;
20
20
  style?: StyleProp<ViewStyle>;
21
- values: string[];
21
+ values: Value[];
22
22
  id?: string;
23
23
  };
24
- export declare const MultiSelect: import("react").NamedExoticComponent<MultiSelectProps<MultiSelectItem>>;
24
+ export declare const MultiSelect: <Value>({ color, disabled, emptyResultText, enableGroups, filterPlaceholder, items, onValueChange, placeholder, renderTagGroup, error, style, values, id, }: MultiSelectProps<Value>) => import("react/jsx-runtime").JSX.Element;
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { Array, Dict, Option } from "@swan-io/boxed";
3
- import { memo, useEffect, useMemo, useRef, useState } from "react";
3
+ import { useEffect, useMemo, useRef, useState } from "react";
4
4
  import { Pressable, StyleSheet, Text, TextInput, View, } from "react-native";
5
5
  import { backgroundColor, colors, radii, shadows, texts } from "../constants/design";
6
6
  import { useBoolean } from "../hooks/useBoolean";
@@ -137,7 +137,7 @@ const styles = StyleSheet.create({
137
137
  color: colors.gray.primary,
138
138
  },
139
139
  });
140
- export const MultiSelect = memo(({ color = "gray", disabled = false, emptyResultText, enableGroups = true, filterPlaceholder, items, onValueChange, placeholder, renderTagGroup, error, style, values, id, }) => {
140
+ export const MultiSelect = ({ color = "gray", disabled = false, emptyResultText, enableGroups = true, filterPlaceholder, items, onValueChange, placeholder, renderTagGroup, error, style, values, id, }) => {
141
141
  const [filter, setFilter] = useState("");
142
142
  const [filterFocused, setFilterFocused] = useBoolean(false);
143
143
  const shouldScrollToBottomRef = useRef(false);
@@ -176,11 +176,14 @@ export const MultiSelect = memo(({ color = "gray", disabled = false, emptyResult
176
176
  onValueChange(newValue);
177
177
  };
178
178
  // Used to get full selected value informations in O(1).
179
- const valueItemMapping = useMemo(() => items.reduce((acc, item) => {
180
- acc[item.value] = item;
181
- return acc;
182
- }, {}), [items]);
183
- const selectedTags = useMemo(() => values.map(value => valueItemMapping[value]).filter(isNotNullish), [values, valueItemMapping]);
179
+ const valueItemMapping = useMemo(() => {
180
+ const map = new Map();
181
+ items.forEach(item => {
182
+ map.set(item.value, item);
183
+ });
184
+ return map;
185
+ }, [items]);
186
+ const selectedTags = useMemo(() => values.map(value => valueItemMapping.get(value)).filter(isNotNullish), [values, valueItemMapping]);
184
187
  const filteredItems = useMemo(() => {
185
188
  const remainingTags = items.filter(item => !values.includes(item.value));
186
189
  const cleanedFilter = filter.trim().toLowerCase();
@@ -199,13 +202,13 @@ export const MultiSelect = memo(({ color = "gray", disabled = false, emptyResult
199
202
  (focused || visible) && styles.focused,
200
203
  disabled && styles.disabled,
201
204
  isNotNullish(error) && styles.errored,
202
- ], children: [_jsx(Box, { ref: selectedTagListRef, alignItems: "center", direction: "row", style: styles.tagsList, children: selectedTags.length > 0 ? (selectedTags.map(item => (_jsx(Tag, { color: color, onPressRemove: disabled ? undefined : () => handleRemoveItem(item), style: styles.tag, children: item.label }, item.value)))) : placeholder !== "" ? (_jsx(Text, { role: "label", numberOfLines: 1, style: styles.placeholder, children: placeholder })) : null }), _jsxs(Box, { direction: "row", alignItems: "center", style: styles.actions, children: [selectedTags.length >= 1 && !disabled && (_jsxs(_Fragment, { children: [_jsx(Pressable, { role: "button", onPress: handleClearAll, children: _jsx(Icon, { name: "dismiss-filled", color: colors.gray.primary, size: 15 }) }), _jsx(Space, { width: 8 })] })), _jsx(Icon, { color: colors.gray.primary, name: visible ? "chevron-up-filled" : "chevron-down-filled", size: 20 })] })] }), _jsx(Popover, { role: "listbox", matchReferenceWidth: true, onDismiss: close, referenceRef: inputRef, returnFocus: true, visible: visible, children: _jsx(View, { style: styles.list, children: enableGroups ? (_jsx(SectionList, { role: "listbox", "aria-multiselectable": true, keyExtractor: (item, index) => `group-field-${item.value}-${index}`, ListHeaderComponent: ListHeaderComponent, ListEmptyComponent: ListEmptyComponent, ListFooterComponent: _jsx(Space, { height: 16 }), sections: sections, renderSectionHeader: ({ title, data }) => (_jsxs(Pressable, { role: "listitem", onPress: () => handleSelectGroup(data), style: ({ hovered, pressed, focused }) => [
205
+ ], children: [_jsx(Box, { ref: selectedTagListRef, alignItems: "center", direction: "row", style: styles.tagsList, children: selectedTags.length > 0 ? (selectedTags.map(item => (_jsx(Tag, { color: color, onPressRemove: disabled ? undefined : () => handleRemoveItem(item), style: styles.tag, children: item.label }, String(item.value))))) : placeholder !== "" ? (_jsx(Text, { role: "label", numberOfLines: 1, style: styles.placeholder, children: placeholder })) : null }), _jsxs(Box, { direction: "row", alignItems: "center", style: styles.actions, children: [selectedTags.length >= 1 && !disabled && (_jsxs(_Fragment, { children: [_jsx(Pressable, { role: "button", onPress: handleClearAll, children: _jsx(Icon, { name: "dismiss-filled", color: colors.gray.primary, size: 15 }) }), _jsx(Space, { width: 8 })] })), _jsx(Icon, { color: colors.gray.primary, name: visible ? "chevron-up-filled" : "chevron-down-filled", size: 20 })] })] }), _jsx(Popover, { role: "listbox", matchReferenceWidth: true, onDismiss: close, referenceRef: inputRef, returnFocus: true, visible: visible, children: _jsx(View, { style: styles.list, children: enableGroups ? (_jsx(SectionList, { role: "listbox", "aria-multiselectable": true, keyExtractor: (item, index) => `group-field-${String(item.value)}-${index}`, ListHeaderComponent: ListHeaderComponent, ListEmptyComponent: ListEmptyComponent, ListFooterComponent: _jsx(Space, { height: 16 }), sections: sections, renderSectionHeader: ({ title, data }) => (_jsxs(Pressable, { role: "listitem", onPress: () => handleSelectGroup(data), style: ({ hovered, pressed, focused }) => [
203
206
  styles.groupTitleBase,
204
207
  (hovered || focused) && { backgroundColor: tint50 },
205
208
  pressed && { backgroundColor: tint100 },
206
- ], children: [_jsx(Text, { numberOfLines: 1, style: styles.groupTitle, children: title }), isNotNullish(renderTagGroup) && (_jsx(Tag, { color: color, children: renderTagGroup(data) }))] })), renderItem: ({ item }) => (_jsx(LineItem, { color: color, filter: filter, item: item, handleSelectItem: handleSelectItem, style: styles.lineInGroup })) })) : (_jsx(FlatList, { role: "list", data: filteredItems, keyExtractor: item => `field-${item.value}`, ListHeaderComponent: ListHeaderComponent, ListEmptyComponent: ListEmptyComponent, ListFooterComponent: _jsx(Space, { height: 8 }), renderItem: ({ item }) => (_jsx(LineItem, { color: color, filter: filter, item: item, handleSelectItem: handleSelectItem })) })) }) }), _jsx(InputError, { message: error })] }));
207
- });
208
- const LineItem = ({ item, color, filter, handleSelectItem, style }) => {
209
+ ], children: [_jsx(Text, { numberOfLines: 1, style: styles.groupTitle, children: title }), isNotNullish(renderTagGroup) && _jsx(Tag, { color: color, children: renderTagGroup(data) })] })), renderItem: ({ item }) => (_jsx(LineItem, { color: color, filter: filter, item: item, handleSelectItem: handleSelectItem, style: styles.lineInGroup })) })) : (_jsx(FlatList, { role: "list", data: filteredItems, keyExtractor: item => `field-${String(item.value)}`, ListHeaderComponent: ListHeaderComponent, ListEmptyComponent: ListEmptyComponent, ListFooterComponent: _jsx(Space, { height: 8 }), renderItem: ({ item }) => (_jsx(LineItem, { color: color, filter: filter, item: item, handleSelectItem: handleSelectItem })) })) }) }), _jsx(InputError, { message: error })] }));
210
+ };
211
+ const LineItem = ({ item, color, filter, handleSelectItem, style, }) => {
209
212
  const { label, disabled = false } = item;
210
213
  const cleanFilter = filter.trim().toLowerCase();
211
214
  const tint50 = colors[color][50];