-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [open-formulieren/open-forms#5016] Referentielijsten as dataSrc
dynamically fetch options for select, selectboxes and radio components
- Loading branch information
Showing
16 changed files
with
347 additions
and
61 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {FormattedMessage, useIntl} from 'react-intl'; | ||
|
||
import {TextField} from '@/components/formio'; | ||
|
||
/** | ||
* The `ReferentielijstenTabelCode` component is used to specify the code of the tabel | ||
* in Referentielijsten API for which the items will be fetched | ||
*/ | ||
export const ReferentielijstenTabelCode: React.FC = () => { | ||
const intl = useIntl(); | ||
return ( | ||
<TextField | ||
name={'openForms.code'} | ||
label={ | ||
<FormattedMessage | ||
description="Label for 'openForms.code' builder field" | ||
defaultMessage="Referentielijsten table code" | ||
/> | ||
} | ||
tooltip={intl.formatMessage({ | ||
description: "Description for the 'openForms.code' builder field", | ||
defaultMessage: `The code of the table from which the options will be retrieved.`, | ||
})} | ||
required | ||
/> | ||
); | ||
}; | ||
|
||
export default ReferentielijstenTabelCode; |
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,9 @@ | ||
/** | ||
* Components to manage options/values for fields that support this, such as: | ||
* | ||
* - select | ||
* - selectboxes | ||
* - radio | ||
*/ | ||
export {default as ReferentielijstenServiceSelect} from './service'; | ||
export {default as ReferentielijstenTabelCode} from './code'; |
68 changes: 68 additions & 0 deletions
68
src/components/builder/values/referentielijsten/service.tsx
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,68 @@ | ||
import {useContext} from 'react'; | ||
import {FormattedMessage, useIntl} from 'react-intl'; | ||
import useAsync from 'react-use/esm/useAsync'; | ||
|
||
import Select from '@/components/formio/select'; | ||
import {BuilderContext} from '@/context'; | ||
|
||
// TODO transform this to id and label? | ||
export interface ReferentielijstenServiceOption { | ||
url: string; | ||
slug: string; | ||
label: string; | ||
apiRoot: string; | ||
apiType: string; | ||
} | ||
|
||
function isServiceOptions( | ||
options: ReferentielijstenServiceOption[] | undefined | ||
): options is ReferentielijstenServiceOption[] { | ||
return options !== undefined; | ||
} | ||
|
||
/** | ||
* Fetch the available Referentielijsten Services and display them in a Select | ||
* | ||
* The selected service is used at runtime to retrieve options to populate a Select | ||
* | ||
* This requires an async function `getServices` to be provided to the | ||
* BuilderContext which is responsible for retrieving the list of available plugins. | ||
* | ||
* If a fetch error occurs, it is thrown during rendering - you should provide your | ||
* own error boundary to catch this. | ||
*/ | ||
const ReferentielijstenServiceSelect: React.FC = () => { | ||
const intl = useIntl(); | ||
const {getServices} = useContext(BuilderContext); | ||
const { | ||
value: options, | ||
loading, | ||
error, | ||
} = useAsync(async () => await getServices('referentielijsten'), []); | ||
if (error) { | ||
throw error; | ||
} | ||
const _options = isServiceOptions(options) ? options : []; | ||
|
||
return ( | ||
<Select | ||
name={'openForms.service'} | ||
label={ | ||
<FormattedMessage | ||
description="Label for 'openForms.service' builder field" | ||
defaultMessage="Referentielijsten service" | ||
/> | ||
} | ||
tooltip={intl.formatMessage({ | ||
description: "Description for the 'openForms.service' builder field", | ||
defaultMessage: `The identifier of the Referentielijsten service from which the options will be retrieved.`, | ||
})} | ||
isLoading={loading} | ||
options={_options} | ||
valueProperty="slug" | ||
required | ||
/> | ||
); | ||
}; | ||
|
||
export default ReferentielijstenServiceSelect; |
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
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
Oops, something went wrong.