Skip to content

Commit f33c42d

Browse files
authored
Typescript/list item (#1460)
* index.js -> index.tsx * migrate ListItem to typescript * ListItemPart.js -> ListItemPart.tsx * ListItemPart migrated to ts * fix typing error * BasicListScreen.js -> BasicListScreen.tsx * docs to point to tsx file * orders to ts * export listitem + prop + passing list-item touchable props instead of view's * orders data to type * testid should be optional * listItem typings * use uilib touchable-opacity props * animatable types * BasicListScreen - demo screen typescript * @ts-expect-error until we merge the fix * remove old typings, export type from generatedTypes
1 parent da1b03c commit f33c42d

File tree

16 files changed

+380
-271
lines changed

16 files changed

+380
-271
lines changed

demo/src/data/orders.js renamed to demo/src/data/orders.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
const orders = [
1+
type InventoryType = {
2+
trackingMethod: string;
3+
status: string;
4+
quantity: number;
5+
}
6+
7+
export type OrderType = {
8+
name: string;
9+
formattedPrice: string;
10+
inventory: InventoryType
11+
mediaUrl: string;
12+
}
13+
14+
const orders: Array<OrderType> = [
215
{
316
name: '#100201',
417
formattedPrice: '$19.99',

demo/src/screens/componentScreens/BasicListScreen.js renamed to demo/src/screens/componentScreens/BasicListScreen.tsx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React, {Component} from 'react';
22
import {StyleSheet, Alert, FlatList} from 'react-native';
33
import * as Animatable from 'react-native-animatable';
4-
import {AnimatableManager, Colors, BorderRadiuses, ListItem, Text} from 'react-native-ui-lib'; //eslint-disable-line
5-
import orders from '../../data/orders';
4+
import {AnimatableManager, Colors, BorderRadiuses, ListItem, Text} from 'react-native-ui-lib';
5+
import orders, {OrderType} from '../../data/orders';
66

7+
type BasicListScreenState = {
8+
onEdit: boolean;
9+
updating: boolean;
10+
}
711

8-
export default class BasicListScreen extends Component {
12+
export default class BasicListScreen extends Component<{}, BasicListScreenState> {
913

10-
constructor(props) {
14+
constructor(props: any) {
1115
super(props);
1216

1317
this.state = {
@@ -16,36 +20,44 @@ export default class BasicListScreen extends Component {
1620
};
1721
}
1822

19-
keyExtractor = item => item.name;
23+
keyExtractor = (item: OrderType) => item.name;
2024

21-
renderRow(row, id) {
25+
renderRow(row: OrderType, id: number) {
2226
const statusColor = row.inventory.status === 'Paid' ? Colors.green30 : Colors.red30;
2327
const animationProps = AnimatableManager.presets.fadeInRight;
24-
const imageAnimationProps = AnimatableManager.getRandomDelay();
28+
const imageAnimationProps = AnimatableManager.getRandomDelay(undefined, undefined);
2529

2630
return (
2731
<Animatable.View {...animationProps}>
2832
<ListItem
33+
// @ts-expect-error
2934
activeBackgroundColor={Colors.dark60}
3035
activeOpacity={0.3}
3136
height={77.5}
3237
onPress={() => Alert.alert(`pressed on order #${id + 1}`)}
3338
>
3439
<ListItem.Part left>
35-
<Animatable.Image
36-
source={{uri: row.mediaUrl}}
37-
style={styles.image}
38-
{...imageAnimationProps}
39-
/>
40+
<Animatable.Image source={{uri: row.mediaUrl}} style={styles.image} {...imageAnimationProps} />
4041
</ListItem.Part>
4142
<ListItem.Part middle column containerStyle={[styles.border, {paddingRight: 17}]}>
4243
<ListItem.Part containerStyle={{marginBottom: 3}}>
43-
<Text dark10 text70 style={{flex: 1, marginRight: 10}} numberOfLines={1}>{row.name}</Text>
44-
<Text dark10 text70 style={{marginTop: 2}}>{row.formattedPrice}</Text>
44+
<Text dark10 text70 style={{flex: 1, marginRight: 10}} numberOfLines={1}>
45+
{row.name}
46+
</Text>
47+
<Text dark10 text70 style={{marginTop: 2}}>
48+
{row.formattedPrice}
49+
</Text>
4550
</ListItem.Part>
4651
<ListItem.Part>
47-
<Text style={{flex: 1, marginRight: 10}} text90 dark40 numberOfLines={1}>{`${row.inventory.quantity} item`}</Text>
48-
<Text text90 color={statusColor} numberOfLines={1}>{row.inventory.status}</Text>
52+
<Text
53+
style={{flex: 1, marginRight: 10}}
54+
text90
55+
dark40
56+
numberOfLines={1}
57+
>{`${row.inventory.quantity} item`}</Text>
58+
<Text text90 color={statusColor} numberOfLines={1}>
59+
{row.inventory.status}
60+
</Text>
4961
</ListItem.Part>
5062
</ListItem.Part>
5163
</ListItem>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { ListItemPartProps } from './types';
3+
export { ListItemPartProps };
4+
declare const _default: React.ComponentClass<ListItemPartProps & {
5+
useCustomTheme?: boolean | undefined;
6+
}, any>;
7+
export default _default;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { ListItemProps } from './types';
3+
export { ListItemProps };
4+
declare const _default: React.ComponentClass<ListItemProps & {
5+
useCustomTheme?: boolean | undefined;
6+
}, any>;
7+
export default _default;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/// <reference types="react" />
2+
import { ViewStyle } from 'react-native';
3+
import { TouchableOpacityProps } from '../touchableOpacity';
4+
export declare type ListItemProps = {
5+
/**
6+
* the list item height
7+
*/
8+
height?: ViewStyle['height'];
9+
/**
10+
* action for when pressing the item
11+
*/
12+
onPress?: () => void;
13+
/**
14+
* action for when long pressing the item
15+
*/
16+
onLongPress?: () => void;
17+
/**
18+
* Additional styles for the top container
19+
*/
20+
containerStyle?: ViewStyle;
21+
/**
22+
* The container element to wrap the ListItem
23+
*/
24+
containerElement?: React.ComponentType<ListItemProps | TouchableOpacityProps>;
25+
/**
26+
* The inner element style
27+
*/
28+
style?: ViewStyle;
29+
/**
30+
* The inner element pressed backgroundColor
31+
*/
32+
underlayColor?: string;
33+
testID?: string;
34+
};
35+
export declare type ListItemPartProps = {
36+
/**
37+
* this part content will be aligned to left
38+
*/
39+
left?: boolean;
40+
/**
41+
* this part content will be aligned to spreaded
42+
*/
43+
middle?: boolean;
44+
/**
45+
* this part content will be aligned to right
46+
*/
47+
right?: boolean;
48+
/**
49+
* this part content direction will be row (default)
50+
*/
51+
row?: boolean;
52+
/**
53+
* this part content direction will be column
54+
*/
55+
column?: boolean;
56+
/**
57+
* container style
58+
*/
59+
containerStyle?: ViewStyle;
60+
};

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export {default as PageControl, PageControlProps} from './components/pageControl
8080
export {default as Carousel, CarouselProps, PageControlPosition} from './components/carousel';
8181
export {default as ActionSheet} from './components/actionSheet';
8282
export {default as Wizard, WizardProps, WizardStepProps, WizardStepStates, WizardStepConfig, WizardStepsConfig} from './components/wizard';
83+
export {default as ListItem, ListItemProps} from './components/listItem';
8384
export {default as StateScreen, StateScreenProps} from './components/stateScreen';
8485
export {default as LoaderScreen, LoaderScreenProps} from './components/loaderScreen';
8586

@@ -88,7 +89,6 @@ export {
8889
BaseInput,
8990
TextArea,
9091
MaskedInput,
91-
ListItem,
9292
ProgressBar,
9393
ColorSliderGroup,
9494
Stepper,

generatedTypes/style/animatableManager.d.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
1+
import * as Animatable from 'react-native-animatable';
2+
declare type AnimationType = {
3+
animation: Animatable.Animation;
4+
easing?: Animatable.Easing;
5+
duration: number;
6+
useNativeDriver?: boolean;
7+
};
8+
declare type AnimationPresets = {
9+
slideInUp: AnimationType;
10+
slideInDown: AnimationType;
11+
fadeIn: AnimationType;
12+
fadeOut: AnimationType;
13+
fadeInRight: AnimationType;
14+
};
115
/**
216
* @description: Animatable animations and presets
317
* @extendsnotes: To have access to uilib's animations, and load your custom animations (optional), call:
418
* 'Animatable.initializeRegistryWithDefinitions(AnimatableManager.loadAnimationDefinitions(<OPTIONAL_CUSTOM_ANIMATION>));'
519
* in your app entry point
620
*/
721
export declare class AnimatableManager {
8-
presets: {
9-
slideInUp: {
10-
animation: string;
11-
easing: string;
12-
duration: number;
13-
useNativeDriver: boolean;
14-
};
15-
slideInDown: {
16-
animation: string;
17-
easing: string;
18-
duration: number;
19-
useNativeDriver: boolean;
20-
};
21-
fadeIn: {
22-
animation: string;
23-
duration: number;
24-
useNativeDriver: boolean;
25-
};
26-
fadeOut: {
27-
animation: string;
28-
duration: number;
29-
useNativeDriver: boolean;
30-
};
31-
fadeInRight: {
32-
animation: string;
33-
easing: string;
34-
duration: number;
35-
useNativeDriver: boolean;
36-
};
37-
};
22+
presets: AnimationPresets;
3823
animations: any;
3924
constructor();
4025
loadAnimationPresets(animationPresets: Dictionary<any>): void;

src/components/actionSheet/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class ActionSheet extends Component<ActionSheetProps> {
164164
key={index}
165165
testID={option.testID}
166166
onPress={() => this.onOptionPress(index)}
167+
// @ts-expect-error
167168
activeBackgroundColor={Colors.dark80}
168169
>
169170
<View row paddingL-16 flex centerV>

src/components/listItem/ListItemPart.js

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, {Component} from 'react';
2+
import {StyleSheet, ViewStyle} from 'react-native';
3+
import {asBaseComponent} from '../../commons/new';
4+
import View from '../view';
5+
import {ListItemPartProps} from './types';
6+
7+
class ListItemPart extends Component<ListItemPartProps> {
8+
static displayName = 'ListItem.Part';
9+
10+
styles = createStyles(this.props);
11+
12+
render() {
13+
const {containerStyle, ...others} = this.props;
14+
return (
15+
<View style={[this.styles.container, containerStyle]} {...others}>
16+
{this.props.children}
17+
</View>
18+
);
19+
}
20+
}
21+
22+
export {ListItemPartProps};
23+
export default asBaseComponent<ListItemPartProps>(ListItemPart);
24+
25+
function createStyles({left, right, middle, column}: ListItemPartProps) {
26+
let justifyContent: ViewStyle['justifyContent'];
27+
if (!column) {
28+
justifyContent = 'space-between';
29+
if (left) {
30+
justifyContent = 'flex-start';
31+
}
32+
if (right) {
33+
justifyContent = 'flex-end';
34+
}
35+
if (middle) {
36+
justifyContent = 'space-between';
37+
}
38+
} else {
39+
justifyContent = 'center';
40+
}
41+
42+
return StyleSheet.create({
43+
container: {
44+
flexDirection: column ? undefined : 'row',
45+
flex: middle ? 1 : 0,
46+
justifyContent,
47+
alignItems: column ? undefined : 'center'
48+
}
49+
});
50+
}

0 commit comments

Comments
 (0)