Skip to content
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

feat(background-image-native): add default image #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BackgroundImageProps } from "../typings/BackgroundImageProps";

export function BackgroundImage(props: BackgroundImageProps<BackgroundImageStyle>): JSX.Element | null {
const styles = flattenStyles(defaultBackgroundImageStyle, props.style);
const { image, name, resizeMode } = props;
const { image, defaultImage, name, resizeMode } = props;
let opacity = Number(props.opacity.toFixed());

if (opacity < 0) {
Expand All @@ -18,15 +18,17 @@ export function BackgroundImage(props: BackgroundImageProps<BackgroundImageStyle
opacity = 1;
}

if (image.status !== ValueStatus.Available || !image.value) {
const renderImage = (image.status === ValueStatus.Available && image.value) || !defaultImage ? image : defaultImage;

if (renderImage.status !== ValueStatus.Available || !renderImage.value) {
return null;
}

const imageStyle = [
StyleSheet.absoluteFill,
typeof image.value === "number"
typeof renderImage.value === "number"
? { width: undefined, height: undefined }
: typeof image.value === "string"
: typeof renderImage.value === "string"
? { width: "100%", height: "100%" }
: undefined,
styles.image,
Expand All @@ -35,7 +37,12 @@ export function BackgroundImage(props: BackgroundImageProps<BackgroundImageStyle

return (
<View style={styles.container} testID={name}>
<Image source={image.value} style={imageStyle} color={styles.image.svgColor} testID={`${name}$image`} />
<Image
source={renderImage.value}
style={imageStyle}
color={styles.image.svgColor}
testID={`${name}$image`}
/>
{props.content}
</View>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<caption>Image</caption>
<description />
</property>
<property key="defaultImage" type="image" required="false">
<caption>Default image</caption>
<description/>
</property>
<property key="resizeMode" type="enumeration" defaultValue="cover">
<caption>Resize mode</caption>
<description>Cover: scale to cover space. Contain: scale to fit in space. Stretch: stretched to fit space. Center: centered and fit in space. For more information see the help page.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface BackgroundImageProps<Style> {
name: string;
style: Style[];
image: DynamicValue<NativeImage>;
defaultImage?: DynamicValue<NativeImage>;
resizeMode: ResizeModeEnum;
opacity: Big;
content?: ReactNode;
Expand All @@ -28,6 +29,7 @@ export interface BackgroundImagePreviewProps {
styleObject?: CSSProperties;
readOnly: boolean;
image: { type: "static"; imageUrl: string; } | { type: "dynamic"; entity: string; } | null;
defaultImage: { type: "static"; imageUrl: string; } | { type: "dynamic"; entity: string; } | null;
resizeMode: ResizeModeEnum;
opacity: number | null;
content: { widgetCount: number; renderer: ComponentType<{ caption?: string }> };
Expand Down