From 1d67503082849e079e46a7ec58bae77ed6c14850 Mon Sep 17 00:00:00 2001 From: Arne Molland Date: Wed, 9 Feb 2022 14:20:57 +0100 Subject: [PATCH] Add conditional criteria --- .../components/inputs/basic/password/Password.tsx | 10 +++------- src/lib/components/inputs/basic/shared/types.ts | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/lib/components/inputs/basic/password/Password.tsx b/src/lib/components/inputs/basic/password/Password.tsx index 3018c097..c206b057 100644 --- a/src/lib/components/inputs/basic/password/Password.tsx +++ b/src/lib/components/inputs/basic/password/Password.tsx @@ -3,12 +3,7 @@ import { useInputValidation } from '../../../../hooks/useInputValidation' import { InputProps, InputType } from '../shared' import { ValidationInfo } from '../shared/ValidationInfo' -import { - ActionArea, - InputGroup, - StyledInput, - VerificationWithToggle -} from '../shared' +import { ActionArea, InputGroup, StyledInput, VerificationWithToggle } from '../shared' import { Icon } from '../../../icons' export const Password = (props: InputProps) => { @@ -47,7 +42,8 @@ export const Password = (props: InputProps) => { toggleType()} - {...props.inputStyles}> + {...props.inputStyles} + > {} diff --git a/src/lib/components/inputs/basic/shared/types.ts b/src/lib/components/inputs/basic/shared/types.ts index da012064..a2f5eb79 100644 --- a/src/lib/components/inputs/basic/shared/types.ts +++ b/src/lib/components/inputs/basic/shared/types.ts @@ -26,7 +26,14 @@ export type InputProps = { export type InputResponseType = 'error' | 'warning' | 'success' | 'none' -export type CriteriaType = 'min' | 'max' | 'email' | 'required' | 'regex' | 'validation' +export type CriteriaType = + | 'min' + | 'max' + | 'email' + | 'required' + | 'regex' + | 'validation' + | 'condition' export type ValidationFunction = (value: string) => boolean @@ -51,8 +58,8 @@ export type InputCriteriaResponse = { export const criteriaRule = ( type: CriteriaType, value: any, - rule?: number | RegExp | ValidationFunction -) => { + rule?: number | RegExp | ValidationFunction | boolean +): boolean => { switch (type) { case 'required': return !isEmpty(value as string) @@ -68,6 +75,8 @@ export const criteriaRule = ( return (rule as RegExp).test(value as string) case 'validation': return (rule as ValidationFunction)?.(value) + case 'condition': + return value default: throw new Error('Unknown criteria type') }