-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft(Forms): API draft for how fields can handle API connections
- Loading branch information
1 parent
d2f5c23
commit c78b161
Showing
4 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/dnb-eufemia/src/extensions/forms/hooks/useConnector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { isAsync } from '../../../shared/helpers/isAsync' | ||
import { ConnectorProps } from '../types' | ||
|
||
import { useCallback } from 'react' | ||
|
||
export default function useConnector<Value>( | ||
connector: ConnectorProps<Value> = {}, | ||
props: Record<string, unknown> & ConnectorProps<Value> = {} | ||
): { | ||
onChange: ConnectorProps<Value>['onChange'] | ||
onBlurValidator: ConnectorProps<Value>['onBlurValidator'] | ||
} { | ||
const { handler: onBlurValidator, asyncHandler: onBlurValidatorAsync } = | ||
useCombinedHandlers(connector.onBlurValidator, props.onBlurValidator) | ||
const { handler: onChange, asyncHandler: onChangeAsync } = | ||
useCombinedHandlers(connector.onChange, props.onChange) | ||
|
||
const onChangeRefs = [connector.onChange, props.onChange] | ||
const onBlurValidatorRefs = [ | ||
connector.onBlurValidator, | ||
props.onBlurValidator, | ||
] | ||
|
||
return { | ||
onChange: isFunctionInArray(onChangeRefs) | ||
? isAsyncInArray(onChangeRefs) | ||
? onChangeAsync | ||
: onChange | ||
: undefined, | ||
onBlurValidator: isFunctionInArray(onBlurValidatorRefs) | ||
? isAsyncInArray(onBlurValidatorRefs) | ||
? onBlurValidatorAsync | ||
: onBlurValidator | ||
: undefined, | ||
} | ||
} | ||
|
||
function useCombinedHandlers( | ||
connectorFn?: (...args: unknown[]) => unknown, | ||
propsFn?: (...args: unknown[]) => unknown | ||
) { | ||
const handler = useCallback( | ||
(...args: unknown[]) => { | ||
{ | ||
const result = propsFn?.apply(this, args) | ||
if (result) { | ||
return result | ||
} | ||
} | ||
|
||
{ | ||
const result = connectorFn?.apply(this, args) | ||
if (result) { | ||
return result | ||
} | ||
} | ||
}, | ||
[connectorFn, propsFn] | ||
) | ||
|
||
const asyncHandler = useCallback( | ||
async (...args: unknown[]) => { | ||
{ | ||
const result = await propsFn?.apply(this, args) | ||
if (result) { | ||
return result | ||
} | ||
} | ||
|
||
{ | ||
const result = await connectorFn?.apply(this, args) | ||
if (result) { | ||
return result | ||
} | ||
} | ||
}, | ||
[connectorFn, propsFn] | ||
) | ||
|
||
return { | ||
handler: | ||
[connectorFn, propsFn].filter(Boolean).length > 0 | ||
? handler | ||
: undefined, | ||
asyncHandler: | ||
[connectorFn, propsFn].filter(Boolean).length > 0 | ||
? asyncHandler | ||
: undefined, | ||
} | ||
} | ||
|
||
function isAsyncInArray(array: Array<unknown>) { | ||
return array.some((fn) => { | ||
return isAsync(fn) | ||
}) | ||
} | ||
function isFunctionInArray(array: Array<unknown>) { | ||
return array.some((fn) => { | ||
return typeof fn === 'function' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters