Skip to content

Commit

Permalink
Add error handling support to the new method
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 21, 2023
1 parent 0d29768 commit bc9cbff
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions packages/dnb-eufemia/src/extensions/forms/hooks/useDataValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,6 @@ export default function useDataValue<
const handleBlur = useCallback(() => setHasFocus(false), [setHasFocus])

const updateValue = useCallback(
(argFromInput) => {
valueRef.current = fromInput(argFromInput)
forceUpdate()
},
[fromInput]
)

const handleChange = useCallback(
(argFromInput) => {
const newValue = fromInput(argFromInput)

Expand All @@ -389,8 +381,8 @@ export default function useDataValue<
// calling onChange even if the actual value did not change.
return
}

valueRef.current = newValue
changedRef.current = true

if (
continuousValidation ||
Expand All @@ -407,31 +399,52 @@ export default function useDataValue<
// Always validate the value immediately when it is changed
validateValue()

onChange?.(newValue)
if (path) {
dataContextHandlePathChange?.(path, newValue)
}

forceUpdate()
},
[
continuousValidation,
dataContextHandlePathChange,
fromInput,
hideError,
path,
showError,
validateValue,
]
)

const handleChange = useCallback(
(argFromInput) => {
const newValue = fromInput(argFromInput)

if (newValue === valueRef.current) {
// Avoid triggering a change if the value was not actually changed. This may be caused by rendering components
// calling onChange even if the actual value did not change.
return
}

updateValue(argFromInput)

changedRef.current = true
onChange?.(newValue)

if (elementPath) {
const iterateValuePath = `/${iterateElementIndex}${
elementPath && elementPath !== '/' ? elementPath : ''
}`
handleIterateElementChange?.(iterateValuePath, newValue)
}
forceUpdate()
},
[
path,
elementPath,
fromInput,
handleIterateElementChange,
iterateElementIndex,
continuousValidation,
onChange,
validateValue,
dataContextHandlePathChange,
showError,
hideError,
handleIterateElementChange,
fromInput,
forceUpdate,
updateValue,
]
)

Expand Down

0 comments on commit bc9cbff

Please sign in to comment.