Skip to content

Commit

Permalink
Fall back to JS SegmentedControl for icons
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 committed Dec 25, 2022
1 parent f703a95 commit 7eb025c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
24 changes: 15 additions & 9 deletions DirectPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import SegmentedControl, {
import type {
NativeSegmentedControlIOSChangeEvent,
SegmentedControlProps,
} from "@react-native-segmented-control/segmented-control";
import SegmentedControl from "@react-native-segmented-control/segmented-control";
// TODO: Use the native SegmentedControl for icons when iOS image resizing is fixed
import SegmentedControlWithIcons from "@react-native-segmented-control/segmented-control/js/SegmentedControl.js";
import { useCallback, useMemo } from "react";
import { NativeSyntheticEvent } from "react-native";

Expand Down Expand Up @@ -30,13 +33,14 @@ export function DirectPicker<T extends string>({
}),
[values]
);
const labels = useMemo(
() =>
normalizedValues.map(
(item) => (item.icon as string) /* TODO: this is a lie */ ?? item.value
),
[normalizedValues]
);
const [labels, hasIcons] = useMemo(() => {
let hasIcons;
const labels = normalizedValues.map((item) => {
hasIcons ||= item.icon != null;
return (item.icon as string) /* TODO: this is a lie */ ?? item.value;
});
return [labels, hasIcons];
}, [normalizedValues]);
const selectedIndex = useMemo(() => {
return normalizedValues.findIndex((item) => item.value === value) ?? -1;
}, [value, normalizedValues]);
Expand All @@ -49,8 +53,10 @@ export function DirectPicker<T extends string>({
[normalizedValues, onValueChange]
);

const Component = hasIcons ? SegmentedControlWithIcons : SegmentedControl;

return (
<SegmentedControl
<Component
// TODO: Make `values` readonly upstream
values={labels}
selectedIndex={selectedIndex}
Expand Down
5 changes: 5 additions & 0 deletions segmented-control-js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Hack to expose the pure JS version of SegmentedControl with types
declare module "@react-native-segmented-control/segmented-control/js/SegmentedControl.js" {
import SegmentedControl from "@react-native-segmented-control/segmented-control";
export default SegmentedControl;
}

0 comments on commit 7eb025c

Please sign in to comment.