UNPKG

@crossed/ui

Version:

A universal & performant styling library for React Native, Next.js & React

77 lines (76 loc) 3.32 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Form_exports = {}; __export(Form_exports, { Form: () => Form, FormControl: () => FormControl, FormField: () => FormField, FormLabel: () => FormLabel, FormMessage: () => FormMessage }); module.exports = __toCommonJS(Form_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_react_native = require("react-native"); var import_react = require("react"); var import_Label = require("../Label"); var import_YBox = require("../../layout/YBox"); var import_styled = require("@crossed/styled"); const fieldContext = (0, import_react.createContext)({}); const Form = ({ onSubmit, asChild, children, ...props }) => { const propsTmp = { role: "form", ...props }; return asChild && (0, import_react.isValidElement)(children) ? (0, import_react.cloneElement)(children, propsTmp) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_YBox.YBox, { space: "xl", ...propsTmp, children }); }; const FormField = ({ name, ...props }) => { const [inputId, setInputId] = (0, import_react.useState)(); const controlRef = (0, import_react.useRef)(null); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(fieldContext.Provider, { value: { setInputId, inputId, controlRef }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { ...props }) }); }; const FormControl = ({ children }) => { var _a; const { setInputId } = (0, import_react.useContext)(fieldContext); const localId = (0, import_react.useId)(); const id = ((_a = children.props) == null ? void 0 : _a.id) || `form-control${localId}`; (0, import_react.useEffect)(() => { setInputId(id); }, [id, setInputId]); return (0, import_react.isValidElement)(children) ? (0, import_react.cloneElement)(children, { id, "nativeID": id, "data-describedby": `${id}:helper` }) : children; }; const FormLabel = (props) => { const { inputId, controlRef } = (0, import_react.useContext)(fieldContext); const render = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Label.Label, { ...props, htmlFor: inputId }); return import_styled.isWeb ? render : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Pressable, { tabIndex: -1, onPress: () => { var _a; return (_a = controlRef.current) == null ? void 0 : _a.focus(); }, children: render }); }; const FormMessage = () => { return null; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Form, FormControl, FormField, FormLabel, FormMessage }); //# sourceMappingURL=index.js.map