UNPKG

@tachui/primitives

Version:

Basic UI components for tachUI framework

486 lines (485 loc) โ€ข 14.8 kB
import { D as x, b as k, d as B, c as T, H as b, L as P, S as y, a as I, V as j, Z as S, e as L, w as A } from "./Divider-DWgDKavU.js"; import { E as V, T as $, a as C, b as H, c as E } from "./Text-DYyFBJ6i.js"; import { E as q, d as U, e as D, f as N, g as W, h as _, i as M, H as R, I as Z, b as z, a as G, c as J } from "./HTML-aWcvQ0VC.js"; import { B as Q, a as X, E as Y, h as ee, c as te, L as oe, b as ne, P as ie, i as re, j as ae, T as se, e as ce, f as le, g as pe, d as de } from "./Picker-DrZ7ibdt.js"; import { B as ue, a as me, b as he, c as fe, d as ve, e as we, f as xe } from "./BasicInput-CWFHw9rL.js"; function d(e) { return e && typeof e == "object" && "value" in e; } class n extends Error { constructor(t, o) { super(t), this.context = o, this.name = "TachUIValidationError"; } getFormattedMessage() { const { component: t, suggestion: o, example: i, documentation: s, package: p } = this.context; let c = `โŒ ${p ? `[@tachui/${p}]` : "[Primitives]"} ${t} Component Error: ${this.message} `; return o && (c += ` ๐Ÿ’ก Suggestion: ${o} `), i && (c += ` โŒ Wrong: ${i.wrong}`, c += ` โœ… Correct: ${i.correct} `), s && (c += ` ๐Ÿ“š Documentation: ${s}`), c; } } let l = { enabled: process.env.NODE_ENV !== "production", errorLevel: "error" }; function a() { return l.enabled && process.env.NODE_ENV !== "production"; } const r = { // Text Component validateText(e) { if (!a()) return; if (e.length === 0) throw new n( "Text component requires a content parameter", { component: "Text", suggestion: 'Add a content parameter: Text("Hello World")', documentation: "https://docs.tachui.dev/components/text", example: { wrong: "Text()", correct: 'Text("Hello World")' } } ); const [t] = e; if (t == null) throw new n( "Text content cannot be undefined or null", { component: "Text", suggestion: "Provide a valid string, function, or Signal", example: { wrong: "Text(null)", correct: 'Text("Hello World")' } } ); const o = typeof t; if (!(o === "string" || o === "function" || d(t))) throw new n( `Text content must be a string, function, or Signal. Received: ${o}`, { component: "Text", suggestion: "Use a string literal, function, or reactive signal", example: { wrong: "Text(123)", correct: 'Text("Hello World")' } } ); }, // Button Component validateButton(e) { if (!a()) return; if (e.length === 0) throw new n( "Button component requires a title parameter", { component: "Button", suggestion: 'Add a title parameter: Button("Click me", () => {})', documentation: "https://docs.tachui.dev/components/button", example: { wrong: "Button()", correct: 'Button("Click me", () => {})' } } ); const [t, o] = e; if (t == null) throw new n( "Button title cannot be undefined or null", { component: "Button", suggestion: "Provide a valid string, function, or Signal", example: { wrong: "Button(null)", correct: 'Button("Click me")' } } ); const i = typeof t; if (!(i === "string" || i === "function" || d(t))) throw new n( `Button title must be a string, function, or Signal. Received: ${i}`, { component: "Button", suggestion: "Use a string literal, function, or reactive signal", example: { wrong: "Button(123)", correct: 'Button("Click me")' } } ); o === void 0 && e.length === 1 && l.errorLevel !== "error" && console.warn( "โš ๏ธ Button without action may not be interactive. Consider adding an action parameter." ); }, // Image Component validateImage(e) { if (!a()) return; if (e.length === 0) throw new n( "Image component requires a source parameter", { component: "Image", suggestion: 'Add a source parameter: Image({ source: "image.jpg" })', documentation: "https://docs.tachui.dev/components/image", example: { wrong: "Image()", correct: 'Image({ source: "image.jpg" })' } } ); const [t] = e; if (!t || typeof t != "object") throw new n("Image requires a props object", { component: "Image", suggestion: "Pass a props object with source property", example: { wrong: 'Image("image.jpg")', correct: 'Image({ source: "image.jpg" })' } }); if (!t.source) throw new n("Image source property is required", { component: "Image", suggestion: "Provide a valid image source URL or path", example: { wrong: 'Image({ alt: "description" })', correct: 'Image({ source: "image.jpg", alt: "description" })' } }); }, // Toggle Component validateToggle(e) { if (!a()) return; if (e.length === 0) throw new n( "Toggle component requires an isOn parameter", { component: "Toggle", suggestion: "Add an isOn parameter: Toggle({ isOn: signal })", documentation: "https://docs.tachui.dev/components/toggle", example: { wrong: "Toggle()", correct: "Toggle({ isOn: isEnabled })" } } ); const [t] = e; if (!t || typeof t != "object") throw new n("Toggle requires a props object", { component: "Toggle", suggestion: "Pass a props object with isOn property", example: { wrong: "Toggle(true)", correct: "Toggle({ isOn: true })" } }); if (t.isOn === void 0) throw new n("Toggle isOn property is required", { component: "Toggle", suggestion: "Provide a boolean value or Signal for isOn", example: { wrong: "Toggle({ onToggle: handler })", correct: "Toggle({ isOn: true, onToggle: handler })" } }); }, // Picker Component validatePicker(e) { if (!a()) return; if (e.length === 0) throw new n( "Picker component requires a props object", { component: "Picker", suggestion: 'Add props: Picker({ options: ["A", "B"], selected: signal })', documentation: "https://docs.tachui.dev/components/picker", example: { wrong: "Picker()", correct: 'Picker({ options: ["A", "B"], selected: selectedValue })' } } ); const [t] = e; if (!t || typeof t != "object") throw new n("Picker requires a props object", { component: "Picker", suggestion: "Pass a props object with options and selected properties", example: { wrong: 'Picker(["A", "B"])', correct: 'Picker({ options: ["A", "B"], selected: selectedValue })' } }); const o = t; if (!Array.isArray(o.options)) throw new n("Picker options must be an array", { component: "Picker", suggestion: "Provide an array of options", example: { wrong: 'Picker({ options: "invalid" })', correct: 'Picker({ options: ["Option A", "Option B"] })' } }); if (o.selected === void 0) throw new n("Picker selected property is required", { component: "Picker", suggestion: "Provide a selected value or Signal", example: { wrong: 'Picker({ options: ["A", "B"] })', correct: 'Picker({ options: ["A", "B"], selected: selectedValue })' } }); }, // Layout Components (VStack, HStack, ZStack) validateStack(e, t) { if (!a()) return; if (t.length === 0) throw new n( `${e} component requires a props object with children array`, { component: e, suggestion: `Add children: ${e}({ children: [Text("Hello")] })`, documentation: "https://docs.tachui.dev/components/layout", example: { wrong: `${e}()`, correct: `${e}({ children: [Text("Hello")] })` } } ); const [o] = t; if (!o || typeof o != "object") throw new n( `${e} requires a props object`, { component: e, suggestion: "Pass a props object with children array", example: { wrong: `${e}("invalid")`, correct: `${e}({ children: [Text("Hello")] })` } } ); const i = o; if (!Array.isArray(i.children)) throw new n( `${e} children must be an array`, { component: e, suggestion: "Provide an array of components", example: { wrong: `${e}({ children: Text("Hello") })`, correct: `${e}({ children: [Text("Hello")] })` } } ); }, // Link Component validateLink(e) { if (!a()) return; if (e.length === 0) throw new n( "Link component requires a props object with href", { component: "Link", suggestion: 'Add href: Link({ href: "/path", children: [Text("Click here")] })', documentation: "https://docs.tachui.dev/components/link", example: { wrong: "Link()", correct: 'Link({ href: "/about", children: [Text("About")] })' } } ); const [t] = e; if (!t || typeof t != "object") throw new n("Link requires a props object", { component: "Link", suggestion: "Pass a props object with href property", example: { wrong: 'Link("/path")', correct: 'Link({ href: "/path", children: [Text("Link")] })' } }); if (!t.href) throw new n("Link href property is required", { component: "Link", suggestion: "Provide a valid URL or path for href", example: { wrong: 'Link({ children: [Text("Link")] })', correct: 'Link({ href: "/about", children: [Text("About")] })' } }); }, // BasicForm Component validateBasicForm(e) { if (!a()) return; if (e.length === 0) throw new n( "BasicForm component requires children array", { component: "BasicForm", suggestion: 'Add children: BasicForm([BasicInput({}), Button("Submit")])', documentation: "https://docs.tachui.dev/components/forms", example: { wrong: "BasicForm()", correct: 'BasicForm([BasicInput({}), Button("Submit")])' } } ); const [t, o] = e; if (!Array.isArray(t)) throw new n("BasicForm children must be an array", { component: "BasicForm", suggestion: "Provide an array of form components", example: { wrong: "BasicForm(BasicInput({}))", correct: 'BasicForm([BasicInput({}), Button("Submit")])' } }); }, // BasicInput Component validateBasicInput(e) { if (!a()) return; const [t] = e; if (t && typeof t == "object") { const o = t; if (o.type) { const i = [ "text", "email", "password", "number", "tel", "url", "search" ]; if (!i.includes(o.type)) throw new n( `Invalid input type: ${o.type}`, { component: "BasicInput", suggestion: `Use one of: ${i.join(", ")}`, example: { wrong: 'BasicInput({ type: "invalid" })', correct: 'BasicInput({ type: "email" })' } } ); } } } }; function g(e, t, o) { return function(...s) { return o(s), e.apply(this, s); }; } function u(e) { l = { ...l, ...e }; } function m() { return { ...l }; } const f = { // Direct validation methods validateText: r.validateText, validateButton: r.validateButton, validateImage: r.validateImage, validateToggle: r.validateToggle, validatePicker: r.validatePicker, validateStack: r.validateStack, validateLink: r.validateLink, validateBasicForm: r.validateBasicForm, validateBasicInput: r.validateBasicInput, // Configuration configure: u, getConfig: m, // Utilities isValidationEnabled: a, withComponentValidation: g, // Test method test: () => { console.group("๐Ÿงช Primitives Validation Test"); try { console.info("Testing Text validation..."), r.validateText(["Hello World"]), console.info("โœ… Text validation passed"), console.info("Testing Button validation..."), r.validateButton(["Click me", () => { }]), console.info("โœ… Button validation passed"), console.info("Testing Image validation..."), r.validateImage([{ source: "image.jpg" }]), console.info("โœ… Image validation passed"), console.info("Testing Stack validation..."), r.validateStack("VStack", [ { children: ["Hello"] } ]), console.info("โœ… Stack validation passed"), console.info("โœ… All primitives validation tests passed"); } catch (e) { console.error("โŒ Primitives validation test failed:", e); } console.groupEnd(); } }; export { ue as BasicForm, me as BasicFormImplementation, he as BasicFormStyles, fe as BasicFormValidation, ve as BasicInput, we as BasicInputStyles, xe as BasicInputUtils, Q as Button, X as ButtonStyles, x as Divider, k as DividerComponent, B as DividerStyles, T as DividerUtils, Y as EnhancedButton, q as EnhancedImage, ee as EnhancedPicker, V as EnhancedText, te as EnhancedToggle, U as H1, D as H2, N as H3, W as H4, _ as H5, M as H6, b as HStack, R as HTML, Z as Image, z as ImageContentModes, G as ImageStates, J as ImageUtils, P as LayoutComponent, oe as Link, ne as LinkUtils, ie as Picker, re as PickerStyles, ae as PickerUtils, r as PrimitiveComponentValidation, f as PrimitivesValidationUtils, y as Spacer, I as SpacerComponent, n as TachUIValidationError, $ as Text, C as TextFormat, H as TextStyles, se as Toggle, ce as ToggleStyles, le as ToggleUtils, pe as ToggleWithLabel, E as Typography, j as VStack, S as ZStack, u as configurePrimitivesValidation, de as defaultButtonTheme, L as defaultDividerTheme, m as getPrimitivesValidationConfig, g as withComponentValidation, A as withModifiers };