Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/calm-lamps-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ensembleui/react-kitchen-sink": patch
"@ensembleui/react-runtime": patch
---

support custom widget in label of Button widget
7 changes: 6 additions & 1 deletion apps/kitchen-sink/src/ensemble/screens/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ View:
names: heading-3
text: Execute Conditional Action
- Button:
label: Tap to pick an image
body:
Text:
text: Tap to pick an image
styles:
fontFamily: fantasy
color: green
onTap:
pickFiles:
allowedExtensions: ["jpg", "png", "pdf", "docs"]
Expand Down
16 changes: 13 additions & 3 deletions packages/runtime/src/widgets/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EnsembleAction, Expression } from "@ensembleui/react-framework";
import { useRegisterBindings } from "@ensembleui/react-framework";
import { unwrapWidget, useRegisterBindings } from "@ensembleui/react-framework";
import { Button as AntButton, Form as AntForm } from "antd";
import {
type MouseEvent,
Expand All @@ -12,11 +12,13 @@ import { WidgetRegistry } from "../registry";
import type { EnsembleWidgetProps, IconProps } from "../shared/types";
import { useEnsembleAction } from "../runtime/hooks/useEnsembleAction";
import { Icon } from "./Icon";
import { EnsembleRuntime } from "../runtime";
import { isString } from "lodash-es";

const widgetName = "Button";

export type ButtonProps = {
label: Expression<string>;
label: Expression<string> | { [key: string]: unknown };
onTap?: EnsembleAction;
submitForm?: boolean;
startingIcon?: IconProps;
Expand Down Expand Up @@ -58,10 +60,18 @@ export const Button: React.FC<ButtonProps> = ({ id, onTap, ...rest }) => {
}
}, [values?.loading]);

const label = useMemo(() => {
const rawLabel = values?.label;
if (!rawLabel) return null;
if (isString(rawLabel)) return rawLabel;
return EnsembleRuntime.render([unwrapWidget(rawLabel)]);
}, [values?.label]);

const ButtonComponent = useMemo(() => {
return (
<AntButton
disabled={values?.disabled ?? false}
className={values?.styles?.names}
htmlType={values?.submitForm === true ? "submit" : "button"}
loading={Boolean(loading)}
onClick={onClickCallback}
Expand All @@ -82,7 +92,7 @@ export const Button: React.FC<ButtonProps> = ({ id, onTap, ...rest }) => {
<Icon {...values.startingIcon} />
) : null}

{!loading && <>{values?.label}</>}
{!loading && <>{label}</>}

{!loading && values?.endingIcon ? (
<Icon {...values.endingIcon} />
Expand Down