forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTableTitle.tsx
160 lines (140 loc) · 3.33 KB
/
DataTableTitle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as React from 'react';
import {
Animated,
StyleProp,
StyleSheet,
TouchableWithoutFeedback,
View,
ViewStyle,
I18nManager,
} from 'react-native';
import color from 'color';
import MaterialCommunityIcon from '../MaterialCommunityIcon';
import Text from '../Typography/Text';
import { withTheme } from '../../core/theming';
import { Theme } from '../../types';
type Props = React.ComponentPropsWithRef<typeof TouchableWithoutFeedback> & {
/**
* Text content of the `DataTableTitle`.
*/
children: React.ReactNode;
/**
* Align the text to the right. Generally monetary or number fields are aligned to right.
*/
numeric?: boolean;
/**
* Direction of sorting. An arrow indicating the direction is displayed when this is given.
*/
sortDirection?: 'ascending' | 'descending';
/**
* The number of lines to show.
*/
numberOfLines?: number;
/**
* Function to execute on press.
*/
onPress?: () => void;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: Theme;
};
type State = {
spinAnim: Animated.Value;
};
class DataTableTitle extends React.Component<Props, State> {
static displayName = 'DataTable.Title';
static defaultProps = {
numberOfLines: 1,
};
state = {
spinAnim: new Animated.Value(
this.props.sortDirection === 'ascending' ? 0 : 1
),
};
componentDidUpdate(prevProps: Props) {
if (prevProps.sortDirection === this.props.sortDirection) {
return;
}
Animated.timing(this.state.spinAnim, {
toValue: this.props.sortDirection === 'ascending' ? 0 : 1,
duration: 150,
useNativeDriver: true,
}).start();
}
render() {
const {
numeric,
children,
onPress,
sortDirection,
theme,
style,
numberOfLines,
...rest
} = this.props;
const textColor = color(theme.colors.text)
.alpha(0.6)
.rgb()
.string();
const spin = this.state.spinAnim.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '180deg'],
});
const icon = sortDirection ? (
<Animated.View style={[styles.icon, { transform: [{ rotate: spin }] }]}>
<MaterialCommunityIcon
name="arrow-down"
size={16}
color={theme.colors.text}
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
</Animated.View>
) : null;
return (
<TouchableWithoutFeedback disabled={!onPress} onPress={onPress} {...rest}>
<View style={[styles.container, numeric && styles.right, style]}>
{icon}
<Text
style={[
styles.cell,
sortDirection ? styles.sorted : { color: textColor },
]}
numberOfLines={numberOfLines}
>
{children}
</Text>
</View>
</TouchableWithoutFeedback>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
alignContent: 'center',
paddingVertical: 12,
},
right: {
justifyContent: 'flex-end',
},
cell: {
height: 24,
lineHeight: 24,
fontSize: 12,
fontWeight: '500',
alignItems: 'center',
},
sorted: {
marginLeft: 8,
},
icon: {
height: 24,
justifyContent: 'center',
},
});
export default withTheme(DataTableTitle);
// @component-docs ignore-next-line
export { DataTableTitle };