Skip to content

Commit

Permalink
[IOPLT-180] Add the Outlined variant on Badge component (#113)
Browse files Browse the repository at this point in the history
## Short description
This PR implements the badge variant for outlined representation

## List of changes proposed in this pull request
- Changes Badge component

## How to test
Check the example app under `Badges & Tags` screen

<img
src="https://github.com/pagopa/io-app-design-system/assets/3959405/b926af9b-702a-4f9a-a4a1-953dcbd9864b"
height="600"/>
  • Loading branch information
CrisTofani authored Oct 24, 2023
1 parent b3ab701 commit 23279f7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
21 changes: 21 additions & 0 deletions example/src/pages/Badges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ const renderBadge = () => (
<HSpacer size={16} />
</View>
<VSpacer size={16} />
<View style={IOStyles.row}>
<Badge outline text={"Info"} variant="info" />
<HSpacer size={16} />
<Badge outline text={"Warning"} variant="warning" />
<HSpacer size={16} />
<Badge outline text={"Error"} variant="error" />
<HSpacer size={16} />
<Badge outline text={"Success"} variant="success" />
</View>
<VSpacer size={16} />
<View style={IOStyles.row}>
<Badge outline text={"Purple"} variant="purple" />
<HSpacer size={16} />
<Badge outline text={"Light blue"} variant="lightBlue" />
<HSpacer size={16} />
<Badge outline text={"Blue"} variant="blue" />
<HSpacer size={16} />
<Badge outline text={"Turquoise"} variant="turquoise" />
<HSpacer size={16} />
</View>
<VSpacer size={16} />
<View
style={{
backgroundColor: IOColors.bluegrey,
Expand Down
70 changes: 63 additions & 7 deletions src/components/badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useMemo } from "react";
import { Platform, StyleSheet, Text, View } from "react-native";
import {
IOBadgeHSpacing,
Expand All @@ -11,6 +11,7 @@ import { makeFontStyleObject } from "../../utils/fonts";
import { WithTestID } from "../../utils/types";

export type Badge = WithTestID<{
outline?: boolean;
text: string;
variant:
| "default"
Expand All @@ -25,12 +26,12 @@ export type Badge = WithTestID<{
| "contrast";
}>;

type VariantProps = {
foreground: IOColors;
type SolidVariantProps = {
background: IOColors;
foreground: IOColors;
};

const mapVariants: Record<NonNullable<Badge["variant"]>, VariantProps> = {
const mapVariants: Record<NonNullable<Badge["variant"]>, SolidVariantProps> = {
default: {
foreground: "grey-700",
background: "grey-50"
Expand Down Expand Up @@ -73,6 +74,47 @@ const mapVariants: Record<NonNullable<Badge["variant"]>, VariantProps> = {
}
};

type OutlinedVariantProps = {
foreground: IOColors;
background?: never;
};

const mapOutlineVariants: Record<
NonNullable<Badge["variant"]>,
OutlinedVariantProps
> = {
default: {
foreground: "grey-700"
},
info: {
foreground: "info-850"
},
warning: {
foreground: "warning-850"
},
success: {
foreground: "success-850"
},
error: {
foreground: "error-850"
},
purple: {
foreground: "hanPurple-850"
},
lightBlue: {
foreground: "blueIO-850"
},
blue: {
foreground: "blueIO-500"
},
turquoise: {
foreground: "turquoise-850"
},
contrast: {
foreground: "grey-850"
}
};

const styles = StyleSheet.create({
badge: {
flexDirection: "row",
Expand Down Expand Up @@ -106,14 +148,26 @@ const styles = StyleSheet.create({
/**
* Official badge component
*/
export const Badge = ({ text, variant, testID }: Badge) => {
export const Badge = ({ text, outline = false, variant, testID }: Badge) => {
const { isExperimental } = useIOExperimentalDesign();
const { background, foreground } = useMemo(
() => (outline ? mapOutlineVariants : mapVariants)[variant],
[outline, variant]
);

return (
<View
testID={testID}
style={[
styles.badge,
{ backgroundColor: IOColors[mapVariants[variant].background] }
outline
? {
borderWidth: 1,
borderColor: IOColors[foreground]
}
: {
backgroundColor: background ? IOColors[background] : undefined
}
]}
>
<Text
Expand All @@ -122,7 +176,9 @@ export const Badge = ({ text, variant, testID }: Badge) => {
style={[
styles.label,
isExperimental ? styles.labelFont : styles.legacyLabelFont,
{ color: IOColors[mapVariants[variant].foreground] }
{
color: IOColors[foreground]
}
]}
>
{text}
Expand Down

0 comments on commit 23279f7

Please sign in to comment.