@wener/console
Version:
Base console UI toolkit
281 lines (280 loc) • 11.1 kB
JavaScript
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length)
len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++)
arr2[i] = arr[i];
return arr2;
}
function _array_with_holes(arr) {
if (Array.isArray(arr))
return arr;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
}
else {
obj[key] = value;
}
return obj;
}
function _iterable_to_array_limit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null)
return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i)
break;
}
}
catch (err) {
_d = true;
_e = err;
}
finally {
try {
if (!_n && _i["return"] != null)
_i["return"]();
}
finally {
if (_d)
throw _e;
}
}
return _arr;
}
function _non_iterable_rest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
}
else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _object_without_properties(source, excluded) {
if (source == null)
return {};
var target = {}, sourceKeys, key, i;
if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
sourceKeys = Reflect.ownKeys(source);
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0)
continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key))
continue;
target[key] = source[key];
}
return target;
}
target = _object_without_properties_loose(source, excluded);
if (Object.getOwnPropertySymbols) {
sourceKeys = Object.getOwnPropertySymbols(source);
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0)
continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key))
continue;
target[key] = source[key];
}
}
return target;
}
function _object_without_properties_loose(source, excluded) {
if (source == null)
return {};
var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0)
continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key))
continue;
target[key] = source[key];
}
return target;
}
function _sliced_to_array(arr, i) {
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o)
return;
if (typeof o === "string")
return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor)
n = o.constructor.name;
if (n === "Map" || n === "Set")
return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
return _array_like_to_array(o, minLen);
}
import React, { createContext, useContext, useId, useRef, useState } from "react";
import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { Errors } from "@wener/utils";
import { DevOnly } from "../components/DevOnly.js";
import { FunctionButton } from "../components/FunctionButton.js";
import { showErrorToast } from "../toast/index.js";
import { cn } from "../utils/cn.js";
import { getDirtyFields } from "./getDirtyFields.js";
import { getFieldErrors } from "./getFieldErrors.js";
var Context = /*#__PURE__*/ createContext(undefined);
(function (ReactHookForm) {
ReactHookForm.handleInvalid = _handleInvalid;
ReactHookForm.Root = function (props) {
return null;
};
function Provider(_0) {
var children = _0.children, onSubmit = _0.onSubmit, props = _object_without_properties(_0, [
"children",
"onSubmit"
]);
var id = useId();
var controller = useForm(_object_spread({
mode: "onBlur"
}, props));
var ref = useRef({});
ref.current.onValid = onSubmit;
var _useState = _sliced_to_array(useState(function () {
return {
id: id,
onValid: function (data) {
var _ref_current_onValid, _ref_current;
(_ref_current_onValid = (_ref_current = ref.current).onValid) === null || _ref_current_onValid === void 0 ? void 0 : _ref_current_onValid.call(_ref_current, data);
},
onInvalid: function (errors) {
(ref.current.onInvalid || ReactHookForm.handleInvalid)(errors);
}
};
}), 1), ctx = _useState[0];
return /*#__PURE__*/ React.createElement(FormProvider, controller, /*#__PURE__*/ React.createElement(Context.Provider, {
value: ctx
}, children));
}
ReactHookForm.Provider = Provider;
ReactHookForm.Form = function (_0) {
var children = _0.children, props = _object_without_properties(_0, [
"children"
]);
var handleSubmit = useFormContext().handleSubmit;
var _Errors_BadRequest_require = Errors.BadRequest.require(useContext(Context), "ReactHookForm: context not exists"), onValid = _Errors_BadRequest_require.onValid, onInvalid = _Errors_BadRequest_require.onInvalid;
return /*#__PURE__*/ React.createElement("form", _object_spread_props(_object_spread({}, props), {
onSubmit: handleSubmit(onValid, onInvalid || _handleInvalid),
autoComplete: "off",
"aria-autocomplete": "none"
}), children);
};
ReactHookForm.SubmitButton = function (_0) {
var className = _0.className, _0_dirty = _0.dirty, dirty = _0_dirty === void 0 ? true : _0_dirty, children = _0.children, props = _object_without_properties(_0, [
"className",
"dirty",
"children"
]);
var context = useFormContext();
if (!context) {
return null;
}
var formState = (context || {}).formState;
var isSubmitting = formState.isSubmitting, isDirty = formState.isDirty, disabled = formState.disabled;
var mute = disabled || isSubmitting;
if (dirty) {
mute || (mute = !isDirty); // require dirty
}
return /*#__PURE__*/ React.createElement(FunctionButton.Submit, _object_spread({
className: cn("btn-primary", className),
disabled: mute,
loading: isSubmitting
}, props), children);
};
var _DebugButton = function (props) {
var log = useFormDebug().log;
return /*#__PURE__*/ React.createElement(FunctionButton.Button, _object_spread({
onClick: log
}, props), "DEBUG");
};
ReactHookForm.DebugButton = function (props) {
return /*#__PURE__*/ React.createElement(DevOnly, null, /*#__PURE__*/ React.createElement(_DebugButton, props));
};
})(ReactHookForm || (ReactHookForm = {}));
function _handleInvalid(errors) {
console.error("[FormInvalid]", errors);
var msg = getFieldErrors(errors).map(function (v) {
return "".concat(v.path, ": ").concat(v.error.message);
}).join("; ");
showErrorToast("\u8868\u5355\u9A8C\u8BC1\u5931\u8D25: ".concat(msg));
}
function useFormDebug() {
var context = useFormContext();
var formState = context.formState, getValues = context.getValues, control = context.control;
return {
log: function () {
var defaultValues = formState.defaultValues, // name,
dirtyFields = formState.dirtyFields, disabled = formState.disabled, errors = formState.errors, isDirty = formState.isDirty, isLoading = formState.isLoading, isSubmitSuccessful = formState.isSubmitSuccessful, isSubmitted = formState.isSubmitted, isSubmitting = formState.isSubmitting, isValid = formState.isValid, isValidating = formState.isValidating, submitCount = formState.submitCount, touchedFields = formState.touchedFields, validatingFields = formState.validatingFields;
console.log("[FormDebug]", "dirty", getDirtyFields(context), "value", getValues(), "formState", {
defaultValues: defaultValues,
dirtyFields: dirtyFields,
disabled: disabled,
errors: errors,
isDirty: isDirty,
isLoading: isLoading,
isSubmitSuccessful: isSubmitSuccessful,
isSubmitted: isSubmitted,
isSubmitting: isSubmitting,
isValid: isValid,
isValidating: isValidating,
submitCount: submitCount,
touchedFields: touchedFields,
validatingFields: validatingFields
}, "props", control._options);
}
};
}
export var ReactHookForm;