Skip to content

Commit fda0600

Browse files
authored
refactor: minor changes to types (vonovak#72)
1 parent c6e4bcc commit fda0600

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

src/NativeSimpleToast.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ export interface Spec extends TurboModule {
1616
BOTTOM: number;
1717
CENTER: number;
1818
};
19-
show: (message: string, duration: number, options: Object) => void;
19+
show: (message: string, duration: number, options?: Object) => void;
2020
showWithGravity: (
2121
message: string,
2222
duration: number,
2323
gravity: number,
24-
options: Object,
24+
options?: Object,
2525
) => void;
2626
showWithGravityAndOffset: (
2727
message: string,
2828
duration: number,
2929
gravity: number,
3030
xOffset: number,
3131
yOffset: number,
32-
options: Object,
32+
options?: Object,
3333
) => void;
3434
}
3535

src/index.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Platform, processColor } from 'react-native';
22
import type { Spec, StylesIOS } from './NativeSimpleToast';
33

4+
const unsupportedPlatform = 'RNSimpleToast: unsupported platform';
5+
46
const RCTToast = Platform.select<() => Spec>({
57
ios: () => require('./NativeSimpleToast').default,
68
android: () => require('react-native').ToastAndroid,
79
default: () => {
8-
throw new Error('RNSimpleToast: unsupported platform');
10+
throw new Error(unsupportedPlatform);
911
},
1012
})();
1113

@@ -21,7 +23,7 @@ const constantsSource = Platform.select<
2123
ios: () => require('./NativeSimpleToast').default.getConstants(),
2224
android: () => require('react-native').ToastAndroid,
2325
default: () => {
24-
throw new Error('RNSimpleToast: unsupported platform');
26+
throw new Error(unsupportedPlatform);
2527
},
2628
})();
2729

@@ -33,23 +35,23 @@ export default {
3335
BOTTOM: constantsSource.BOTTOM,
3436
CENTER: constantsSource.CENTER,
3537

36-
show(message: string, durationSeconds: number, options: StylesIOS = {}) {
38+
show(message: string, duration: number, options?: StylesIOS) {
3739
RCTToast.show(
3840
message,
39-
durationSeconds ?? constantsSource.SHORT,
41+
duration ?? constantsSource.SHORT,
4042
processColors(options),
4143
);
4244
},
4345

4446
showWithGravity(
4547
message: string,
46-
durationSeconds: number,
48+
duration: number,
4749
gravity: number,
48-
options: StylesIOS = {},
50+
options?: StylesIOS,
4951
) {
5052
RCTToast.showWithGravity(
5153
message,
52-
durationSeconds ?? constantsSource.SHORT,
54+
duration ?? constantsSource.SHORT,
5355
gravity,
5456
processColors(options),
5557
);
@@ -61,11 +63,11 @@ export default {
6163
gravity: number,
6264
xOffset: number,
6365
yOffset: number,
64-
options: StylesIOS = {},
66+
options?: StylesIOS,
6567
) {
6668
RCTToast.showWithGravityAndOffset(
6769
message,
68-
duration,
70+
duration ?? constantsSource.SHORT,
6971
gravity,
7072
xOffset,
7173
yOffset,
@@ -74,17 +76,16 @@ export default {
7476
},
7577
};
7678

77-
function processColors(options: StylesIOS) {
78-
if (Platform.OS === 'ios') {
79-
return {
80-
// the types are not 100% correct
81-
...options,
82-
messageColor: processColor(options.textColor) as number | undefined,
83-
backgroundColor: processColor(options.backgroundColor) as
84-
| number
85-
| undefined,
86-
};
79+
function processColors(options?: StylesIOS) {
80+
if (Platform.OS === 'android' || !options) {
81+
return undefined;
8782
}
88-
89-
return {};
83+
return {
84+
// the types are not 100% correct
85+
...options,
86+
messageColor: processColor(options.textColor) as number | undefined,
87+
backgroundColor: processColor(options.backgroundColor) as
88+
| number
89+
| undefined,
90+
};
9091
}

0 commit comments

Comments
 (0)