@momo-kits/foundation 0.152.1-beta.4 → 0.152.1-beta.6
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/Button/index.tsx +75 -56
- package/Button/styles.ts +4 -1
- package/package.json +1 -1
package/Button/index.tsx
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
TouchableOpacity,
|
|
6
6
|
TouchableOpacityProps,
|
|
7
7
|
View,
|
|
8
|
+
ViewStyle,
|
|
9
|
+
GestureResponderEvent,
|
|
8
10
|
} from 'react-native';
|
|
9
11
|
import LinearGradient from 'react-native-linear-gradient';
|
|
10
12
|
import { useComponentId } from '../Application';
|
|
@@ -22,7 +24,6 @@ import { Skeleton } from '../Skeleton';
|
|
|
22
24
|
import LottieView from 'lottie-react-native';
|
|
23
25
|
|
|
24
26
|
const AnimationLinear = Animated.createAnimatedComponent(LinearGradient);
|
|
25
|
-
const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);
|
|
26
27
|
|
|
27
28
|
export interface ButtonProps extends TouchableOpacityProps {
|
|
28
29
|
/**
|
|
@@ -51,16 +52,6 @@ export interface ButtonProps extends TouchableOpacityProps {
|
|
|
51
52
|
params?: any;
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
const shouldLoadingOnLeft = (iconLeft?: string, iconRight?: string): boolean => {
|
|
55
|
-
const hasLeft = !!iconLeft;
|
|
56
|
-
const hasRight = !!iconRight;
|
|
57
|
-
|
|
58
|
-
if (!hasLeft && !hasRight) return true;
|
|
59
|
-
if (hasLeft && !hasRight) return true;
|
|
60
|
-
if (!hasLeft && hasRight) return false;
|
|
61
|
-
return false;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
55
|
const Button: FC<ButtonProps> = ({
|
|
65
56
|
type = 'primary',
|
|
66
57
|
size = 'large',
|
|
@@ -76,21 +67,20 @@ const Button: FC<ButtonProps> = ({
|
|
|
76
67
|
}) => {
|
|
77
68
|
const { theme, config } = useContext(ApplicationContext);
|
|
78
69
|
const skeleton = useContext(SkeletonContext);
|
|
79
|
-
const componentName = 'Button';
|
|
80
70
|
const animationRef = useRef<LottieView>(null);
|
|
71
|
+
const pressAnim = useRef(new Animated.Value(0)).current;
|
|
81
72
|
|
|
73
|
+
const componentName = 'Button';
|
|
82
74
|
const { componentId } = useComponentId(
|
|
83
75
|
`${componentName}/${title}`,
|
|
84
76
|
rest.accessibilityLabel,
|
|
85
77
|
);
|
|
86
78
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
const animateIn = () => {
|
|
93
|
-
if (isDisabled) return;
|
|
79
|
+
/**
|
|
80
|
+
* animate press in
|
|
81
|
+
*/
|
|
82
|
+
const animateIn = (event: GestureResponderEvent) => {
|
|
83
|
+
rest?.onPressIn?.(event);
|
|
94
84
|
Animated.timing(pressAnim, {
|
|
95
85
|
toValue: 1,
|
|
96
86
|
duration: 100,
|
|
@@ -98,7 +88,11 @@ const Button: FC<ButtonProps> = ({
|
|
|
98
88
|
}).start();
|
|
99
89
|
};
|
|
100
90
|
|
|
101
|
-
|
|
91
|
+
/**
|
|
92
|
+
* animate press out
|
|
93
|
+
*/
|
|
94
|
+
const animateOut = (event: GestureResponderEvent) => {
|
|
95
|
+
rest?.onPressIn?.(event);
|
|
102
96
|
Animated.timing(pressAnim, {
|
|
103
97
|
toValue: 0,
|
|
104
98
|
duration: 100,
|
|
@@ -106,22 +100,38 @@ const Button: FC<ButtonProps> = ({
|
|
|
106
100
|
}).start();
|
|
107
101
|
};
|
|
108
102
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
103
|
+
/**
|
|
104
|
+
* determine loading position
|
|
105
|
+
* @param left
|
|
106
|
+
* @param right
|
|
107
|
+
* @param isLeft
|
|
108
|
+
*/
|
|
109
|
+
const shouldShowLoading = (
|
|
110
|
+
left?: string,
|
|
111
|
+
right?: string,
|
|
112
|
+
isLeft?: boolean,
|
|
113
|
+
): boolean => {
|
|
114
|
+
const hasLeft = !!left;
|
|
115
|
+
const hasRight = !!right;
|
|
116
|
+
|
|
117
|
+
const loadingOnLeft =
|
|
118
|
+
(!hasLeft && !hasRight) ||
|
|
119
|
+
(hasLeft && !hasRight);
|
|
120
|
+
|
|
121
|
+
return isLeft === loadingOnLeft;
|
|
122
|
+
};
|
|
119
123
|
|
|
124
|
+
/**
|
|
125
|
+
* export size style
|
|
126
|
+
*/
|
|
120
127
|
const getSizeStyle = () => {
|
|
121
128
|
const styleSheet: { [key: string]: any } = styles;
|
|
122
129
|
return styleSheet[size ?? 'small'];
|
|
123
130
|
};
|
|
124
131
|
|
|
132
|
+
/**
|
|
133
|
+
* export type style
|
|
134
|
+
*/
|
|
125
135
|
const getTypeStyle = () => {
|
|
126
136
|
switch (type) {
|
|
127
137
|
case 'disabled':
|
|
@@ -203,6 +213,9 @@ const Button: FC<ButtonProps> = ({
|
|
|
203
213
|
}
|
|
204
214
|
};
|
|
205
215
|
|
|
216
|
+
/**
|
|
217
|
+
* export text color
|
|
218
|
+
*/
|
|
206
219
|
const getTextColor = (): string => {
|
|
207
220
|
switch (type) {
|
|
208
221
|
case 'disabled':
|
|
@@ -237,6 +250,10 @@ const Button: FC<ButtonProps> = ({
|
|
|
237
250
|
);
|
|
238
251
|
};
|
|
239
252
|
|
|
253
|
+
/**
|
|
254
|
+
* render icon
|
|
255
|
+
* @param position
|
|
256
|
+
*/
|
|
240
257
|
const renderIcon = (position: 'left' | 'right') => {
|
|
241
258
|
const iconSize = getIconSize();
|
|
242
259
|
const space = getIconSpace();
|
|
@@ -245,9 +262,6 @@ const Button: FC<ButtonProps> = ({
|
|
|
245
262
|
const isLeft = position === 'left';
|
|
246
263
|
const icon = isLeft ? iconLeft : iconRight;
|
|
247
264
|
|
|
248
|
-
const showLoading =
|
|
249
|
-
loading && ((isLeft && loadingOnLeft) || (!isLeft && !loadingOnLeft));
|
|
250
|
-
|
|
251
265
|
const style = [
|
|
252
266
|
isLeft ? styles.leading : styles.trailing,
|
|
253
267
|
{
|
|
@@ -258,7 +272,9 @@ const Button: FC<ButtonProps> = ({
|
|
|
258
272
|
},
|
|
259
273
|
];
|
|
260
274
|
|
|
261
|
-
|
|
275
|
+
const showLoading = shouldShowLoading(iconLeft, iconRight, isLeft);
|
|
276
|
+
|
|
277
|
+
if (loading && showLoading) {
|
|
262
278
|
return (
|
|
263
279
|
<LottieView
|
|
264
280
|
ref={animationRef}
|
|
@@ -286,28 +302,36 @@ const Button: FC<ButtonProps> = ({
|
|
|
286
302
|
return null;
|
|
287
303
|
};
|
|
288
304
|
|
|
305
|
+
const { gradient, color } = config?.navigationBar?.buttonColors ?? {};
|
|
306
|
+
const isDisabled = type === 'disabled' || loading;
|
|
307
|
+
let gradientPros;
|
|
308
|
+
if (gradient?.length > 0 && type === 'primary') {
|
|
309
|
+
gradientPros = {
|
|
310
|
+
colors: gradient,
|
|
311
|
+
start: { x: 1, y: 0.5 },
|
|
312
|
+
end: { x: 0, y: 0.5 },
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
289
316
|
const sizeStyle = getSizeStyle();
|
|
290
317
|
const typeStyle = getTypeStyle();
|
|
291
|
-
|
|
292
|
-
const containerStyle = StyleSheet.flatten([
|
|
318
|
+
const containerStyle: ViewStyle = StyleSheet.flatten([
|
|
293
319
|
full && { width: '100%' },
|
|
294
320
|
loading && { opacity: 0.75 },
|
|
295
321
|
]);
|
|
296
|
-
|
|
297
322
|
const buttonStyle = StyleSheet.flatten([
|
|
298
323
|
sizeStyle,
|
|
299
324
|
typeStyle,
|
|
325
|
+
{
|
|
326
|
+
marginHorizontal: pressAnim.interpolate({
|
|
327
|
+
inputRange: [0, 1],
|
|
328
|
+
outputRange: [0, 2],
|
|
329
|
+
}),
|
|
330
|
+
},
|
|
300
331
|
]);
|
|
301
332
|
|
|
302
|
-
const innerAnimatedStyle = {
|
|
303
|
-
marginHorizontal: pressAnim.interpolate({
|
|
304
|
-
inputRange: [0, 1],
|
|
305
|
-
outputRange: [0, 2],
|
|
306
|
-
}),
|
|
307
|
-
};
|
|
308
|
-
|
|
309
333
|
if (skeleton?.loading) {
|
|
310
|
-
return <Skeleton style={[
|
|
334
|
+
return <Skeleton style={[buttonStyle, styles.skeleton]} />;
|
|
311
335
|
}
|
|
312
336
|
|
|
313
337
|
return (
|
|
@@ -317,8 +341,9 @@ const Button: FC<ButtonProps> = ({
|
|
|
317
341
|
componentId,
|
|
318
342
|
params,
|
|
319
343
|
action: 'click',
|
|
320
|
-
}}
|
|
321
|
-
|
|
344
|
+
}}
|
|
345
|
+
>
|
|
346
|
+
<TouchableOpacity
|
|
322
347
|
{...rest}
|
|
323
348
|
accessibilityLabel={componentId}
|
|
324
349
|
accessibilityRole="button"
|
|
@@ -328,18 +353,12 @@ const Button: FC<ButtonProps> = ({
|
|
|
328
353
|
...accessibilityState,
|
|
329
354
|
}}
|
|
330
355
|
disabled={isDisabled}
|
|
331
|
-
activeOpacity={0.
|
|
356
|
+
activeOpacity={0.75}
|
|
332
357
|
style={containerStyle}
|
|
333
358
|
onPressIn={animateIn}
|
|
334
359
|
onPressOut={animateOut}
|
|
335
|
-
onPress={rest.onPress}
|
|
336
360
|
>
|
|
337
|
-
<Animated.View
|
|
338
|
-
style={[
|
|
339
|
-
buttonStyle,
|
|
340
|
-
innerAnimatedStyle,
|
|
341
|
-
]}
|
|
342
|
-
>
|
|
361
|
+
<Animated.View style={buttonStyle}>
|
|
343
362
|
{renderIcon('left')}
|
|
344
363
|
{renderTitle()}
|
|
345
364
|
{renderIcon('right')}
|
|
@@ -348,7 +367,7 @@ const Button: FC<ButtonProps> = ({
|
|
|
348
367
|
)}
|
|
349
368
|
{gradientPros && <View style={styles.strokeView} />}
|
|
350
369
|
</Animated.View>
|
|
351
|
-
</
|
|
370
|
+
</TouchableOpacity>
|
|
352
371
|
</ComponentContext.Provider>
|
|
353
372
|
);
|
|
354
373
|
};
|
package/Button/styles.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {StyleSheet} from 'react-native';
|
|
1
|
+
import { StyleSheet } from 'react-native';
|
|
2
2
|
|
|
3
3
|
export default StyleSheet.create({
|
|
4
4
|
large: {
|
|
@@ -57,4 +57,7 @@ export default StyleSheet.create({
|
|
|
57
57
|
borderWidth: 0.5,
|
|
58
58
|
borderColor: '#FFF9E1',
|
|
59
59
|
},
|
|
60
|
+
skeleton: {
|
|
61
|
+
paddingHorizontal: 0,
|
|
62
|
+
},
|
|
60
63
|
});
|