react-native-platform-components 0.6.1 → 0.8.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/README.md +259 -44
- package/android/src/main/java/com/platformcomponents/PCLiquidGlassView.kt +84 -0
- package/android/src/main/java/com/platformcomponents/PCLiquidGlassViewManager.kt +52 -0
- package/android/src/main/java/com/platformcomponents/PCSegmentedControlView.kt +241 -0
- package/android/src/main/java/com/platformcomponents/PCSegmentedControlViewManager.kt +105 -0
- package/android/src/main/java/com/platformcomponents/PlatformComponentsPackage.kt +2 -0
- package/ios/PCDatePickerView.swift +16 -13
- package/ios/PCLiquidGlass.h +10 -0
- package/ios/PCLiquidGlass.mm +140 -0
- package/ios/PCLiquidGlass.swift +354 -0
- package/ios/PCSegmentedControl.h +10 -0
- package/ios/PCSegmentedControl.mm +194 -0
- package/ios/PCSegmentedControl.swift +200 -0
- package/ios/PCSelectionMenu.swift +1 -1
- package/lib/commonjs/LiquidGlass.js +72 -0
- package/lib/commonjs/LiquidGlass.js.map +1 -0
- package/lib/commonjs/LiquidGlassNativeComponent.ts +110 -0
- package/lib/commonjs/SegmentedControl.js +93 -0
- package/lib/commonjs/SegmentedControl.js.map +1 -0
- package/lib/commonjs/SegmentedControlNativeComponent.ts +79 -0
- package/lib/commonjs/index.js +22 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/LiquidGlass.js +64 -0
- package/lib/module/LiquidGlass.js.map +1 -0
- package/lib/module/LiquidGlassNativeComponent.ts +110 -0
- package/lib/module/SegmentedControl.js +87 -0
- package/lib/module/SegmentedControl.js.map +1 -0
- package/lib/module/SegmentedControlNativeComponent.ts +79 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/commonjs/src/LiquidGlass.d.ts +96 -0
- package/lib/typescript/commonjs/src/LiquidGlass.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/LiquidGlassNativeComponent.d.ts +93 -0
- package/lib/typescript/commonjs/src/LiquidGlassNativeComponent.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/SegmentedControl.d.ts +62 -0
- package/lib/typescript/commonjs/src/SegmentedControl.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/SegmentedControlNativeComponent.d.ts +63 -0
- package/lib/typescript/commonjs/src/SegmentedControlNativeComponent.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +2 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/LiquidGlass.d.ts +96 -0
- package/lib/typescript/module/src/LiquidGlass.d.ts.map +1 -0
- package/lib/typescript/module/src/LiquidGlassNativeComponent.d.ts +93 -0
- package/lib/typescript/module/src/LiquidGlassNativeComponent.d.ts.map +1 -0
- package/lib/typescript/module/src/SegmentedControl.d.ts +62 -0
- package/lib/typescript/module/src/SegmentedControl.d.ts.map +1 -0
- package/lib/typescript/module/src/SegmentedControlNativeComponent.d.ts +63 -0
- package/lib/typescript/module/src/SegmentedControlNativeComponent.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +2 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/package.json +13 -4
- package/react-native.config.js +1 -0
- package/shared/PCSegmentedControlComponentDescriptors-custom.h +22 -0
- package/shared/PCSegmentedControlShadowNode-custom.cpp +54 -0
- package/shared/PCSegmentedControlShadowNode-custom.h +56 -0
- package/shared/PCSegmentedControlState-custom.h +62 -0
- package/shared/react/renderer/components/PlatformComponentsViewSpec/ComponentDescriptors.h +1 -0
- package/src/LiquidGlass.tsx +169 -0
- package/src/LiquidGlassNativeComponent.ts +110 -0
- package/src/SegmentedControl.tsx +178 -0
- package/src/SegmentedControlNativeComponent.ts +79 -0
- package/src/index.tsx +2 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <react/renderer/core/LayoutPrimitives.h>
|
|
4
|
+
#include <memory>
|
|
5
|
+
|
|
6
|
+
#ifdef RN_SERIALIZABLE_STATE
|
|
7
|
+
#include <folly/dynamic.h>
|
|
8
|
+
#include <react/renderer/mapbuffer/MapBuffer.h>
|
|
9
|
+
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
namespace facebook::react {
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Custom state for SegmentedControl that holds the measured frame size from native.
|
|
16
|
+
* This allows the native side to measure the actual segmented control and communicate
|
|
17
|
+
* the size to the shadow node for proper Yoga layout.
|
|
18
|
+
*/
|
|
19
|
+
struct PCSegmentedControlStateFrameSize {
|
|
20
|
+
using Shared = std::shared_ptr<const PCSegmentedControlStateFrameSize>;
|
|
21
|
+
|
|
22
|
+
Size frameSize{}; // {width, height} in points
|
|
23
|
+
|
|
24
|
+
PCSegmentedControlStateFrameSize() = default;
|
|
25
|
+
|
|
26
|
+
explicit PCSegmentedControlStateFrameSize(Size size) : frameSize(size) {}
|
|
27
|
+
|
|
28
|
+
bool operator==(const PCSegmentedControlStateFrameSize& other) const {
|
|
29
|
+
return frameSize.width == other.frameSize.width &&
|
|
30
|
+
frameSize.height == other.frameSize.height;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
bool operator!=(const PCSegmentedControlStateFrameSize& other) const {
|
|
34
|
+
return !(*this == other);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#ifdef RN_SERIALIZABLE_STATE
|
|
38
|
+
// Required for Android state serialization
|
|
39
|
+
PCSegmentedControlStateFrameSize(
|
|
40
|
+
const PCSegmentedControlStateFrameSize& previousState,
|
|
41
|
+
folly::dynamic data)
|
|
42
|
+
: frameSize(previousState.frameSize) {
|
|
43
|
+
// Parse frame size from dynamic data if provided
|
|
44
|
+
if (data.isObject()) {
|
|
45
|
+
if (data.count("width") && data.count("height")) {
|
|
46
|
+
frameSize.width = static_cast<Float>(data["width"].asDouble());
|
|
47
|
+
frameSize.height = static_cast<Float>(data["height"].asDouble());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
folly::dynamic getDynamic() const {
|
|
53
|
+
return folly::dynamic::object("width", frameSize.width)("height", frameSize.height);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
MapBuffer getMapBuffer() const {
|
|
57
|
+
return MapBufferBuilder::EMPTY();
|
|
58
|
+
}
|
|
59
|
+
#endif
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
} // namespace facebook::react
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// LiquidGlass.tsx
|
|
2
|
+
import React, { useCallback, useMemo } from 'react';
|
|
3
|
+
import { Platform, type ViewProps } from 'react-native';
|
|
4
|
+
|
|
5
|
+
import NativeLiquidGlass, {
|
|
6
|
+
type LiquidGlassColorScheme,
|
|
7
|
+
type LiquidGlassEffect,
|
|
8
|
+
type LiquidGlassPressEvent,
|
|
9
|
+
} from './LiquidGlassNativeComponent';
|
|
10
|
+
|
|
11
|
+
export type {
|
|
12
|
+
LiquidGlassEffect,
|
|
13
|
+
LiquidGlassColorScheme,
|
|
14
|
+
LiquidGlassPressEvent,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether the LiquidGlass effect is supported on the current device.
|
|
19
|
+
* Returns true on iOS 26+ (with Liquid Glass support), false otherwise.
|
|
20
|
+
*
|
|
21
|
+
* Use this to conditionally render fallback UI on unsupported devices.
|
|
22
|
+
*/
|
|
23
|
+
export const isLiquidGlassSupported: boolean =
|
|
24
|
+
Platform.OS === 'ios' && Number(Platform.Version) >= 26;
|
|
25
|
+
|
|
26
|
+
export interface LiquidGlassProps extends ViewProps {
|
|
27
|
+
/**
|
|
28
|
+
* Corner radius for the glass effect.
|
|
29
|
+
* Applied uniformly to all corners.
|
|
30
|
+
* @default 0
|
|
31
|
+
*/
|
|
32
|
+
cornerRadius?: number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* iOS-specific props for the glass effect.
|
|
36
|
+
*/
|
|
37
|
+
ios?: {
|
|
38
|
+
/**
|
|
39
|
+
* Glass effect style.
|
|
40
|
+
* - 'clear': More transparent, subtle glass effect
|
|
41
|
+
* - 'regular': Standard blur intensity (default)
|
|
42
|
+
* - 'none': No glass effect (useful for animating materialization)
|
|
43
|
+
* @default 'regular'
|
|
44
|
+
*/
|
|
45
|
+
effect?: LiquidGlassEffect;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Enables native touch interaction effects when pressing the view.
|
|
49
|
+
* When enabled, the glass effect responds to touch location with
|
|
50
|
+
* position-aware visual feedback (iOS 26+ UIGlassEffect.isInteractive).
|
|
51
|
+
*
|
|
52
|
+
* Note: Only applies on component mount; cannot be changed dynamically.
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
interactive?: boolean;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Overlay tint color applied to the glass effect.
|
|
59
|
+
* Accepts any valid color value (hex, rgba, named colors).
|
|
60
|
+
*
|
|
61
|
+
* Example: '#FF0000', 'rgba(255, 0, 0, 0.5)', 'red'
|
|
62
|
+
*/
|
|
63
|
+
tintColor?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Appearance adaptation mode.
|
|
67
|
+
* - 'light': Force light appearance
|
|
68
|
+
* - 'dark': Force dark appearance
|
|
69
|
+
* - 'system': Follow system appearance (default)
|
|
70
|
+
* @default 'system'
|
|
71
|
+
*/
|
|
72
|
+
colorScheme?: LiquidGlassColorScheme;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Shadow radius for the glass effect glow.
|
|
76
|
+
* Higher values create a more diffuse shadow.
|
|
77
|
+
* @default 20
|
|
78
|
+
*/
|
|
79
|
+
shadowRadius?: number;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated Use `interactive` instead for native touch-based highlighting.
|
|
83
|
+
* This prop is a no-op on iOS 26+ where UIGlassEffect handles touch feedback.
|
|
84
|
+
* @default false
|
|
85
|
+
*/
|
|
86
|
+
isHighlighted?: boolean;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Android-specific props.
|
|
91
|
+
* Note: LiquidGlass is an iOS-only effect. On Android, the component
|
|
92
|
+
* renders as a regular View with optional fallback styling.
|
|
93
|
+
*/
|
|
94
|
+
android?: {
|
|
95
|
+
/**
|
|
96
|
+
* Fallback background color for Android since glass effect is not supported.
|
|
97
|
+
* If not provided, the view renders transparently.
|
|
98
|
+
*/
|
|
99
|
+
fallbackBackgroundColor?: string;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Content to render inside the glass effect container.
|
|
104
|
+
*/
|
|
105
|
+
children?: React.ReactNode;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Called when the glass view is pressed.
|
|
109
|
+
* Includes touch coordinates relative to the view bounds.
|
|
110
|
+
*/
|
|
111
|
+
onPress?: (event: { x: number; y: number }) => void;
|
|
112
|
+
|
|
113
|
+
/** Test identifier */
|
|
114
|
+
testID?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function LiquidGlass(props: LiquidGlassProps): React.ReactElement {
|
|
118
|
+
const {
|
|
119
|
+
style,
|
|
120
|
+
cornerRadius = 0,
|
|
121
|
+
ios,
|
|
122
|
+
android,
|
|
123
|
+
children,
|
|
124
|
+
onPress,
|
|
125
|
+
...viewProps
|
|
126
|
+
} = props;
|
|
127
|
+
|
|
128
|
+
// Normalize iOS props for native layer (strings for codegen)
|
|
129
|
+
const nativeIos = useMemo(() => {
|
|
130
|
+
if (Platform.OS !== 'ios' || !ios) return undefined;
|
|
131
|
+
return {
|
|
132
|
+
effect: ios.effect,
|
|
133
|
+
interactive: ios.interactive ? 'true' : 'false',
|
|
134
|
+
tintColor: ios.tintColor,
|
|
135
|
+
colorScheme: ios.colorScheme,
|
|
136
|
+
shadowRadius: ios.shadowRadius,
|
|
137
|
+
isHighlighted: ios.isHighlighted ? 'true' : 'false',
|
|
138
|
+
};
|
|
139
|
+
}, [ios]);
|
|
140
|
+
|
|
141
|
+
// Normalize Android props
|
|
142
|
+
const nativeAndroid = useMemo(() => {
|
|
143
|
+
if (Platform.OS !== 'android' || !android) return undefined;
|
|
144
|
+
return {
|
|
145
|
+
fallbackBackgroundColor: android.fallbackBackgroundColor,
|
|
146
|
+
};
|
|
147
|
+
}, [android]);
|
|
148
|
+
|
|
149
|
+
// Handle native press event
|
|
150
|
+
const handlePress = useCallback(
|
|
151
|
+
(event: { nativeEvent: { x: number; y: number } }) => {
|
|
152
|
+
onPress?.({ x: event.nativeEvent.x, y: event.nativeEvent.y });
|
|
153
|
+
},
|
|
154
|
+
[onPress]
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<NativeLiquidGlass
|
|
159
|
+
style={style}
|
|
160
|
+
cornerRadius={cornerRadius}
|
|
161
|
+
ios={nativeIos}
|
|
162
|
+
android={nativeAndroid}
|
|
163
|
+
onGlassPress={onPress ? handlePress : undefined}
|
|
164
|
+
{...viewProps}
|
|
165
|
+
>
|
|
166
|
+
{children}
|
|
167
|
+
</NativeLiquidGlass>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// LiquidGlassNativeComponent.ts
|
|
2
|
+
import type { CodegenTypes, HostComponent, ViewProps } from 'react-native';
|
|
3
|
+
import { codegenNativeComponent } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Glass effect intensity/style.
|
|
7
|
+
* - 'clear': More transparent, subtle glass effect
|
|
8
|
+
* - 'regular': Standard blur intensity (default)
|
|
9
|
+
* - 'none': No glass effect (useful for animating materialization)
|
|
10
|
+
*/
|
|
11
|
+
export type LiquidGlassEffect = 'clear' | 'regular' | 'none';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Color scheme for the glass effect.
|
|
15
|
+
* - 'light': Force light appearance
|
|
16
|
+
* - 'dark': Force dark appearance
|
|
17
|
+
* - 'system': Follow system appearance (default)
|
|
18
|
+
*/
|
|
19
|
+
export type LiquidGlassColorScheme = 'light' | 'dark' | 'system';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* iOS-specific configuration.
|
|
23
|
+
*/
|
|
24
|
+
export type LiquidGlassIOSProps = Readonly<{
|
|
25
|
+
/**
|
|
26
|
+
* Glass effect style.
|
|
27
|
+
* @default 'regular'
|
|
28
|
+
*/
|
|
29
|
+
effect?: string; // LiquidGlassEffect
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Enables touch interaction effects when pressing the view.
|
|
33
|
+
* Note: Only applies on component mount; cannot be changed dynamically.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
interactive?: string; // 'true' | 'false'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Overlay tint color applied to the glass effect.
|
|
40
|
+
* Accepts hex color strings (e.g., '#FF0000', '#FF000080').
|
|
41
|
+
*/
|
|
42
|
+
tintColor?: string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Appearance adaptation mode.
|
|
46
|
+
* @default 'system'
|
|
47
|
+
*/
|
|
48
|
+
colorScheme?: string; // LiquidGlassColorScheme
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Shadow radius for the glass effect.
|
|
52
|
+
* @default 20
|
|
53
|
+
*/
|
|
54
|
+
shadowRadius?: CodegenTypes.Float;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether the view is highlighted (pressed state).
|
|
58
|
+
*/
|
|
59
|
+
isHighlighted?: string; // 'true' | 'false'
|
|
60
|
+
}>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Android-specific configuration (stub - LiquidGlass is iOS only).
|
|
64
|
+
*/
|
|
65
|
+
export type LiquidGlassAndroidProps = Readonly<{
|
|
66
|
+
/**
|
|
67
|
+
* Fallback background color for Android (since glass effect is not supported).
|
|
68
|
+
* If not provided, renders as transparent.
|
|
69
|
+
*/
|
|
70
|
+
fallbackBackgroundColor?: string;
|
|
71
|
+
}>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Event emitted when the glass view is pressed.
|
|
75
|
+
*/
|
|
76
|
+
export type LiquidGlassPressEvent = Readonly<{
|
|
77
|
+
/** X coordinate of touch relative to view bounds */
|
|
78
|
+
x: CodegenTypes.Float;
|
|
79
|
+
/** Y coordinate of touch relative to view bounds */
|
|
80
|
+
y: CodegenTypes.Float;
|
|
81
|
+
}>;
|
|
82
|
+
|
|
83
|
+
export interface LiquidGlassNativeProps extends ViewProps {
|
|
84
|
+
/**
|
|
85
|
+
* Corner radius for the glass effect.
|
|
86
|
+
* Applied uniformly to all corners.
|
|
87
|
+
* @default 0
|
|
88
|
+
*/
|
|
89
|
+
cornerRadius?: CodegenTypes.WithDefault<CodegenTypes.Float, 0>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* iOS-specific props.
|
|
93
|
+
*/
|
|
94
|
+
ios?: LiquidGlassIOSProps;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Android-specific props.
|
|
98
|
+
*/
|
|
99
|
+
android?: LiquidGlassAndroidProps;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Fired when the glass view is pressed.
|
|
103
|
+
* Includes touch coordinates relative to view bounds.
|
|
104
|
+
*/
|
|
105
|
+
onGlassPress?: CodegenTypes.DirectEventHandler<LiquidGlassPressEvent>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default codegenNativeComponent<LiquidGlassNativeProps>(
|
|
109
|
+
'PCLiquidGlass'
|
|
110
|
+
) as HostComponent<LiquidGlassNativeProps>;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// SegmentedControl.tsx
|
|
2
|
+
import React, { useCallback, useMemo } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
Platform,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
type StyleProp,
|
|
7
|
+
type ViewProps,
|
|
8
|
+
type ViewStyle,
|
|
9
|
+
} from 'react-native';
|
|
10
|
+
|
|
11
|
+
import NativeSegmentedControl, {
|
|
12
|
+
type SegmentedControlSelectEvent,
|
|
13
|
+
} from './SegmentedControlNativeComponent';
|
|
14
|
+
|
|
15
|
+
// Android: Minimum height to ensure visibility.
|
|
16
|
+
// Fabric's shadow node measurement isn't being called on initial render,
|
|
17
|
+
// so we apply a minHeight that matches Material design touch target guidelines.
|
|
18
|
+
const ANDROID_MIN_HEIGHT = 48;
|
|
19
|
+
|
|
20
|
+
export interface SegmentedControlSegmentProps {
|
|
21
|
+
/** Display label for the segment */
|
|
22
|
+
label: string;
|
|
23
|
+
|
|
24
|
+
/** Unique value identifier for the segment */
|
|
25
|
+
value: string;
|
|
26
|
+
|
|
27
|
+
/** Whether this specific segment is disabled */
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
|
|
30
|
+
/** Optional SF Symbol name (iOS) or drawable resource name (Android) */
|
|
31
|
+
icon?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SegmentedControlProps extends ViewProps {
|
|
35
|
+
/** Array of segments to display */
|
|
36
|
+
segments: readonly SegmentedControlSegmentProps[];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Currently selected segment value.
|
|
40
|
+
* Use `null` for no selection.
|
|
41
|
+
*/
|
|
42
|
+
selectedValue: string | null;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Called when the user selects a segment.
|
|
46
|
+
* @param value - The selected segment's value
|
|
47
|
+
* @param index - The selected segment's index
|
|
48
|
+
*/
|
|
49
|
+
onSelect?: (value: string, index: number) => void;
|
|
50
|
+
|
|
51
|
+
/** Whether the entire control is disabled */
|
|
52
|
+
disabled?: boolean;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* iOS-specific configuration
|
|
56
|
+
*/
|
|
57
|
+
ios?: {
|
|
58
|
+
/**
|
|
59
|
+
* Momentary mode: segment springs back after touch (no persistent selection)
|
|
60
|
+
* Default: false
|
|
61
|
+
*/
|
|
62
|
+
momentary?: boolean;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether segment widths are proportional to content
|
|
66
|
+
* Default: false (equal widths)
|
|
67
|
+
*/
|
|
68
|
+
apportionsSegmentWidthsByContent?: boolean;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Selected segment tint color (hex string, e.g., "#007AFF")
|
|
72
|
+
*/
|
|
73
|
+
selectedSegmentTintColor?: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Android-specific configuration
|
|
78
|
+
*/
|
|
79
|
+
android?: {
|
|
80
|
+
/**
|
|
81
|
+
* Whether one segment must always be selected.
|
|
82
|
+
* Default: false
|
|
83
|
+
*/
|
|
84
|
+
selectionRequired?: boolean;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** Test identifier */
|
|
88
|
+
testID?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function normalizeSelectedValue(selected: string | null): string {
|
|
92
|
+
return selected ?? '';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function SegmentedControl(
|
|
96
|
+
props: SegmentedControlProps
|
|
97
|
+
): React.ReactElement {
|
|
98
|
+
const {
|
|
99
|
+
style,
|
|
100
|
+
segments,
|
|
101
|
+
selectedValue,
|
|
102
|
+
disabled,
|
|
103
|
+
onSelect,
|
|
104
|
+
ios,
|
|
105
|
+
android,
|
|
106
|
+
...viewProps
|
|
107
|
+
} = props;
|
|
108
|
+
|
|
109
|
+
// Normalize segments for native
|
|
110
|
+
const nativeSegments = useMemo(() => {
|
|
111
|
+
return segments.map((seg) => ({
|
|
112
|
+
label: seg.label,
|
|
113
|
+
value: seg.value,
|
|
114
|
+
disabled: seg.disabled ? 'disabled' : 'enabled',
|
|
115
|
+
icon: seg.icon ?? '',
|
|
116
|
+
}));
|
|
117
|
+
}, [segments]);
|
|
118
|
+
|
|
119
|
+
const selectedData = useMemo(
|
|
120
|
+
() => normalizeSelectedValue(selectedValue),
|
|
121
|
+
[selectedValue]
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const handleSelect = useCallback(
|
|
125
|
+
(e: { nativeEvent: SegmentedControlSelectEvent }) => {
|
|
126
|
+
const { index, value } = e.nativeEvent;
|
|
127
|
+
onSelect?.(value, index);
|
|
128
|
+
},
|
|
129
|
+
[onSelect]
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
// Normalize iOS props to native string format
|
|
133
|
+
const nativeIos = useMemo(() => {
|
|
134
|
+
if (!ios) return undefined;
|
|
135
|
+
return {
|
|
136
|
+
momentary: ios.momentary ? 'true' : 'false',
|
|
137
|
+
apportionsSegmentWidthsByContent: ios.apportionsSegmentWidthsByContent
|
|
138
|
+
? 'true'
|
|
139
|
+
: 'false',
|
|
140
|
+
selectedSegmentTintColor: ios.selectedSegmentTintColor ?? '',
|
|
141
|
+
};
|
|
142
|
+
}, [ios]);
|
|
143
|
+
|
|
144
|
+
// Normalize Android props
|
|
145
|
+
const nativeAndroid = useMemo(() => {
|
|
146
|
+
if (!android) return undefined;
|
|
147
|
+
return {
|
|
148
|
+
selectionRequired: android.selectionRequired ? 'true' : 'false',
|
|
149
|
+
};
|
|
150
|
+
}, [android]);
|
|
151
|
+
|
|
152
|
+
// Merge user style with Android minHeight default
|
|
153
|
+
const mergedStyle = useMemo((): StyleProp<ViewStyle> => {
|
|
154
|
+
if (Platform.OS === 'android') {
|
|
155
|
+
return [styles.androidDefault, style];
|
|
156
|
+
}
|
|
157
|
+
return style;
|
|
158
|
+
}, [style]);
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<NativeSegmentedControl
|
|
162
|
+
style={mergedStyle}
|
|
163
|
+
segments={nativeSegments}
|
|
164
|
+
selectedValue={selectedData}
|
|
165
|
+
interactivity={disabled ? 'disabled' : 'enabled'}
|
|
166
|
+
onSelect={onSelect ? handleSelect : undefined}
|
|
167
|
+
ios={nativeIos}
|
|
168
|
+
android={nativeAndroid}
|
|
169
|
+
{...viewProps}
|
|
170
|
+
/>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const styles = StyleSheet.create({
|
|
175
|
+
androidDefault: {
|
|
176
|
+
minHeight: ANDROID_MIN_HEIGHT,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SegmentedControlNativeComponent.ts
|
|
2
|
+
import type { CodegenTypes, ViewProps } from 'react-native';
|
|
3
|
+
import { codegenNativeComponent } from 'react-native';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A single segment in the control.
|
|
7
|
+
*/
|
|
8
|
+
export type SegmentedControlSegment = Readonly<{
|
|
9
|
+
label: string;
|
|
10
|
+
value: string;
|
|
11
|
+
disabled: string; // 'enabled' | 'disabled'
|
|
12
|
+
icon: string; // SF Symbol (iOS) or drawable name (Android), empty = none
|
|
13
|
+
}>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Event emitted when the user selects a segment.
|
|
17
|
+
*/
|
|
18
|
+
export type SegmentedControlSelectEvent = Readonly<{
|
|
19
|
+
/** Selected segment index */
|
|
20
|
+
index: CodegenTypes.Int32;
|
|
21
|
+
|
|
22
|
+
/** Selected segment value */
|
|
23
|
+
value: string;
|
|
24
|
+
}>;
|
|
25
|
+
|
|
26
|
+
/** Interactivity state (no booleans). */
|
|
27
|
+
export type SegmentedControlInteractivity = 'enabled' | 'disabled';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* iOS-specific configuration.
|
|
31
|
+
*/
|
|
32
|
+
export type IOSProps = Readonly<{
|
|
33
|
+
/** Momentary mode: segment springs back after touch */
|
|
34
|
+
momentary?: string; // 'true' | 'false'
|
|
35
|
+
|
|
36
|
+
/** Whether segment widths are proportional to content */
|
|
37
|
+
apportionsSegmentWidthsByContent?: string; // 'true' | 'false'
|
|
38
|
+
|
|
39
|
+
/** Selected segment tint color (hex string) */
|
|
40
|
+
selectedSegmentTintColor?: string;
|
|
41
|
+
}>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Android-specific configuration.
|
|
45
|
+
*/
|
|
46
|
+
export type AndroidProps = Readonly<{
|
|
47
|
+
/** Whether one segment must always be selected */
|
|
48
|
+
selectionRequired?: string; // 'true' | 'false'
|
|
49
|
+
}>;
|
|
50
|
+
|
|
51
|
+
export interface SegmentedControlProps extends ViewProps {
|
|
52
|
+
/**
|
|
53
|
+
* Segments to display.
|
|
54
|
+
*/
|
|
55
|
+
segments: ReadonlyArray<SegmentedControlSegment>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Controlled selection by `value`.
|
|
59
|
+
* Empty string means "no selection".
|
|
60
|
+
*/
|
|
61
|
+
selectedValue?: CodegenTypes.WithDefault<string, ''>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Enabled / disabled state.
|
|
65
|
+
*/
|
|
66
|
+
interactivity?: string; // SegmentedControlInteractivity
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Fired when the user selects a segment.
|
|
70
|
+
*/
|
|
71
|
+
onSelect?: CodegenTypes.BubblingEventHandler<SegmentedControlSelectEvent>;
|
|
72
|
+
|
|
73
|
+
ios?: IOSProps;
|
|
74
|
+
android?: AndroidProps;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default codegenNativeComponent<SegmentedControlProps>(
|
|
78
|
+
'PCSegmentedControl'
|
|
79
|
+
);
|