@rcb-plugins/input-validator
Version:
A lightweight plugin to validate user input with visual feedbacks in React ChatBotify!
111 lines (110 loc) • 3.37 kB
JavaScript
import { useState as A, useRef as U, useEffect as B } from "react";
import { useToasts as E, useFlow as C, useStyles as I, useOnRcbEvent as f, RcbEvent as h } from "react-chatbotify";
const i = {
autoConfig: !0,
// defaults to true to help users enable required events
promptBaseColors: {
info: "#007bff",
warning: "#ffc107",
error: "#dc3545",
success: "#28a745"
},
promptHoveredColors: {
info: "#0056b3",
warning: "#d39e00",
error: "#c82333",
success: "#218838"
},
textAreaHighlightColors: {
info: "#007bff",
warning: "#ffc107",
error: "#dc3545",
success: "#28a745"
}
}, R = (t) => ({
...i,
...t,
promptBaseColors: {
...i.promptBaseColors,
...t == null ? void 0 : t.promptBaseColors
},
promptHoveredColors: {
...i.promptHoveredColors,
...t == null ? void 0 : t.promptHoveredColors
},
textAreaHighlightColors: {
...i.textAreaHighlightColors,
...t == null ? void 0 : t.textAreaHighlightColors
}
}), w = (t, e) => {
const r = t.promptType ?? "info";
let o = {};
return e.advancedStyles && (o = e.advancedStyles[r]), e.promptBaseColors && (o.toastPromptStyle = {
color: e.promptBaseColors[r],
borderColor: e.promptBaseColors[r]
}), e.promptHoveredColors && (o.toastPromptHoveredStyle = {
color: e.promptHoveredColors[r],
borderColor: e.promptHoveredColors[r]
}), e.textAreaHighlightColors && (t.highlightTextArea ?? !0) && (o.chatInputAreaStyle = {
boxShadow: `${e.textAreaHighlightColors[r]} 0px 0px 5px`
}), o;
}, y = (t, e, r = "validateTextInput") => {
var l;
if (!((l = t.detail) != null && l.currPath))
return;
const o = e[t.detail.currPath];
if (!o)
return;
const u = o[r];
return typeof u == "function" ? u : void 0;
}, F = (t) => {
const { showToast: e } = E(), { getFlow: r } = C(), { styles: o, updateStyles: u, replaceStyles: l } = I(), S = R(t), [m, v] = A(0), b = U({}), P = (a) => {
const n = a, p = y(n, r(), "validateTextInput");
if (!p)
return;
const s = p(n.data.inputText);
if (s != null && s.success || a.preventDefault(), !s.promptContent)
return;
m === 0 && (b.current = structuredClone(o));
const c = w(s, S);
u(c), e(s.promptContent, s.promptDuration ?? 3e3), v((d) => d + 1);
}, H = (a) => {
var d, x;
const n = a, p = (x = (d = n.data) == null ? void 0 : d.files) == null ? void 0 : x[0];
if (!p) {
console.error("No file uploaded."), a.preventDefault();
return;
}
const s = y(n, r(), "validateFileInput");
if (!s) {
console.error("Validator not found for file input.");
return;
}
const c = s(p);
if (!c.success) {
console.error("Validation failed:", c), c.promptContent && e(c.promptContent, c.promptDuration ?? 3e3), a.preventDefault();
return;
}
console.log("Validation successful:", c);
}, D = () => {
v((a) => a - 1);
};
f(h.USER_SUBMIT_TEXT, P), f(h.USER_UPLOAD_FILE, H), f(h.DISMISS_TOAST, D), B(() => {
m === 0 && setTimeout(() => {
l(b.current);
});
}, [m, l]);
const T = {
name: "@rcb-plugins/input-validator"
};
return S.autoConfig && (T.settings = {
event: {
rcbUserSubmitText: !0,
rcbUserUploadFile: !0,
rcbDismissToast: !0
}
}), T;
}, M = (t) => () => F(t);
export {
M as default
};