import React, { CSSProperties } from 'react'; import { useNode, UserComponent } from '@craftjs/core'; import { cssPropsToString } from '../../utils/style-helpers'; interface SearchBarProps { placeholder?: string; buttonText?: string; showButton?: boolean; style?: CSSProperties; } export const SearchBar: UserComponent = ({ placeholder = 'Search...', buttonText = 'Search', showButton = true, style = {}, }) => { const { connectors: { connect, drag }, selected, } = useNode((node) => ({ selected: node.events.selected, })); return (
{ if (ref) connect(drag(ref)); }} role="search" onSubmit={(e) => e.preventDefault()} style={{ display: 'flex', alignItems: 'center', maxWidth: '560px', outline: selected ? '2px solid #3b82f6' : 'none', ...style, }} >
{showButton && ( )} ); }; /* ---------- Settings panel ---------- */ const SearchBarSettings: React.FC = () => { const { actions: { setProp }, props } = useNode((node) => ({ props: node.data.props as SearchBarProps, })); const labelStyle: CSSProperties = { fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }; const inputStyle: CSSProperties = { width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 12, }; return (
{/* Placeholder */}
setProp((p: SearchBarProps) => { p.placeholder = e.target.value; })} placeholder="Search..." style={inputStyle} />
{/* Show Button */}
{/* Button Text */} {props.showButton !== false && (
setProp((p: SearchBarProps) => { p.buttonText = e.target.value; })} placeholder="Search" style={inputStyle} />
)}
); }; /* ---------- Craft config ---------- */ SearchBar.craft = { displayName: 'Search Bar', props: { placeholder: 'Search...', buttonText: 'Search', showButton: true, style: {}, }, rules: { canDrag: () => true, canMoveIn: () => false, canMoveOut: () => true, }, related: { settings: SearchBarSettings, }, }; /* ---------- HTML export ---------- */ (SearchBar as any).toHtml = (props: SearchBarProps, _childrenHtml: string) => { const esc = (s: string) => s.replace(//g, '>').replace(/"/g, '"'); const { placeholder = 'Search...', buttonText = 'Search', showButton = true, style = {}, } = props; const formStyle = cssPropsToString({ display: 'flex', alignItems: 'center', maxWidth: '560px', ...style, }); const inputStyleStr = `width:100%;padding:12px 16px 12px 40px;font-size:15px;font-family:Inter,sans-serif;border:1px solid #d1d5db;border-radius:${showButton ? '8px 0 0 8px' : '8px'};background-color:#ffffff;color:#1f2937;outline:none;box-sizing:border-box`; const btnHtml = showButton ? `` : ''; return { html: `
${btnHtml}
`, }; };