UNPKG

@ducor/react

Version:

admin template ui interface

37 lines (36 loc) 1.97 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { uuidv4 } from "@ducor/helpers"; import { useMemo } from "react"; import { useField } from "../../hook"; import { useFieldName } from "./arrayField"; var Checkbox = function (_a) { var label = _a.label, checked = _a.checked, name = _a.name, defaultValue = _a.defaultValue, props = __rest(_a, ["label", "checked", "name", "defaultValue"]); var fieldName = useFieldName(name).fieldName; if (typeof fieldName !== "string") { return null; } var id = uuidv4(); var _b = useField(fieldName), error = _b.error, processing = _b.processing, setValue = _b.setValue; var onChange = function (e) { var value = e.target.value; setValue(name, value); }; useMemo(function () { if (defaultValue !== undefined) { setValue(name, defaultValue); } }, [defaultValue]); return (_jsxs("div", { className: 'flex items-center mb-2', children: [_jsx("input", { type: 'checkbox', name: fieldName, id: id, checked: checked, onChange: onChange, className: 'mr-2', disabled: processing }), _jsx("label", { htmlFor: id, className: 'text-sm', children: label }), typeof error === "string" ? (_jsx("p", { className: 'text-red-500 text-sm', children: error })) : Array.isArray(error) ? (error.map(function (err, index) { return (_jsx("p", { className: 'text-red-500 text-sm', children: err }, index)); })) : null] })); }; export default Checkbox;