From 7b50083d205cd4e54e9bfc881c2ae6ec449c1e16 Mon Sep 17 00:00:00 2001 From: Elvyra <88881326+EepyElvyra@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:23:12 +0100 Subject: [PATCH] add isValid to arrays and fix types --- .../components/SettingArrayComponent.tsx | 19 +++++++++++++++++-- src/utils/types.ts | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/components/PluginSettings/components/SettingArrayComponent.tsx b/src/components/PluginSettings/components/SettingArrayComponent.tsx index e0ab5f0bd..6d5282499 100644 --- a/src/components/PluginSettings/components/SettingArrayComponent.tsx +++ b/src/components/PluginSettings/components/SettingArrayComponent.tsx @@ -72,6 +72,18 @@ export function SettingArrayComponent({ const [text, setText] = useState(""); useEffect(() => { + if (text === "") { + setError(null); + return; + } + + if (option.type === OptionType.ARRAY) { + const isValid = option.isValid?.call(definedSettings, text) ?? true; + if (typeof isValid === "string") setError(isValid); + else if (!isValid) setError("Invalid input provided."); + else setError(null); + return; + } if (!isNaN(Number(text)) && text !== "") { if (text.length >= 18 && text.length <= 19) { setError(null); @@ -79,7 +91,10 @@ export function SettingArrayComponent({ setError("Invalid ID"); } } else if (text !== "") { - setError(null); + const isValid = option.isValid?.call(definedSettings, text) ?? true; + if (typeof isValid === "string") setError(isValid); + else if (!isValid) setError("Invalid input provided."); + else setError(null); } }, [text]); @@ -377,7 +392,7 @@ export function SettingArrayComponent({ id={cl("add-button")} onClick={handleSubmit} style={{ background: "none" }} - disabled={((text.length < 18 || text.length > 19) && option.type !== OptionType.ARRAY) || text === ""} + disabled={((text.length < 18 || text.length > 19) && option.type !== OptionType.ARRAY) || text === "" || error != null} > : diff --git a/src/utils/types.ts b/src/utils/types.ts index 9b177c5f3..09e289046 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -326,14 +326,14 @@ export interface PluginSettingArrayDef { * Only applies to User, Channel, and Guild arrays. */ hidePopout?: boolean; - default?: any[]; + default?: string[]; /** * If the setting used to be a string with a custom delimiter, you can specify the delimiter or a function to split the string * @default "," */ oldStringSeparator?: string | ((value: string) => string[]) | RegExp; - onChange?(newValue: any[]): void; + onChange?(newValue: string[]): void; } export interface IPluginOptionComponentProps { @@ -434,7 +434,7 @@ export type PluginOptionSelect = PluginSettingSelectDef & PluginSettingCommon & export type PluginOptionSlider = PluginSettingSliderDef & PluginSettingCommon & IsDisabled & IsValid; export type PluginOptionComponent = PluginSettingComponentDef & Omit; export type PluginOptionCustom = PluginSettingCustomDef & Pick; -export type PluginOptionArray = PluginSettingArrayDef & PluginSettingCommon; +export type PluginOptionArray = PluginSettingArrayDef & PluginSettingCommon & IsDisabled & IsValid; export type PluginNative any>> = { [key in keyof PluginExports]: