The Open Standard for Generative UI
"use client";
import type { ActionPlan } from "@openuidev/react-lang";
import {
ACTION_STEPS,
defineComponent,
useFormName,
useFormValidation,
useIsStreaming,
useTriggerAction,
} from "@openuidev/react-lang";
import { Button as OpenUIButton } from "../../components/Button";
import { ButtonSchema } from "./schema";
export { ButtonSchema } from "./schema";
const variantMap: Record<string, "primary" | "secondary" | "tertiary"> = {
primary: "primary",
secondary: "secondary",
ghost: "tertiary",
tertiary: "tertiary",
};
export const Button = defineComponent({
name: "Button",
props: ButtonSchema,
description: "Clickable button",
component: ({ props }) => {
const triggerAction = useTriggerAction();
const formName = useFormName();
const isStreaming = useIsStreaming();
const formValidation = useFormValidation();
const label = props.label as string;
return (
<OpenUIButton
variant={variantMap[props.variant as string] || "primary"}
size={(props.size as "extra-small" | "small" | "medium" | "large") || "medium"}
buttonType={props.type as "normal" | "destructive"}
disabled={isStreaming}
onClick={() => {
const action = props.action as ActionPlan | undefined;
const variant = (props.variant as string) || "primary";
// Validate form for primary buttons before firing action
if (formValidation && variant === "primary") {
if (action?.steps) {
// v0.5 ActionPlan: validate if any step is ToAssistant, a mutation, or a custom action
const needsValidation = action.steps.some(
(s) =>
s.type === ACTION_STEPS.ToAssistant ||
s.type === "custom_action" ||
(s.type === ACTION_STEPS.Run && s.refType === "mutation"),
);
if (needsValidation && !formValidation.validateForm()) return;
} else {
// v0.1 legacy or no action — always validate for primary buttons
if (!formValidation.validateForm()) return;
}
}
triggerAction(label, formName, action);
}}
>
{label}
</OpenUIButton>
);
},
});