Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Sep 5, 2024
1 parent 9a09a0d commit 09031fc
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions packages/dnb-eufemia/src/extensions/forms/hooks/useFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,9 @@ export default function useFieldProps<Value, EmptyValue, Props>(
hasLocalErrorRef.current = false
}, [persistErrorState])

const onChangeValidatorResultRef = useRef({
cache: null,
unchangedValue: undefined,
})
const onBlurValidatorResultRef = useRef({
cache: null,
const validatorCacheRef = useRef({
onChangeValidator: null,
onBlurValidator: null,
})

const revealOnChangeValidatorResult = useCallback(
Expand Down Expand Up @@ -645,18 +642,32 @@ export default function useFieldProps<Value, EmptyValue, Props>(
}, [callValidatorFnAsync, callValidatorFnSync])

const startOnChangeValidatorProcess = useCallback(async () => {
if (typeof onChangeValidatorRef.current !== 'function') {
return
}

if (isAsync(onChangeValidatorRef.current)) {
defineAsyncProcess('validator')
setFieldState('validating')
hideError()
}

const { result, unchangedValue } = await callOnChangeValidator()
const tmpValue = valueRef.current
let result = isAsync(onChangeValidatorRef.current)
? await callValidatorFnAsync(onChangeValidatorRef.current)
: callValidatorFnSync(onChangeValidatorRef.current)
if (result instanceof Promise) {
result = await result
}
const unchangedValue = tmpValue === valueRef.current

// const { result, unchangedValue } = await callOnChangeValidator()
revealOnChangeValidatorResult({ result, unchangedValue })

return { result }
}, [
callOnChangeValidator,
callValidatorFnAsync,
callValidatorFnSync,
defineAsyncProcess,
hideError,
revealOnChangeValidatorResult,
Expand All @@ -667,7 +678,8 @@ export default function useFieldProps<Value, EmptyValue, Props>(
const { result, unchangedValue } = await callOnChangeValidator()

if (
String(result) !== String(onChangeValidatorResultRef.current.cache)
String(result) !==
String(validatorCacheRef.current.onChangeValidator)
) {
if (result) {
revealOnChangeValidatorResult({ result, unchangedValue })
Expand All @@ -677,7 +689,7 @@ export default function useFieldProps<Value, EmptyValue, Props>(
}
}

onChangeValidatorResultRef.current.cache = result || null
validatorCacheRef.current.onChangeValidator = result || null
}, [
callOnChangeValidator,
clearErrorState,
Expand Down Expand Up @@ -715,17 +727,15 @@ export default function useFieldProps<Value, EmptyValue, Props>(

const revealOnBlurValidatorResult = useCallback(
({ result }) => {
const runAsync = isAsync(onBlurValidatorRef.current)

persistErrorState('gracefully', result as Error)

if (runAsync) {
if (isAsync(onBlurValidatorRef.current)) {
defineAsyncProcess(undefined)
setFieldState(result instanceof Error ? 'error' : 'complete')
}

revealError()
forceUpdate()
// forceUpdate()
},
[defineAsyncProcess, persistErrorState, revealError, setFieldState]
)
Expand All @@ -736,11 +746,22 @@ export default function useFieldProps<Value, EmptyValue, Props>(
}: {
overrideValue?: Value
} = {}) => {
if (typeof onBlurValidatorRef.current !== 'function') {
return
}

if (isAsync(onBlurValidatorRef.current)) {
defineAsyncProcess('onBlurValidator')
setFieldState('validating')
}

// let result = isAsync(onBlurValidatorRef.current)
// ? await callValidatorFnAsync(onBlurValidatorRef.current, value)
// : callValidatorFnSync(onBlurValidatorRef.current, value)
// if (result instanceof Promise) {
// result = await result
// }

const { result } = await callOnBlurValidator({ overrideValue })
revealOnBlurValidatorResult({ result })
},
Expand All @@ -756,7 +777,8 @@ export default function useFieldProps<Value, EmptyValue, Props>(
const { result } = await callOnBlurValidator()

if (
String(result) !== String(onBlurValidatorResultRef.current.cache) &&
String(result) !==
String(validatorCacheRef.current.onBlurValidator) &&
revealErrorRef.current
) {
if (result) {
Expand All @@ -767,7 +789,7 @@ export default function useFieldProps<Value, EmptyValue, Props>(
}
}

onBlurValidatorResultRef.current.cache = result || null
validatorCacheRef.current.onBlurValidator = result || null
}, [
callOnBlurValidator,
clearErrorState,
Expand Down

0 comments on commit 09031fc

Please sign in to comment.