From 8a0c6e81809ba402247bd20d7830918825c9ffcf Mon Sep 17 00:00:00 2001 From: soup Date: Thu, 30 Mar 2023 23:41:03 +0200 Subject: [PATCH] feat(filters): regex validation for supported fields (#773) * initial commit * minor visual improvements * lock validation to golang flavor * made a new field called regexfield reverted old textfield to be up2date with develop * added Formik Validation * removed yup as its not needed * improvements * refactor: simplify and make more generic --------- Co-authored-by: ze0s --- web/src/components/inputs/index.ts | 2 +- web/src/components/inputs/input.tsx | 127 ++++++++++++++++-- web/src/components/tooltips/CustomTooltip.tsx | 2 +- web/src/screens/filters/details.tsx | 14 +- web/tailwind.config.js | 3 + 5 files changed, 130 insertions(+), 18 deletions(-) diff --git a/web/src/components/inputs/index.ts b/web/src/components/inputs/index.ts index 2feb2e0..49cf304 100644 --- a/web/src/components/inputs/index.ts +++ b/web/src/components/inputs/index.ts @@ -1,5 +1,5 @@ export { ErrorField, CheckboxField } from "./common"; -export { TextField, NumberField, PasswordField } from "./input"; +export { TextField, NumberField, PasswordField, RegexField } from "./input"; export { NumberFieldWide, PasswordFieldWide, SwitchGroupWide, SwitchGroupWideRed, TextFieldWide } from "./input_wide"; export { RadioFieldsetWide } from "./radio"; export { MultiSelect, Select, SelectWide, DownloadClientSelect, IndexerMultiSelect } from "./select"; diff --git a/web/src/components/inputs/input.tsx b/web/src/components/inputs/input.tsx index bf3490b..d52021c 100644 --- a/web/src/components/inputs/input.tsx +++ b/web/src/components/inputs/input.tsx @@ -1,21 +1,21 @@ import { Field, FieldProps } from "formik"; import { classNames } from "../../utils"; -import { EyeIcon, EyeSlashIcon } from "@heroicons/react/24/solid"; +import { EyeIcon, EyeSlashIcon, CheckCircleIcon, XCircleIcon } from "@heroicons/react/24/solid"; import { useToggle } from "../../hooks/hooks"; import { CustomTooltip } from "../tooltips/CustomTooltip"; type COL_WIDTHS = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; interface TextFieldProps { - name: string; - defaultValue?: string; - label?: string; - placeholder?: string; - columns?: COL_WIDTHS; - autoComplete?: string; - hidden?: boolean; - disabled?: boolean; - tooltip?: JSX.Element; + name: string; + defaultValue?: string; + label?: string; + placeholder?: string; + columns?: COL_WIDTHS; + autoComplete?: string; + hidden?: boolean; + disabled?: boolean; + tooltip?: JSX.Element; } export const TextField = ({ @@ -75,6 +75,113 @@ export const TextField = ({ ); +interface RegexFieldProps { + name: string; + defaultValue?: string; + label?: string; + placeholder?: string; + columns?: COL_WIDTHS; + autoComplete?: string; + useRegex?: boolean; + hidden?: boolean; + disabled?: boolean; + tooltip?: JSX.Element; +} + +export const RegexField = ({ + name, + defaultValue, + label, + placeholder, + columns, + autoComplete, + useRegex, + hidden, + tooltip, + disabled +}: RegexFieldProps) => { + const golangRegex = /^((\\\*|\\\?|\\[^\s\\])+|\(\?i\))(\|((\\\*|\\\?|\\[^\s\\])+|\(\?i\)))*$/; + + const validRegex = (pattern: string) => { + try { + new RegExp(golangRegex.source + pattern); + return true; + } catch (e) { + return false; + } + }; + + const validateRegexp = (val: string) => { + let error = ""; + + if (!validRegex(val)) { + error = "Invalid regex"; + } + + return error; + }; + + return ( + + ); +}; + interface TextAreaProps { name: string; defaultValue?: string; diff --git a/web/src/components/tooltips/CustomTooltip.tsx b/web/src/components/tooltips/CustomTooltip.tsx index 5f92c2f..2b8ebb7 100644 --- a/web/src/components/tooltips/CustomTooltip.tsx +++ b/web/src/components/tooltips/CustomTooltip.tsx @@ -18,7 +18,7 @@ export const CustomTooltip = ({ const id = `${anchorId}-tooltip`; return (
- + {children} diff --git a/web/src/screens/filters/details.tsx b/web/src/screens/filters/details.tsx index d325de7..384aafa 100644 --- a/web/src/screens/filters/details.tsx +++ b/web/src/screens/filters/details.tsx @@ -32,7 +32,8 @@ import { NumberField, Select, SwitchGroup, - TextField + TextField, + RegexField } from "../../components/inputs"; import DEBUG from "../../components/debug"; import Toast from "../../components/notifications/Toast"; @@ -472,8 +473,9 @@ export function Advanced({ values }: AdvancedProps) {
-

This field has full regex support (Golang flavour).

https://autobrr.com/filters#advanced

Remember to tick Use Regex below if using more than * and ?.

} /> -

This field has full regex support (Golang flavour).

https://autobrr.com/filters#advanced

Remember to tick Use Regex below if using more than * and ?.

} /> +

This field has full regex support (Golang flavour).

https://autobrr.com/filters#advanced

Remember to tick Use Regex below if using more than * and ?.

} /> +

This field has full regex support (Golang flavour).

https://autobrr.com/filters#advanced

Remember to tick Use Regex below if using more than * and ?.

} /> + {values.match_releases ? ( -

Comma separated list of release groups to match.

https://autobrr.com/filters#advanced} />

Comma separated list of release groups to ignore (takes priority over Match releases).

https://autobrr.com/filters#advanced} /> @@ -531,8 +532,9 @@ export function Advanced({ values }: AdvancedProps) {
- - + + +
diff --git a/web/tailwind.config.js b/web/tailwind.config.js index a8f8ba4..982fcd3 100644 --- a/web/tailwind.config.js +++ b/web/tailwind.config.js @@ -25,6 +25,9 @@ module.exports = { colors: { gray: colors.zinc, }, + margin: { // for the checkmarks used for regex validation in Filters/Advanced + "2.5": "0.625rem", // 10px, between mb-2 (8px) and mb-3 (12px) + }, }, }, variants: {