@momo-kits/collapse 0.80.9 → 0.81.1

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.
Files changed (4) hide show
  1. package/index.tsx +87 -72
  2. package/package.json +1 -1
  3. package/styles.ts +9 -3
  4. package/types.ts +11 -5
package/index.tsx CHANGED
@@ -1,9 +1,28 @@
1
- import React, {FC, useContext, useRef, useState} from 'react';
2
- import {TouchableOpacity, View, Animated, Easing} from 'react-native';
3
- import {ApplicationContext, Icon, Image, Text} from '@momo-kits/foundation';
4
- import {CollapseProps} from './types';
1
+ import React, {FC, useContext, useState} from 'react';
2
+ import {
3
+ Animated,
4
+ LayoutAnimation,
5
+ Platform,
6
+ TouchableOpacity,
7
+ UIManager,
8
+ View,
9
+ } from 'react-native';
10
+ import {
11
+ ApplicationContext,
12
+ Icon,
13
+ Image,
14
+ Tag,
15
+ Text,
16
+ } from '@momo-kits/foundation';
17
+ import {CollapseProps, CollapseImageSize} from './types';
5
18
  import styles from './styles';
6
19
 
20
+ if (Platform.OS === 'android') {
21
+ if (UIManager.setLayoutAnimationEnabledExperimental) {
22
+ UIManager.setLayoutAnimationEnabledExperimental(true);
23
+ }
24
+ }
25
+
7
26
  const Collapse: FC<CollapseProps> = ({
8
27
  image,
9
28
  title = 'Title',
@@ -11,10 +30,14 @@ const Collapse: FC<CollapseProps> = ({
11
30
  onPress,
12
31
  showBorder,
13
32
  children,
33
+ imageSize = 24,
34
+ subTitle,
35
+ tagProps,
36
+ scrollEnabled = false,
37
+ scrollContentMaxHeight = 240,
14
38
  }) => {
15
39
  const {theme} = useContext(ApplicationContext);
16
40
  const [expanded, setExpanded] = useState(false);
17
- const [height, setHeight] = useState(0);
18
41
 
19
42
  const iconSource = expanded
20
43
  ? 'arrow_chevron_up_small'
@@ -22,60 +45,41 @@ const Collapse: FC<CollapseProps> = ({
22
45
  const radiusStyle = {borderBottomLeftRadius: 0, borderBottomRightRadius: 0};
23
46
  const borderWidth = showBorder ? 1 : 0;
24
47
 
25
- const animation = useRef(new Animated.Value(0)).current;
26
- const opacity = useRef(new Animated.Value(0)).current;
27
- const containerHeight = animation.interpolate({
28
- inputRange: [0, 1],
29
- outputRange: [-height, 0],
30
- });
48
+ const renderInfo = () => {
49
+ if (subTitle) {
50
+ return (
51
+ <Text numberOfLines={1} typography={'paragraph_default'}>
52
+ {subTitle}
53
+ </Text>
54
+ );
55
+ }
56
+ if (tagProps) {
57
+ return <Tag {...tagProps} />;
58
+ }
31
59
 
32
- const expand = () => {
33
- Animated.parallel([
34
- Animated.timing(animation, {
35
- toValue: 1,
36
- duration: 300,
37
- easing: Easing.linear,
38
- useNativeDriver: false,
39
- }),
40
- Animated.timing(opacity, {
41
- toValue: 1,
42
- duration: 100,
43
- easing: Easing.linear,
44
- useNativeDriver: false,
45
- }),
46
- ]).start();
60
+ return null;
47
61
  };
48
- const collapse = () => {
49
- Animated.parallel([
50
- Animated.timing(animation, {
51
- toValue: 0,
52
- duration: 300,
53
- easing: Easing.linear,
54
- useNativeDriver: false,
55
- }),
56
- Animated.timing(opacity, {
57
- toValue: 0,
58
- duration: 300,
59
- easing: Easing.linear,
60
- useNativeDriver: false,
61
- }),
62
- ]).start();
62
+
63
+ const onPressHeader = () => {
64
+ onPress?.();
65
+
66
+ LayoutAnimation.configureNext({
67
+ duration: 300,
68
+ create: {
69
+ type: LayoutAnimation.Types.easeInEaseOut,
70
+ property: LayoutAnimation.Properties.opacity,
71
+ },
72
+ update: {type: LayoutAnimation.Types.easeInEaseOut},
73
+ });
74
+
75
+ setExpanded(!expanded);
63
76
  };
64
77
 
65
78
  const renderHeader = () => {
66
- const _onPress = () => {
67
- onPress?.();
68
- if (!expanded) {
69
- expand();
70
- } else {
71
- collapse();
72
- }
73
- setExpanded(!expanded);
74
- };
75
79
  return (
76
80
  <TouchableOpacity
77
81
  activeOpacity={1}
78
- onPress={_onPress}
82
+ onPress={onPressHeader}
79
83
  style={[
80
84
  styles.header,
81
85
  {
@@ -85,39 +89,49 @@ const Collapse: FC<CollapseProps> = ({
85
89
  },
86
90
  expanded && radiusStyle,
87
91
  ]}>
88
- {!!image && <Image source={{uri: image}} style={styles.image} />}
92
+ {!!image && (
93
+ <Image
94
+ source={{uri: image}}
95
+ style={[styles.image, {width: imageSize, height: imageSize}]}
96
+ />
97
+ )}
89
98
  <View style={styles.headerContent}>
90
- <Text numberOfLines={1} typography={'header_default'}>
91
- {title}
92
- </Text>
93
- {!!description && (
94
- <Text numberOfLines={2} typography={'description_default'}>
95
- {description}
99
+ <View style={styles.flex2}>
100
+ <Text numberOfLines={1} typography={'header_default'}>
101
+ {title}
96
102
  </Text>
97
- )}
103
+ {!!description && (
104
+ <Text numberOfLines={2} typography={'description_default'}>
105
+ {description}
106
+ </Text>
107
+ )}
108
+ </View>
109
+ <View style={styles.infoWrap}>{renderInfo()}</View>
98
110
  </View>
99
- <Icon source={iconSource} />
111
+ <Icon source={iconSource} style={styles.icon} />
100
112
  </TouchableOpacity>
101
113
  );
102
114
  };
103
115
 
104
116
  const renderContent = () => {
105
117
  return (
106
- <Animated.View
107
- onLayout={e => {
108
- setHeight(e.nativeEvent.layout.height);
109
- }}
110
- style={{
111
- transform: [{translateY: containerHeight}],
112
- zIndex: -1,
113
- opacity,
114
- }}>
115
- {children}
116
- </Animated.View>
118
+ <Animated.ScrollView
119
+ scrollEnabled={scrollEnabled}
120
+ showsVerticalScrollIndicator={false}
121
+ style={[
122
+ scrollEnabled && {
123
+ maxHeight: scrollContentMaxHeight,
124
+ },
125
+ {
126
+ backgroundColor: theme.colors.background.surface,
127
+ },
128
+ ]}>
129
+ {expanded && children}
130
+ </Animated.ScrollView>
117
131
  );
118
132
  };
119
133
  return (
120
- <View style={{overflow: 'hidden'}}>
134
+ <View>
121
135
  {renderHeader()}
122
136
  {renderContent()}
123
137
  </View>
@@ -125,3 +139,4 @@ const Collapse: FC<CollapseProps> = ({
125
139
  };
126
140
 
127
141
  export default Collapse;
142
+ export type {CollapseProps, CollapseImageSize};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/collapse",
3
- "version": "0.80.9",
3
+ "version": "0.81.1",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {
package/styles.ts CHANGED
@@ -3,8 +3,7 @@ import {Spacing, Radius} from '@momo-kits/foundation';
3
3
 
4
4
  export default StyleSheet.create({
5
5
  image: {
6
- width: 24,
7
- height: 24,
6
+ borderRadius: Radius.XS,
8
7
  marginRight: Spacing.S,
9
8
  },
10
9
  header: {
@@ -13,5 +12,12 @@ export default StyleSheet.create({
13
12
  paddingVertical: Spacing.S,
14
13
  borderRadius: Radius.S,
15
14
  },
16
- headerContent: {flex: 1},
15
+ headerContent: {
16
+ flex: 1,
17
+ flexDirection: 'row',
18
+ justifyContent: 'space-between',
19
+ },
20
+ infoWrap: {maxWidth: 200, alignItems: 'flex-end', marginLeft: Spacing.S},
21
+ flex2: {flex: 2},
22
+ icon: {marginLeft: Spacing.S},
17
23
  });
package/types.ts CHANGED
@@ -1,8 +1,7 @@
1
- /**
2
- * Props for the Collapse component. The Collapse component is an interactive component
3
- * typically used to show and hide content via user interaction, often used for FAQs,
4
- * toggles, or detailed breakdowns in a condensed space.
5
- */
1
+ import {TagProps} from '@momo-kits/foundation';
2
+
3
+ export type CollapseImageSize = 24 | 32 | 40;
4
+
6
5
  export type CollapseProps = {
7
6
  /**
8
7
  * The title represents what is being collapsed or expanded. It is usually a summary or
@@ -37,4 +36,11 @@ export type CollapseProps = {
37
36
  * Defaults to `false` if not provided.
38
37
  */
39
38
  showBorder?: boolean;
39
+
40
+ imageSize?: CollapseImageSize;
41
+
42
+ subTitle?: string;
43
+ tagProps?: TagProps;
44
+ scrollEnabled?: boolean;
45
+ scrollContentMaxHeight?: number;
40
46
  };