123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import {
- isObject,
- isPromise,
- isFunction,
- getRootScrollTop,
- setRootScrollTop
- } from "../utils/index.mjs";
- function isEmptyValue(value) {
- if (Array.isArray(value)) {
- return !value.length;
- }
- if (value === 0) {
- return false;
- }
- return !value;
- }
- function runSyncRule(value, rule) {
- if (isEmptyValue(value)) {
- if (rule.required) {
- return false;
- }
- if (rule.validateEmpty === false) {
- return true;
- }
- }
- if (rule.pattern && !rule.pattern.test(String(value))) {
- return false;
- }
- return true;
- }
- function runRuleValidator(value, rule) {
- return new Promise((resolve) => {
- const returnVal = rule.validator(value, rule);
- if (isPromise(returnVal)) {
- returnVal.then(resolve);
- return;
- }
- resolve(returnVal);
- });
- }
- function getRuleMessage(value, rule) {
- const { message } = rule;
- if (isFunction(message)) {
- return message(value, rule);
- }
- return message || "";
- }
- function startComposing({ target }) {
- target.composing = true;
- }
- function endComposing({ target }) {
- if (target.composing) {
- target.composing = false;
- target.dispatchEvent(new Event("input"));
- }
- }
- function resizeTextarea(input, autosize) {
- const scrollTop = getRootScrollTop();
- input.style.height = "auto";
- let height = input.scrollHeight;
- if (isObject(autosize)) {
- const { maxHeight, minHeight } = autosize;
- if (maxHeight !== void 0) {
- height = Math.min(height, maxHeight);
- }
- if (minHeight !== void 0) {
- height = Math.max(height, minHeight);
- }
- }
- if (height) {
- input.style.height = `${height}px`;
- setRootScrollTop(scrollTop);
- }
- }
- function mapInputType(type, inputmode) {
- if (type === "number") {
- type = "text";
- inputmode != null ? inputmode : inputmode = "decimal";
- }
- if (type === "digit") {
- type = "tel";
- inputmode != null ? inputmode : inputmode = "numeric";
- }
- return { type, inputmode };
- }
- function getStringLength(str) {
- return [...str].length;
- }
- function cutString(str, maxlength) {
- return [...str].slice(0, maxlength).join("");
- }
- export {
- cutString,
- endComposing,
- getRuleMessage,
- getStringLength,
- isEmptyValue,
- mapInputType,
- resizeTextarea,
- runRuleValidator,
- runSyncRule,
- startComposing
- };
|