import React, { CSSProperties } from 'react'; import { useNode, Element, UserComponent } from '@craftjs/core'; import { Container } from '../layout/Container'; import { cssPropsToString } from '../../utils/style-helpers'; interface FormContainerProps { action?: string; method?: 'GET' | 'POST'; style?: CSSProperties; children?: React.ReactNode; } export const FormContainer: UserComponent = ({ action = '#', method = 'POST', style = {}, }) => { const { connectors: { connect, drag } } = useNode(); return (
{ if (ref) connect(drag(ref)); }} action={action} method={method} onSubmit={(e) => e.preventDefault()} style={{ padding: '24px', minHeight: '80px', ...style, }} > ); }; /* ---------- Settings panel ---------- */ const FormContainerSettings: React.FC = () => { const { actions: { setProp }, props } = useNode((node) => ({ props: node.data.props as FormContainerProps, })); const bgPresets = ['#ffffff', '#f8fafc', '#f1f5f9', '#18181b', '#0f172a']; return (
setProp((p: FormContainerProps) => { p.action = e.target.value; })} placeholder="https://... or /api/submit" style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 12 }} />
{(['GET', 'POST'] as const).map((m) => ( ))}
{bgPresets.map((c) => (
); }; /* ---------- Craft config ---------- */ FormContainer.craft = { displayName: 'Form', props: { action: '#', method: 'POST', style: { padding: '24px', backgroundColor: '#ffffff', borderRadius: '8px', border: '1px solid #e4e4e7', }, }, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, related: { settings: FormContainerSettings, }, }; /* ---------- HTML export ---------- */ (FormContainer as any).toHtml = (props: FormContainerProps, childrenHtml: string) => { const styleStr = cssPropsToString({ padding: '24px', ...props.style, }); return { html: `
${childrenHtml}
`, }; };