-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(viewer): load dynamic input data
Closes #197
- Loading branch information
1 parent
f7d8ca5
commit daf867b
Showing
13 changed files
with
646 additions
and
568 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
63 changes: 63 additions & 0 deletions
63
packages/form-js-viewer/src/render/hooks/useValuesAsync.js
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,63 @@ | ||
import { useEffect, useState } from 'preact/hooks'; | ||
import useService from './useService'; | ||
|
||
/** | ||
* @enum { String } | ||
*/ | ||
export const LOAD_STATES = { | ||
LOADING: 'loading', | ||
LOADED: 'loaded', | ||
ERROR: 'error' | ||
}; | ||
|
||
/** | ||
* @typedef {Object} ValuesGetter | ||
* @property {Object[]} values - The values data | ||
* @property {(LOAD_STATES)} state - The values data's loading state, to use for conditional rendering | ||
*/ | ||
|
||
/** | ||
* A hook to load values for single and multiselect components. | ||
* | ||
* @param {Object} field - The form field to handle values for | ||
* @return {ValuesGetter} valuesGetter - A values getter object providing loading state and values | ||
*/ | ||
export default function(field) { | ||
const { | ||
valuesKey, | ||
values: staticValues | ||
} = field; | ||
|
||
const [ valuesGetter, setValuesGetter ] = useState({ values: [], error: undefined, state: LOAD_STATES.LOADING }); | ||
const initialData = useService('form')._getState().initialData; | ||
|
||
useEffect(() => { | ||
|
||
let values = []; | ||
|
||
if (valuesKey !== undefined) { | ||
|
||
const keyedValues = (initialData || {})[ valuesKey ]; | ||
|
||
if (keyedValues && Array.isArray(keyedValues)) { | ||
values = keyedValues; | ||
} | ||
} | ||
else if (staticValues !== undefined) { | ||
values = Array.isArray(staticValues) ? staticValues : []; | ||
} | ||
else { | ||
setValuesGetter(getErrorState('No values source defined in the form definition')); | ||
return; | ||
} | ||
|
||
setValuesGetter(buildLoadedState(values)); | ||
|
||
}, [ valuesKey, staticValues, initialData ]); | ||
|
||
return valuesGetter; | ||
} | ||
|
||
const getErrorState = (error) => ({ values: [], error, state: LOAD_STATES.ERROR }); | ||
|
||
const buildLoadedState = (values) => ({ values, error: undefined, state: LOAD_STATES.LOADED }); |
Oops, something went wrong.