Skip to content

Migrate Card & Card.Image to typescript #805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions generatedTypes/components/card/CardContext.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';
declare const CardContext: React.Context<{}>;
export default CardContext;
29 changes: 29 additions & 0 deletions generatedTypes/components/card/CardImage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { ImageSourcePropType } from 'react-native';
import { ImageProps } from '../image';
export declare type CardImageProps = ImageProps & {
/**
* Image source, either remote source or local. Note: for remote pass object {uri: <remote_uri_string>}
*/
imageSource?: ImageSourcePropType;
/**
* Image width
*/
width?: number | string;
/**
* Image height
*/
height?: number | string;
/**
* The Image position which determines the appropriate flex-ness of the image and border radius (for Android)
* this prop derived automatically from Card parent component if it rendered as a direct child of the
* Card component
*/
position?: string[];
/**
* border radius, basically for Android since overflow doesn't work well (deprecated)
*/
borderRadius?: number;
};
declare const _default: React.ComponentType<CardImageProps>;
export default _default;
12 changes: 12 additions & 0 deletions generatedTypes/components/card/CardPresenter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export declare function extractPositionValues(position: string[] | undefined): {
top: boolean;
left: boolean;
right: boolean;
bottom: boolean;
};
export declare function generateBorderRadiusStyle({ position, borderRadius }: {
position: string[] | undefined;
borderRadius: number | undefined;
}): {
[key: string]: number | undefined;
};
29 changes: 29 additions & 0 deletions generatedTypes/components/card/CardSection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { ViewStyle } from 'react-native';
import { ViewPropTypes } from '../view';
import { TextPropTypes } from '../text';
import { ImageProps } from '../image';
declare type ContentType = TextPropTypes & {
text?: string;
};
export declare type CardSectionProps = ViewPropTypes & {
/**
* Text content for the CardSection.
* Example: content={[{text: 'You’re Invited!', text70: true, dark10: true}]}
*/
content?: ContentType[];
/**
* Style for the content
*/
contentStyle?: ViewStyle;
/**
* Image props for a leading icon to render before the text
*/
leadingIcon?: ImageProps;
/**
* Image props for a trailing icon to render after the text
*/
trailingIcon?: ImageProps;
};
declare const _default: React.ComponentType<CardSectionProps>;
export default _default;
6 changes: 6 additions & 0 deletions generatedTypes/components/card/asCardChild.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
declare function asCardChild<T>(WrappedComponent: React.ComponentType<any>): React.ComponentType<T>;
export default asCardChild;
export declare type asCardChildProps = {
context?: any;
};
63 changes: 63 additions & 0 deletions generatedTypes/components/card/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { ViewStyle } from 'react-native';
import { ViewPropTypes } from '../view';
import { TouchableOpacityProps } from '../touchableOpacity';
export declare type CardPropTypes = ViewPropTypes & TouchableOpacityProps & {
/**
* card custom width
*/
width?: number | string;
/**
* card custom height
*/
height?: number | string;
/**
* should inner card flow direction be horizontal
*/
row?: boolean;
/**
* card border radius (will be passed to inner Card.Image component)
*/
borderRadius?: number;
/**
* action for when pressing the card
*/
onPress?: () => void;
/**
* whether the card should have shadow or not
*/
enableShadow?: boolean;
/**
* elevation value (Android only)
*/
elevation?: number;
/**
* enable blur effect (iOS only)
*/
enableBlur?: boolean;
/**
* blur option for blur effect according to @react-native-community/blur lib (make sure enableBlur is on)
*/
blurOptions?: object;
/**
* Additional styles for the top container
*/
containerStyle?: ViewStyle;
/**
* Adds visual indication that the card is selected
*/
selected?: boolean;
/**
* Custom options for styling the selection indication
*/
selectionOptions?: {
icon?: number;
iconColor?: string;
color?: string;
borderWidth?: number;
indicatorSize?: number;
hideIndicator?: boolean;
};
};
declare const _default: React.ComponentType<CardPropTypes>;
export default _default;
2 changes: 1 addition & 1 deletion generatedTypes/components/image/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { PureComponent } from 'react';
import { ImageProps as RNImageProps, ImageSourcePropType } from 'react-native';
import { ForwardRefInjectedProps } from '../../commons/new';
import { OverlayTypeType } from '../overlay';
declare type ImageProps = RNImageProps & {
export declare type ImageProps = RNImageProps & {
/**
* custom source transform handler for manipulating the image source (great for size control)
*/
Expand Down
9 changes: 5 additions & 4 deletions generatedTypes/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Please use this file for declaring all the exports, so they could be picked up by typescript's complier
*/
export * from './style';
export {default as Card} from './components/card';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to add some (or all) of the ones below here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that the other types will be part of Card
Card.Image, Card.Section..
But now I see it's not working so well...
I will check it

export {default as View, ViewPropTypes} from './components/view';
export {default as Text} from './components/text';
export {default as TouchableOpacity, TouchableOpacityProps} from './components/touchableOpacity';
Expand All @@ -21,10 +22,10 @@ export {
Avatar,
Badge,
Card,
CardProps,
CardImageProps,
CardSectionProps,
CardSectionContentProps,
// CardProps,
// CardImageProps,
// CardSectionProps,
// CardSectionContentProps,
Carousel,
ConnectionStatusBar,
Dialog,
Expand Down
File renamed without changes.
90 changes: 0 additions & 90 deletions src/components/card/CardImage.js

This file was deleted.

100 changes: 100 additions & 0 deletions src/components/card/CardImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, {PureComponent} from 'react';
import {View, StyleSheet, ImageSourcePropType} from 'react-native';
// import {BaseComponent} from '../../commons';
import Image, {ImageProps} from '../image';
import * as CardPresenter from './CardPresenter';
import asCardChild, {asCardChildProps} from './asCardChild';
// @ts-ignore
import {LogService} from '../../services';

export type CardImageProps = ImageProps & {
/**
* Image source, either remote source or local. Note: for remote pass object {uri: <remote_uri_string>}
*/
imageSource?: ImageSourcePropType;
/**
* Image width
*/
width?: number | string;
/**
* Image height
*/
height?: number | string;
/**
* The Image position which determines the appropriate flex-ness of the image and border radius (for Android)
* this prop derived automatically from Card parent component if it rendered as a direct child of the
* Card component
*/
position?: string[];
/**
* border radius, basically for Android since overflow doesn't work well (deprecated)
*/
borderRadius?: number;
};

type Props = CardImageProps & asCardChildProps;

/**
* @description: Card.Image, part of the Card component belongs inside a Card (better be a direct child)
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/CardsScreen.js
*/
class CardImage extends PureComponent<Props> {
static displayName = 'Card.Image';

styles: any;

constructor(props: Props) {
super(props);

if (props.borderRadius) {
LogService.warn(
'uilib: Please stop passing borderRadius to Card.Image, it will get the borderRadius from the Card'
);
}
this.styles = createStyles(props);
}

render() {
const {
imageSource,
style,
testID,
overlayType,
context: {borderStyle}
} = this.props;
if (imageSource) {
return (
<View style={[this.styles.container, borderStyle, style]}>
<Image
testID={testID}
source={imageSource}
style={[this.styles.image]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed imageStyle, I need it in CardItem

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you pass the imageStyle I couldn't find where it comes from?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just this style={[this.styles.image, imageStyle]}, I might have forgotten to add it to the PropTypes :S

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What not call it style ?
you mean that the user can pass from outside?

overlayType={overlayType}
/>
</View>
);
}
return null;
}
}

function createStyles({width, height, context: {position}}: Props) {
const {top, left, right, bottom} = CardPresenter.extractPositionValues(
position
);
return StyleSheet.create({
container: {
height: left || right ? undefined : height,
width: top || bottom ? undefined : width,
overflow: 'hidden'
},
image: {
width: undefined,
height: undefined,
flex: 1,
resizeMode: 'cover'
}
});
}

export default asCardChild<CardImageProps>(CardImage);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';

export function extractPositionValues(position) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of turning position to an enum?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think position is not a string but an array.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be an array of enums, I have code for it, I can create a PR later and if you can decide if you like the idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, let's save it for later.. this PR is getting too messy

export function extractPositionValues(position: string[] | undefined) {
const top = _.includes(position, 'top');
const left = _.includes(position, 'left');
const right = _.includes(position, 'right');
Expand All @@ -9,8 +9,8 @@ export function extractPositionValues(position) {
return {top, left, right, bottom};
}

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

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

Expand Down
Loading