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

Commit

Permalink
Test nonBlocking callback as simpler solution for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
guerrato committed Feb 17, 2022
1 parent 02d2a8f commit 7471007
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/lib/components/inputs/basic/password/Password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const Password = (props: InputProps) => {
}

useEffect(() => {
props.onValidate?.call(null, getResultantValidationResponse(validationResponse))
const { isValid, nonBlocking } = getResultantValidationResponse(validationResponse)
props.onValidate?.call(null, isValid, nonBlocking)
}, [validationResponse])

return (
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/inputs/basic/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const Search = (props: InputProps) => {
}

useEffect(() => {
props.onValidate?.call(null, getResultantValidationResponse(validationResponse))
const { isValid, nonBlocking } = getResultantValidationResponse(validationResponse)
props.onValidate?.call(null, isValid, nonBlocking)
}, [validationResponse])

return (
Expand Down
25 changes: 18 additions & 7 deletions src/lib/components/inputs/basic/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export type InputProps = {
multipleCriteriaInfo?: boolean
criteria?: InputValidation | InputValidation[]
onChange?: ChangeEventHandler<HTMLInputElement>
onValidate?: (isValid: boolean) => void
onFeedback?: (validationResponse: InputCriteriaResponse | InputCriteriaResponse[]) => void
onValidate?: (isValid: boolean, nonBlocking: boolean) => void
inputStyles?: SharedProps
readOnly?: boolean
autoComplete?: AutocompleteValues
Expand Down Expand Up @@ -51,6 +50,7 @@ export type InputCriteriaResponse = {
type: InputResponseType
icon?: IconName
isValid: boolean
nonBlocking: boolean
}

export const criteriaRule = (
Expand Down Expand Up @@ -82,15 +82,26 @@ export const criteriaRule = (

export const getResultantValidationResponse = (
validationResponse?: InputCriteriaResponse | InputCriteriaResponse[]
) => {
): {
isValid: boolean
nonBlocking: boolean
} => {
if (!validationResponse) {
return false
return { isValid: false, nonBlocking: false }
}

if (Array.isArray(validationResponse)) {
const allResp = validationResponse.map((v) => v.isValid)
return allResp.includes(false) ? false : true
const allResp = validationResponse.map((v: InputCriteriaResponse) => {
const { isValid, nonBlocking } = v
return { isValid, nonBlocking }
})

const found = allResp.find((v) => v.isValid === false)

if (found) {
return found
}
}

return validationResponse.isValid
return { isValid: true, nonBlocking: true }
}
3 changes: 2 additions & 1 deletion src/lib/components/inputs/basic/text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const Text = (props: InputProps) => {
}

useEffect(() => {
props.onValidate?.call(null, getResultantValidationResponse(validationResponse))
const { isValid, nonBlocking } = getResultantValidationResponse(validationResponse)
props.onValidate?.call(null, isValid, nonBlocking)
}, [validationResponse])

return (
Expand Down
9 changes: 5 additions & 4 deletions src/lib/hooks/useInputValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ const handleResponse = (
message = !isEmpty(validMessage) ? validMessage : ''
}

if (!isValid && nonBlocking) {
type = 'warning'
}
// if (!isValid && nonBlocking) {
// type = 'warning'
// }

const icon = getValidationIcon(type)

return {
message,
type: type,
icon,
isValid
isValid,
nonBlocking
}
}

Expand Down

0 comments on commit 7471007

Please sign in to comment.