Skip to content

Commit 0634a60

Browse files
committed
refactor(ios): use synthetic data for events
1 parent 726c684 commit 0634a60

File tree

7 files changed

+60
-43
lines changed

7 files changed

+60
-43
lines changed

example/src/components/sheets/BasicSheet.tsx

+22-15
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,30 @@ export const BasicSheet = forwardRef((props: BasicSheetProps, ref: Ref<TrueSheet
5050
cornerRadius={12}
5151
edgeToEdge
5252
grabberProps={{ color: GRABBER_COLOR }}
53-
onDragChange={(e) => {
54-
const { index, value } = e.nativeEvent
55-
console.log(`drag changed with size of ${value} at index: ${index}`)
56-
}}
57-
onDragBegin={(e) => {
58-
const { index, value } = e.nativeEvent
59-
console.log(`drag began with size of ${value} at index: ${index}`)
60-
}}
61-
onDragEnd={(e) => {
62-
const { index, value } = e.nativeEvent
63-
console.log(`drag ended with size of ${value} at index: ${index}`)
64-
}}
53+
onDragChange={(e) =>
54+
console.log(
55+
`drag changed with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
56+
)
57+
}
58+
onDragBegin={(e) =>
59+
console.log(
60+
`drag began with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
61+
)
62+
}
63+
onDragEnd={(e) =>
64+
console.log(
65+
`drag ended with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
66+
)
67+
}
6568
onDismiss={() => console.log('Basic sheet dismissed!')}
66-
onPresent={({ index, value }) =>
67-
console.log(`Basic sheet presented with size of ${value} at index: ${index}`)
69+
onPresent={(e) =>
70+
console.log(
71+
`Basic sheet presented with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
72+
)
73+
}
74+
onSizeChange={(e) =>
75+
console.log(`Resized to:`, e.nativeEvent.value, 'at index:', e.nativeEvent.index)
6876
}
69-
onSizeChange={({ index, value }) => console.log(`Resized to:`, value, 'at index:', index)}
7077
FooterComponent={<Footer />}
7178
{...props}
7279
>

example/src/components/sheets/GestureSheet.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ export const GestureSheet = forwardRef((props: GestureSheetProps, ref: Ref<TrueS
5656
cornerRadius={12}
5757
grabberProps={{ color: GRABBER_COLOR }}
5858
onDismiss={() => console.log('Gesture sheet dismissed!')}
59-
onPresent={({ index, value }) =>
60-
console.log(`Gesture sheet presented with size of ${value} at index: ${index}`)
59+
onPresent={(e) =>
60+
console.log(
61+
`Gesture sheet presented with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
62+
)
63+
}
64+
onSizeChange={(e) =>
65+
console.log(`Resized to:`, e.nativeEvent.value, 'at index:', e.nativeEvent.index)
6166
}
62-
onSizeChange={({ index, value }) => console.log(`Resized to:`, value, 'at index:', index)}
6367
FooterComponent={<Footer />}
6468
{...props}
6569
>

example/src/components/sheets/PromptSheet.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ export const PromptSheet = forwardRef((props: PromptSheetProps, ref: Ref<TrueShe
4545
cornerRadius={12}
4646
grabberProps={{ color: GRABBER_COLOR }}
4747
onDismiss={handleDismiss}
48-
onPresent={({ index, value }) =>
49-
console.log(`Sheet prompt presented with size of ${value} at index: ${index}`)
48+
onPresent={(e) =>
49+
console.log(
50+
`Sheet prompt presented with size of ${e.nativeEvent.value} at index: ${e.nativeEvent.index}`
51+
)
52+
}
53+
onSizeChange={(e) =>
54+
console.log(`Resized to:`, e.nativeEvent.value, 'at index:', e.nativeEvent.index)
5055
}
51-
onSizeChange={({ index, value }) => console.log(`Resized to:`, value, 'at index:', index)}
5256
FooterComponent={<Footer />}
5357
{...props}
5458
>

example/src/screens/MapScreen.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const MapScreen = () => {
9595
dismissible={false}
9696
cornerRadius={12}
9797
initialIndex={1}
98-
onPresent={handlePresent}
98+
onPresent={(e) => handlePresent(e.nativeEvent)}
9999
onDragChange={dragChangeHandler}
100100
onDragEnd={(e) => handleDragEnd(e.nativeEvent)}
101101
// initialIndexAnimated={false}

ios/TrueSheetView.swift

+12-9
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
146146
present(at: initialIndex, promise: nil, animated: initialIndexAnimated)
147147
}
148148

149-
onMount?(nil)
149+
dispatchEvent(name: "onMount", data: nil)
150150
}
151151
}
152152

@@ -174,19 +174,19 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
174174

175175
func viewControllerDidChangeWidth(_ width: CGFloat) {
176176
// We only pass width to JS since height is handled by the constraints
177-
onContainerSizeChange?(["width": width])
177+
dispatchEvent(name: "onContainerSizeChange", data: ["width": width])
178178
}
179179

180180
func viewControllerDidDrag(_ state: UIGestureRecognizer.State, _ height: CGFloat) {
181181
let sizeInfo = SizeInfo(index: activeIndex ?? 0, value: height)
182182

183183
switch state {
184184
case .began:
185-
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: "onDragBegin", data: sizeInfoData(from: sizeInfo)))
185+
dispatchEvent(name: "onDragBegin", data: sizeInfoData(from: sizeInfo))
186186
case .changed:
187-
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: "onDragChange", data: sizeInfoData(from: sizeInfo)))
187+
dispatchEvent(name: "onDragChange", data: sizeInfoData(from: sizeInfo))
188188
case .ended, .cancelled:
189-
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: "onDragEnd", data: sizeInfoData(from: sizeInfo)))
189+
dispatchEvent(name: "onDragEnd", data: sizeInfoData(from: sizeInfo))
190190
default:
191191
Logger.info("Drag state is not supported")
192192
}
@@ -199,16 +199,15 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
199199
func viewControllerDidDismiss() {
200200
isPresented = false
201201
activeIndex = nil
202-
203-
onDismiss?(nil)
202+
dispatchEvent(name: "onDismiss", data: nil)
204203
}
205204

206205
func viewControllerDidChangeSize(_ sizeInfo: SizeInfo?) {
207206
guard let sizeInfo else { return }
208207

209208
if sizeInfo.index != activeIndex {
210209
activeIndex = sizeInfo.index
211-
onSizeChange?(sizeInfoData(from: sizeInfo))
210+
dispatchEvent(name: "onSizeChange", data: sizeInfoData(from: sizeInfo))
212211
}
213212
}
214213

@@ -389,6 +388,10 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
389388
}
390389
}
391390

391+
func dispatchEvent(name: String, data: [String: Any]?) {
392+
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: name, data: data))
393+
}
394+
392395
func dismiss(promise: Promise) {
393396
guard isPresented else {
394397
promise.resolve(nil)
@@ -429,7 +432,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
429432
}
430433

431434
let data = self.sizeInfoData(from: self.viewController.currentSizeInfo)
432-
self.onPresent?(data)
435+
self.dispatchEvent(name: "onPresent", data: data)
433436
promise?.resolve(nil)
434437
}
435438
}

src/TrueSheet.tsx

+7-10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import {
1414

1515
import type {
1616
TrueSheetProps,
17-
SizeInfo,
1817
DragBeginEvent,
1918
DragChangeEvent,
2019
DragEndEvent,
20+
SizeChangeEvent,
21+
PresentEvent,
2122
} from './TrueSheet.types'
2223
import { TrueSheetModule } from './TrueSheetModule'
2324
import { TrueSheetGrabber } from './TrueSheetGrabber'
@@ -30,17 +31,13 @@ const LINKING_ERROR =
3031
'- You rebuilt the app after installing the package\n' +
3132
'- You are not using Expo Go\n'
3233

33-
type ContainerSizeChangeEvent = NativeSyntheticEvent<{ width: number; height: number }>
34-
type SizeChangeEvent = NativeSyntheticEvent<SizeInfo>
34+
export type ContainerSizeChangeEvent = NativeSyntheticEvent<{ width: number; height: number }>
3535

36-
interface TrueSheetNativeViewProps
37-
extends Omit<TrueSheetProps, 'onPresent' | 'onSizeChange' | 'backgroundColor'> {
36+
interface TrueSheetNativeViewProps extends Omit<TrueSheetProps, 'backgroundColor'> {
3837
contentHeight?: number
3938
footerHeight?: number
4039
background?: ProcessedColorValue | null
4140
scrollableHandle: number | null
42-
onPresent: (event: SizeChangeEvent) => void
43-
onSizeChange: (event: SizeChangeEvent) => void
4441
onContainerSizeChange: (event: ContainerSizeChangeEvent) => void
4542
}
4643

@@ -159,7 +156,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
159156
}
160157

161158
private onSizeChange(event: SizeChangeEvent): void {
162-
this.props.onSizeChange?.(event.nativeEvent)
159+
this.props.onSizeChange?.(event)
163160
}
164161

165162
private onContainerSizeChange(event: ContainerSizeChangeEvent): void {
@@ -169,8 +166,8 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
169166
})
170167
}
171168

172-
private onPresent(event: SizeChangeEvent): void {
173-
this.props.onPresent?.(event.nativeEvent)
169+
private onPresent(event: PresentEvent): void {
170+
this.props.onPresent?.(event)
174171
}
175172

176173
private onFooterLayout(event: LayoutChangeEvent): void {

src/TrueSheet.types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface SizeInfo {
1414
value: number
1515
}
1616

17+
export type SizeChangeEvent = NativeSyntheticEvent<SizeInfo>
18+
export type PresentEvent = NativeSyntheticEvent<SizeInfo>
1719
export type DragBeginEvent = NativeSyntheticEvent<SizeInfo>
1820
export type DragChangeEvent = NativeSyntheticEvent<SizeInfo>
1921
export type DragEndEvent = NativeSyntheticEvent<SizeInfo>
@@ -257,7 +259,7 @@ export interface TrueSheetProps extends ViewProps {
257259
* Called when the Sheet has been presented.
258260
* Comes with the size info.
259261
*/
260-
onPresent?: (info: SizeInfo) => void
262+
onPresent?: (event: PresentEvent) => void
261263

262264
/**
263265
* Called when the Sheet has been dismissed
@@ -268,7 +270,7 @@ export interface TrueSheetProps extends ViewProps {
268270
* Called when the size of the sheet has changed.
269271
* Either by dragging or programatically.
270272
*/
271-
onSizeChange?: (info: SizeInfo) => void
273+
onSizeChange?: (event: SizeChangeEvent) => void
272274

273275
/**
274276
* Called when the sheet has began dragging.

0 commit comments

Comments
 (0)