Skip to content

support margin modifiers in SkeletonView #1807

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 1 commit into from
Feb 4, 2022
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
10 changes: 6 additions & 4 deletions generatedTypes/src/components/skeletonView/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import { Animated, StyleProp, ViewStyle, AccessibilityProps } from 'react-native';
import { BaseComponentInjectedProps, MarginModifiers } from '../../commons/new';
export declare enum Template {
LIST_ITEM = "listItem",
TEXT_CONTENT = "content"
Expand Down Expand Up @@ -36,7 +37,7 @@ export interface SkeletonListProps {
*/
renderEndContent?: () => React.ReactElement | undefined;
}
export interface SkeletonViewProps extends AccessibilityProps {
export interface SkeletonViewProps extends AccessibilityProps, MarginModifiers {
/**
* The content has been loaded, start fading out the skeleton and fading in the content
*/
Expand Down Expand Up @@ -138,7 +139,8 @@ interface SkeletonState {
* @image: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Skeleton/Skeleton.gif?raw=true
* @notes: View requires installing the 'react-native-shimmer-placeholder' and 'react-native-linear-gradient' library
*/
declare class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {
declare type InternalSkeletonViewProps = SkeletonViewProps & BaseComponentInjectedProps;
declare class SkeletonView extends Component<InternalSkeletonViewProps, SkeletonState> {
static defaultProps: {
size: Size;
borderRadius: number;
Expand All @@ -147,9 +149,9 @@ declare class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {
static sizes: typeof Size;
static contentTypes: typeof ContentType;
fadeInAnimation?: Animated.CompositeAnimation;
constructor(props: SkeletonViewProps);
constructor(props: InternalSkeletonViewProps);
componentDidMount(): void;
componentDidUpdate(prevProps: SkeletonViewProps): void;
componentDidUpdate(prevProps: InternalSkeletonViewProps): void;
fade(isFadeIn: boolean, onAnimationEnd?: Animated.EndCallback): Animated.CompositeAnimation;
showChildren: () => void;
getAccessibilityProps: (accessibilityLabel: any) => {
Expand Down
85 changes: 66 additions & 19 deletions src/components/skeletonView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {StyleSheet, Animated, Easing, StyleProp, ViewStyle, AccessibilityProps}
import {BorderRadiuses, Colors, Dividers, Spacings} from '../../style';
import {createShimmerPlaceholder, LinearGradientPackage} from 'optionalDeps';
import View from '../view';
import {Constants, asBaseComponent} from '../../commons/new';
import {Constants, asBaseComponent, BaseComponentInjectedProps, MarginModifiers} from '../../commons/new';
import {extractAccessibilityProps} from '../../commons/modifiers';

const LinearGradient = LinearGradientPackage?.default;
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface SkeletonListProps {
renderEndContent?: () => React.ReactElement | undefined;
}

export interface SkeletonViewProps extends AccessibilityProps {
export interface SkeletonViewProps extends AccessibilityProps, MarginModifiers {
/**
* The content has been loaded, start fading out the skeleton and fading in the content
*/
Expand Down Expand Up @@ -157,7 +157,10 @@ interface SkeletonState {
* @image: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Skeleton/Skeleton.gif?raw=true
* @notes: View requires installing the 'react-native-shimmer-placeholder' and 'react-native-linear-gradient' library
*/
class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {

type InternalSkeletonViewProps = SkeletonViewProps & BaseComponentInjectedProps;

class SkeletonView extends Component<InternalSkeletonViewProps, SkeletonState> {
static defaultProps = {
size: Size.SMALL,
// listProps: {size: Size.SMALL}, TODO: once size is deprecated remove it and add this
Expand All @@ -170,7 +173,7 @@ class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {

fadeInAnimation?: Animated.CompositeAnimation;

constructor(props: SkeletonViewProps) {
constructor(props: InternalSkeletonViewProps) {
super(props);

this.state = {
Expand All @@ -193,7 +196,7 @@ class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {
}
}

componentDidUpdate(prevProps: SkeletonViewProps) {
componentDidUpdate(prevProps: InternalSkeletonViewProps) {
if (this.props.showContent && !prevProps.showContent) {
this.fadeInAnimation?.stop();
this.fade(false, this.showChildren);
Expand Down Expand Up @@ -407,22 +410,66 @@ class SkeletonView extends Component<SkeletonViewProps, SkeletonState> {
return null;
}

const {times, timesKey, renderContent, testID} = this.props;
const {
times,
timesKey,
renderContent,
showContent,
customValue,
contentData,
template,
listProps,
size,
contentType,
hideSeparator,
showLastSeparator,
height,
width,
borderRadius,
circle,
style,
testID,
...others
} = this.props;

const passedProps = {
showContent,
renderContent,
customValue,
contentData,
template,
listProps,
size,
contentType,
hideSeparator,
showLastSeparator,
height,
width,
borderRadius,
circle,
style,
testID
};

if (times) {
return _.times(times, index => {
const key = timesKey ? `${timesKey}-${index}` : `${index}`;
return (
<SkeletonView
{...this.props}
key={key}
testID={`${testID}-${index}`}
renderContent={index === 0 ? renderContent : this.renderNothing}
hideSeparator={this.hideSeparator || (!this.showLastSeparator && index === times - 1)}
times={undefined}
/>
);
});
return (
<View {...others}>
{_.times(times, index => {
const key = timesKey ? `${timesKey}-${index}` : `${index}`;
return (
<SkeletonView
modifiers={{}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we pass null here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It throws a TS error:
Type 'null' is not assignable to type 'ExtractedStyle'.ts(2322)
I can set it to null and add a @ts-expect-error, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Where the modifiers prop typing comes from?
What about passing undefined or is that ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same error for undefined as well.
The type comes from asBaseComponent:
modifiers: ReturnType<typeof Modifiers.generateModifiersStyle>;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it, ok. Approving..

{...passedProps}
key={key}
testID={`${testID}-${index}`}
renderContent={index === 0 ? renderContent : this.renderNothing}
hideSeparator={this.hideSeparator || (!this.showLastSeparator && index === times - 1)}
times={undefined}
/>
);
})}
</View>
);
} else {
return this.renderSkeleton();
}
Expand Down
1 change: 1 addition & 0 deletions src/components/skeletonView/skeletonView.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "SkeletonView",
"category": "layoutsAndTemplates",
"description": "Allows showing a temporary skeleton view while your real view is loading",
"modifiers": ["margin"],
"note": "Requires installing the 'react-native-shimmer-placeholder' and 'react-native-linear-gradient' libraries",
"example": "https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SkeletonViewScreen.tsx",
"images": ["https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Skeleton/Skeleton.gif?raw=true"],
Expand Down