Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Add conditional criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
arnemolland committed Feb 9, 2022
1 parent 27c1ea5 commit 1d67503
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 3 additions & 7 deletions src/lib/components/inputs/basic/password/Password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -47,7 +42,8 @@ export const Password = (props: InputProps) => {
<ActionArea
className={validationResponse?.type}
onClick={() => toggleType()}
{...props.inputStyles}>
{...props.inputStyles}
>
{<Icon icon={initialType === 'password' ? 'eye' : 'hide'} />}
</ActionArea>
</InputGroup>
Expand Down
15 changes: 12 additions & 3 deletions src/lib/components/inputs/basic/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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')
}
Expand Down

0 comments on commit 1d67503

Please sign in to comment.