Skip to content

Commit 1b7e407

Browse files
Merge pull request #7 from ObservedObserver/dev
feat: badges
2 parents 796ccc4 + a205550 commit 1b7e407

File tree

10 files changed

+83
-8
lines changed

10 files changed

+83
-8
lines changed

pages/Badges.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import streamlit as st
2+
3+
import streamlit_shadcn_ui as ui
4+
5+
st.header("Badges")
6+
7+
ui.badges(badge_list=[("default", "default"), ("secondary", "secondary"), ("outline", "outline"), ("Hello", "destructive"), ("World", "destructive")], class_name="flex gap-2", key="badges1")
8+

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "streamlit-shadcn-ui"
7-
version = "0.1.8"
7+
version = "0.1.9"
88
readme = "README.md"
99
keywords = ["streamlit", "shadcn", "ui", "components"]
1010
description = "Using shadcn components in Streamlit"

streamlit_shadcn_ui/components/packages/frontend/src/App.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import { StRadioGroup } from "./components/streamlit/radioGroup"
2323
import { StTextarea } from "./components/streamlit/textarea";
2424
import { StInput } from "./components/streamlit/input";
2525
import { StSwitch } from "./components/streamlit/switch";
26-
import { StHoverCardContent } from "./components/hoverCard/hoverCardContent";
27-
import { StHoverCardTrigger } from "./components/hoverCard/hoverCardTrigger";
26+
import { StHoverCardContent } from "./components/streamlit/hoverCard/hoverCardContent";
27+
import { StHoverCardTrigger } from "./components/streamlit/hoverCard/hoverCardTrigger";
2828
import { StAlertDialog } from "./components/streamlit/alertDialog";
2929
import { StLinkButton } from "./components/streamlit/linkButton";
30+
import { StBadges } from "./components/streamlit/badge";
3031

3132
const crouter = new ComponentRouter();
3233
crouter.declare("button", StButton);
@@ -49,6 +50,7 @@ crouter.declare("hover_card_content", StHoverCardContent);
4950
crouter.declare("hover_card_trigger", StHoverCardTrigger);
5051
crouter.declare("alert_dialog", StAlertDialog);
5152
crouter.declare("link_button", StLinkButton);
53+
crouter.declare("badges", StBadges);
5254

5355
function App(props: ComponentProps<{comp: string; props: any; [key: string]: any}>) {
5456
const { args, width, disabled, theme } = props;
@@ -63,5 +65,4 @@ function App(props: ComponentProps<{comp: string; props: any; [key: string]: any
6365
return crouter.render(args.comp, container, args.props);
6466
}
6567

66-
const WP = withStreamlitConnection(App);
67-
export default WP;
68+
export const StApp = withStreamlitConnection(App);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Badge, BadgeProps } from "@/components/ui/badge";
2+
import { forwardRef } from "react";
3+
4+
interface StBadgesProps {
5+
badges: { text: string; variant: BadgeProps['variant'] }[];
6+
className?: string;
7+
}
8+
export const StBadges = forwardRef<HTMLDivElement, StBadgesProps>((props, ref) => {
9+
const { badges, className } = props;
10+
return <div className={className} ref={ref}>
11+
{
12+
badges.map((badge, index) => {
13+
return <Badge key={`${badge}-${index}`} variant={badge.variant}>{badge.text}</Badge>
14+
})
15+
}
16+
</div>
17+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as React from "react"
2+
import { cva, type VariantProps } from "class-variance-authority"
3+
4+
import { cn } from "@/lib/utils"
5+
6+
const badgeVariants = cva(
7+
"inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
8+
{
9+
variants: {
10+
variant: {
11+
default:
12+
"border-transparent bg-primary text-primary-foreground shadow hover:bg-primary/80",
13+
secondary:
14+
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
15+
destructive:
16+
"border-transparent bg-destructive text-destructive-foreground shadow hover:bg-destructive/80",
17+
outline: "text-foreground",
18+
},
19+
},
20+
defaultVariants: {
21+
variant: "default",
22+
},
23+
}
24+
)
25+
26+
export interface BadgeProps
27+
extends React.HTMLAttributes<HTMLDivElement>,
28+
VariantProps<typeof badgeVariants> {}
29+
30+
function Badge({ className, variant, ...props }: BadgeProps) {
31+
return (
32+
<div className={cn(badgeVariants({ variant }), className)} {...props} />
33+
)
34+
}
35+
36+
export { Badge, badgeVariants }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ReactDOM from "react-dom/client";
2-
import App from "./App.tsx";
2+
import { StApp } from "./App.tsx";
33
import "./index.css";
44

5-
ReactDOM.createRoot(document.getElementById("root")!).render(<App />);
5+
ReactDOM.createRoot(document.getElementById("root")!).render(<StApp />);

streamlit_shadcn_ui/py_components/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
from .radio_group import radio_group
1414
from .hover_card import hover_card
1515
from .alert_dialog import alert_dialog
16-
from .link_button import link_button
16+
from .link_button import link_button
17+
from .badges import badges
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .utils import declare_component
2+
from typing import Tuple
3+
4+
_RELEASE = True
5+
6+
_component_func = declare_component("badges", release=_RELEASE)
7+
8+
def badges(badge_list: list[Tuple[str, str]], class_name: str = None, key = None):
9+
bl = [{"text": b[0], "variant": b[1] } for b in badge_list]
10+
props = {"badges":bl , "className": class_name}
11+
clicked = _component_func(comp="badges", props=props, key=key, default=False)
12+
return clicked

0 commit comments

Comments
 (0)