import React, { CSSProperties } from 'react'; import { useNode, UserComponent } from '@craftjs/core'; import { cssPropsToString } from '../../utils/style-helpers'; import { escapeHtml, escapeAttr, scopeId, sanitizeInputType } from '../../utils/escape'; interface InputFieldProps { label?: string; type?: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url'; name?: string; placeholder?: string; required?: boolean; style?: CSSProperties; animation?: string; animationDelay?: string; hideOnDesktop?: boolean; hideOnTablet?: boolean; hideOnMobile?: boolean; } export const InputField: UserComponent = ({ label = 'Label', type = 'text', name = 'field', placeholder = '', required = false, style = {}, }) => { const { connectors: { connect, drag }, selected, } = useNode((node) => ({ selected: node.events.selected, })); return (
{ if (ref) connect(drag(ref)); }} style={{ display: 'flex', flexDirection: 'column', gap: '4px', outline: selected ? '2px solid #3b82f6' : 'none', borderRadius: '4px', ...style, }} > {label && ( )}
); }; /* ---------- Craft config ---------- */ InputField.craft = { displayName: 'Input', props: { label: 'Your Name', type: 'text', name: 'name', placeholder: 'Enter your name', required: false, style: {}, animation: '', animationDelay: '', hideOnDesktop: false, hideOnTablet: false, hideOnMobile: false, }, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, }; /* ---------- HTML export ---------- */ (InputField as any).toHtml = (props: InputFieldProps, _childrenHtml: string, nodeId?: string) => { const wrapStyle = cssPropsToString({ display: 'flex', flexDirection: 'column', gap: '4px', ...props.style, }); const reqAttr = props.required ? ' required' : ''; // Deterministic AND unique id: scoped on the Craft node id so the //