Skip to content

Commit 0d660e2

Browse files
authored
Migrate Card & Card.Image to typescript (#805)
* rename files to ts/x * Migrate Card (and CardImage) to typescript * migrate Card files to TS * migrate Card components to Typescript * fix Card typescript issues * Complete Card migration, migrate CardScreen to ts
1 parent ed2812f commit 0d660e2

File tree

20 files changed

+484
-256
lines changed

20 files changed

+484
-256
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
declare const CardContext: React.Context<{}>;
3+
export default CardContext;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { ImageSourcePropType } from 'react-native';
3+
import { ImageProps } from '../image';
4+
export declare type CardImageProps = ImageProps & {
5+
/**
6+
* Image source, either remote source or local. Note: for remote pass object {uri: <remote_uri_string>}
7+
*/
8+
imageSource?: ImageSourcePropType;
9+
/**
10+
* Image width
11+
*/
12+
width?: number | string;
13+
/**
14+
* Image height
15+
*/
16+
height?: number | string;
17+
/**
18+
* The Image position which determines the appropriate flex-ness of the image and border radius (for Android)
19+
* this prop derived automatically from Card parent component if it rendered as a direct child of the
20+
* Card component
21+
*/
22+
position?: string[];
23+
/**
24+
* border radius, basically for Android since overflow doesn't work well (deprecated)
25+
*/
26+
borderRadius?: number;
27+
};
28+
declare const _default: React.ComponentType<CardImageProps>;
29+
export default _default;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export declare function extractPositionValues(position: string[] | undefined): {
2+
top: boolean;
3+
left: boolean;
4+
right: boolean;
5+
bottom: boolean;
6+
};
7+
export declare function generateBorderRadiusStyle({ position, borderRadius }: {
8+
position: string[] | undefined;
9+
borderRadius: number | undefined;
10+
}): {
11+
[key: string]: number | undefined;
12+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { ViewStyle } from 'react-native';
3+
import { ViewPropTypes } from '../view';
4+
import { TextPropTypes } from '../text';
5+
import { ImageProps } from '../image';
6+
declare type ContentType = TextPropTypes & {
7+
text?: string;
8+
};
9+
export declare type CardSectionProps = ViewPropTypes & {
10+
/**
11+
* Text content for the CardSection.
12+
* Example: content={[{text: 'You’re Invited!', text70: true, dark10: true}]}
13+
*/
14+
content?: ContentType[];
15+
/**
16+
* Style for the content
17+
*/
18+
contentStyle?: ViewStyle;
19+
/**
20+
* Image props for a leading icon to render before the text
21+
*/
22+
leadingIcon?: ImageProps;
23+
/**
24+
* Image props for a trailing icon to render after the text
25+
*/
26+
trailingIcon?: ImageProps;
27+
};
28+
declare const _default: React.ComponentType<CardSectionProps>;
29+
export default _default;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
declare function asCardChild<T>(WrappedComponent: React.ComponentType<any>): React.ComponentType<T>;
3+
export default asCardChild;
4+
export declare type asCardChildProps = {
5+
context?: any;
6+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from 'react';
2+
import { ViewStyle } from 'react-native';
3+
import { ViewPropTypes } from '../view';
4+
import { TouchableOpacityProps } from '../touchableOpacity';
5+
export declare type CardPropTypes = ViewPropTypes & TouchableOpacityProps & {
6+
/**
7+
* card custom width
8+
*/
9+
width?: number | string;
10+
/**
11+
* card custom height
12+
*/
13+
height?: number | string;
14+
/**
15+
* should inner card flow direction be horizontal
16+
*/
17+
row?: boolean;
18+
/**
19+
* card border radius (will be passed to inner Card.Image component)
20+
*/
21+
borderRadius?: number;
22+
/**
23+
* action for when pressing the card
24+
*/
25+
onPress?: () => void;
26+
/**
27+
* whether the card should have shadow or not
28+
*/
29+
enableShadow?: boolean;
30+
/**
31+
* elevation value (Android only)
32+
*/
33+
elevation?: number;
34+
/**
35+
* enable blur effect (iOS only)
36+
*/
37+
enableBlur?: boolean;
38+
/**
39+
* blur option for blur effect according to @react-native-community/blur lib (make sure enableBlur is on)
40+
*/
41+
blurOptions?: object;
42+
/**
43+
* Additional styles for the top container
44+
*/
45+
containerStyle?: ViewStyle;
46+
/**
47+
* Adds visual indication that the card is selected
48+
*/
49+
selected?: boolean;
50+
/**
51+
* Custom options for styling the selection indication
52+
*/
53+
selectionOptions?: {
54+
icon?: number;
55+
iconColor?: string;
56+
color?: string;
57+
borderWidth?: number;
58+
indicatorSize?: number;
59+
hideIndicator?: boolean;
60+
};
61+
};
62+
declare const _default: React.ComponentType<CardPropTypes>;
63+
export default _default;

generatedTypes/components/image/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { PureComponent } from 'react';
22
import { ImageProps as RNImageProps, ImageSourcePropType } from 'react-native';
33
import { ForwardRefInjectedProps } from '../../commons/new';
44
import { OverlayTypeType } from '../overlay';
5-
declare type ImageProps = RNImageProps & {
5+
export declare type ImageProps = RNImageProps & {
66
/**
77
* custom source transform handler for manipulating the image source (great for size control)
88
*/

generatedTypes/index.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Please use this file for declaring all the exports, so they could be picked up by typescript's complier
55
*/
66
export * from './style';
7+
export {default as Card} from './components/card';
78
export {default as View, ViewPropTypes} from './components/view';
89
export {default as Text} from './components/text';
910
export {default as TouchableOpacity, TouchableOpacityProps} from './components/touchableOpacity';
@@ -21,10 +22,10 @@ export {
2122
Avatar,
2223
Badge,
2324
Card,
24-
CardProps,
25-
CardImageProps,
26-
CardSectionProps,
27-
CardSectionContentProps,
25+
// CardProps,
26+
// CardImageProps,
27+
// CardSectionProps,
28+
// CardSectionContentProps,
2829
Carousel,
2930
ConnectionStatusBar,
3031
Dialog,

src/components/card/CardImage.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/components/card/CardImage.tsx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import React, {PureComponent} from 'react';
2+
import {View, StyleSheet, ImageSourcePropType} from 'react-native';
3+
// import {BaseComponent} from '../../commons';
4+
import Image, {ImageProps} from '../image';
5+
import * as CardPresenter from './CardPresenter';
6+
import asCardChild, {asCardChildProps} from './asCardChild';
7+
// @ts-ignore
8+
import {LogService} from '../../services';
9+
10+
export type CardImageProps = ImageProps & {
11+
/**
12+
* Image source, either remote source or local. Note: for remote pass object {uri: <remote_uri_string>}
13+
*/
14+
imageSource?: ImageSourcePropType;
15+
/**
16+
* Image width
17+
*/
18+
width?: number | string;
19+
/**
20+
* Image height
21+
*/
22+
height?: number | string;
23+
/**
24+
* The Image position which determines the appropriate flex-ness of the image and border radius (for Android)
25+
* this prop derived automatically from Card parent component if it rendered as a direct child of the
26+
* Card component
27+
*/
28+
position?: string[];
29+
/**
30+
* border radius, basically for Android since overflow doesn't work well (deprecated)
31+
*/
32+
borderRadius?: number;
33+
};
34+
35+
type Props = CardImageProps & asCardChildProps;
36+
37+
/**
38+
* @description: Card.Image, part of the Card component belongs inside a Card (better be a direct child)
39+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/CardsScreen.js
40+
*/
41+
class CardImage extends PureComponent<Props> {
42+
static displayName = 'Card.Image';
43+
44+
styles: any;
45+
46+
constructor(props: Props) {
47+
super(props);
48+
49+
if (props.borderRadius) {
50+
LogService.warn(
51+
'uilib: Please stop passing borderRadius to Card.Image, it will get the borderRadius from the Card'
52+
);
53+
}
54+
this.styles = createStyles(props);
55+
}
56+
57+
render() {
58+
const {
59+
imageSource,
60+
style,
61+
testID,
62+
overlayType,
63+
context: {borderStyle}
64+
} = this.props;
65+
if (imageSource) {
66+
return (
67+
<View style={[this.styles.container, borderStyle, style]}>
68+
<Image
69+
testID={testID}
70+
source={imageSource}
71+
style={[this.styles.image]}
72+
overlayType={overlayType}
73+
/>
74+
</View>
75+
);
76+
}
77+
return null;
78+
}
79+
}
80+
81+
function createStyles({width, height, context: {position}}: Props) {
82+
const {top, left, right, bottom} = CardPresenter.extractPositionValues(
83+
position
84+
);
85+
return StyleSheet.create({
86+
container: {
87+
height: left || right ? undefined : height,
88+
width: top || bottom ? undefined : width,
89+
overflow: 'hidden'
90+
},
91+
image: {
92+
width: undefined,
93+
height: undefined,
94+
flex: 1,
95+
resizeMode: 'cover'
96+
}
97+
});
98+
}
99+
100+
export default asCardChild<CardImageProps>(CardImage);

src/components/card/CardPresenter.js renamed to src/components/card/CardPresenter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22

3-
export function extractPositionValues(position) {
3+
export function extractPositionValues(position: string[] | undefined) {
44
const top = _.includes(position, 'top');
55
const left = _.includes(position, 'left');
66
const right = _.includes(position, 'right');
@@ -9,8 +9,8 @@ export function extractPositionValues(position) {
99
return {top, left, right, bottom};
1010
}
1111

12-
export function generateBorderRadiusStyle({position, borderRadius}) {
13-
const borderRadiusStyle = {};
12+
export function generateBorderRadiusStyle({position, borderRadius}: {position: string[] | undefined, borderRadius: number | undefined}) {
13+
const borderRadiusStyle: {[key: string]: number | undefined} = {};
1414

1515
const {top, left, right, bottom} = extractPositionValues(position);
1616

0 commit comments

Comments
 (0)