Skip to content

Commit

Permalink
Merge pull request #3 from ObservedObserver/dev
Browse files Browse the repository at this point in the history
feat: button reset value
  • Loading branch information
ObservedObserver authored Nov 23, 2023
2 parents d9f3d1a + 7801af5 commit 39509ad
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 15 deletions.
16 changes: 16 additions & 0 deletions Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

from streamlit_shadcn_ui import slider, input, textarea, radio_group, switch

ui_result = ui.button("Button", key="btn")
st.write("UI Button Clicked:", ui_result)

st.write("before", st.session_state)

st_result = st.button("Button", key="btn2")
st.write("ST Button Clicked:", st_result)


st.write("after", st.session_state)


# Slider Component
slider_value = slider(default_value=[20], min_value=0, max_value=100, step=2, label="Select a Range", key="slider1")
st.write("Slider Value:", slider_value)
Expand All @@ -31,3 +43,7 @@
# Switch Component
switch_value = switch(default_checked=True, label="Toggle Switch", key="switch1")
st.write("Switch is On:", switch_value)

st.header("Button Component")
trigger_btn = ui.button(text="Trigger Button", key="trigger_btn")
ui.alert_dialog(show=trigger_btn, title="Alert Dialog", description="This is an alert dialog", confirm_label="OK", cancel_label="Cancel", key="alert_dialog1")
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ Using shadcn-ui components in streamlit
pip install streamlit-shadcn-ui
```

example:
```py
import streamlit_shadcn_ui as ui
trigger_btn = ui.button(text="Trigger Button", key="trigger_btn")

ui.alert_dialog(show=trigger_btn, title="Alert Dialog", description="This is an alert dialog", confirm_label="OK", cancel_label="Cancel", key="alert_dialog1")

```

## Components

Check docs and compoenent examples in [![Streamlit App](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://shadcn.streamlit.app/)
Expand All @@ -21,7 +30,6 @@ Check docs and compoenent examples in [![Streamlit App](https://static.streamlit
+ [x] select
+ [x] tabs
+ [x] card
+ [x] containers
+ [x] avatar
+ [x] date_picker
+ [ ] date_range_picker
Expand All @@ -30,4 +38,6 @@ Check docs and compoenent examples in [![Streamlit App](https://static.streamlit
+ [x] slider
+ [x] textarea
+ [x] switch
+ [x] radio_group
+ [x] radio_group
+ [x] alert_dialog
+ [x] hover_card
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "streamlit-shadcn-ui"
version = "0.1.5"
version = "0.1.6"
readme = "README.md"
keywords = ["streamlit", "shadcn", "ui", "components"]
description = "Using shadcn components in Streamlit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-hover-card": "^1.0.7",
Expand Down
3 changes: 2 additions & 1 deletion streamlit_shadcn_ui/components/packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { StInput } from "./components/streamlit/input";
import { StSwitch } from "./components/streamlit/switch";
import { StHoverCardContent } from "./components/hoverCard/hoverCardContent";
import { StHoverCardTrigger } from "./components/hoverCard/hoverCardTrigger";

import { StAlertDialog } from "./components/streamlit/alertDialog";

const crouter = new ComponentRouter();
crouter.declare("button", StButton);
Expand All @@ -46,6 +46,7 @@ crouter.declare("input", StInput);
crouter.declare("switch", StSwitch);
crouter.declare("hover_card_content", StHoverCardContent);
crouter.declare("hover_card_trigger", StHoverCardTrigger);
crouter.declare("alert_dialog", StAlertDialog);

function App(props: ComponentProps<{comp: string; props: any; [key: string]: any}>) {
const { args, width, disabled, theme } = props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { useBodyStyle } from "@/hooks/useBodyStyle";
import { forwardRef, useEffect } from "react";
import { Streamlit } from "streamlit-component-lib";

interface StAlertDialogProps {
title?: string;
description?: string;
confirmLabel?: string;
cancelLabel?: string;
}
export const StAlertDialog = forwardRef<HTMLDivElement, StAlertDialogProps>(
(props, ref) => {
const {
title,
description,
confirmLabel = "Confirm",
cancelLabel = "Cancel",
} = props;
useEffect(() => {
if (ref && typeof ref !== "function") {
Streamlit.setFrameHeight(ref.current.offsetHeight + 20);
// Streamlit.setFrameHeight(1000);
}
});
useBodyStyle(
"body { background-color: transparent !important; padding-right: 0.5em !important; }"
);
const handleAction = (confirm: boolean) => {
Streamlit.setComponentValue({
confirm,
open: false,
});
};
return (
<AlertDialog open={true}>
<AlertDialogTrigger asChild>
<Button variant="outline">Show Dialog</Button>
</AlertDialogTrigger>
<AlertDialogContent ref={ref} className="m-1">
<AlertDialogHeader>
<AlertDialogTitle>{title}</AlertDialogTitle>
<AlertDialogDescription>
{description}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel
onClick={() => {
handleAction(false);
}}
>
{cancelLabel}
</AlertDialogCancel>
<AlertDialogAction
onClick={() => {
handleAction(true);
}}
>
{confirmLabel}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
);
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { Button, ButtonProps } from "@/components/ui/button";
import { forwardRef } from "react";
import { forwardRef, useCallback, useEffect, useRef } from "react";
import { Streamlit } from "streamlit-component-lib";

interface StButtonProps {
text?: string;
variant?: ButtonProps['variant'];
disabled?: boolean;
onClick?: () => void;
}
export const StButton = forwardRef<HTMLButtonElement, StButtonProps>((props: StButtonProps, ref) => {
const { text, disabled, onClick, variant } = props;
const { text, disabled, variant } = props;

const queue = useRef<(() => void)[]>([]);

const clickHandler = useCallback(() => {
queue.current = [];
Streamlit.setComponentValue(true);
queue.current.push(() => {})
queue.current.push(() => {
Streamlit.setComponentValue(false);
})
}, [])

useEffect(() => {
if (queue.current.length > 0) {
const fn = queue.current.shift();
fn?.();
}
})

return (
<Button variant={variant} ref={ref} disabled={disabled} onClick={onClick}>
<Button variant={variant} ref={ref} disabled={disabled} onClick={clickHandler}>
{text}
</Button>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const StDatePickerTrigger = forwardRef<
}
}, [open]);
useBodyStyle("body { padding-right: 0.5em !important; }");
console.log(label);

return (
<Popover>
<PopoverTrigger asChild>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as React from "react"
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"

import { cn } from "@/lib/utils"
import { buttonVariants } from "@/components/ui/button"

const AlertDialog = AlertDialogPrimitive.Root

const AlertDialogTrigger = AlertDialogPrimitive.Trigger

const AlertDialogPortal = AlertDialogPrimitive.Portal

const AlertDialogOverlay = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName

const AlertDialogContent = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
>(({ className, ...props }, ref) => (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
/>
</AlertDialogPortal>
))
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName

const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-2 text-center sm:text-left",
className
)}
{...props}
/>
)
AlertDialogHeader.displayName = "AlertDialogHeader"

const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
AlertDialogFooter.displayName = "AlertDialogFooter"

const AlertDialogTitle = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Title
ref={ref}
className={cn("text-lg font-semibold", className)}
{...props}
/>
))
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName

const AlertDialogDescription = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
AlertDialogDescription.displayName =
AlertDialogPrimitive.Description.displayName

const AlertDialogAction = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Action>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Action>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Action
ref={ref}
className={cn(buttonVariants(), className)}
{...props}
/>
))
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName

const AlertDialogCancel = React.forwardRef<
React.ElementRef<typeof AlertDialogPrimitive.Cancel>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Cancel>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Cancel
ref={ref}
className={cn(
buttonVariants({ variant: "outline" }),
"mt-2 sm:mt-0",
className
)}
{...props}
/>
))
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName

export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
}
Loading

0 comments on commit 39509ad

Please sign in to comment.