-
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.
🚧 [#5016] Referentielijsten dataSrc for options
- Loading branch information
Showing
18 changed files
with
384 additions
and
13 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
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 @@ | ||
export {default as ReferentielijstenServiceSelect} from './referentielijsten-services'; |
56 changes: 56 additions & 0 deletions
56
src/components/builder/referentielijsten/referentielijsten-services.spec.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,56 @@ | ||
import userEvent from '@testing-library/user-event'; | ||
import {Formik} from 'formik'; | ||
|
||
import {act, contextRender, screen} from '@/tests/test-utils'; | ||
|
||
import RegistrationAttributeSelect, { | ||
RegistrationAttributeOption, | ||
} from './referentielijsten-services'; | ||
|
||
const REGISTRATION_ATTRIBUTES: RegistrationAttributeOption[] = [ | ||
{id: 'bsn', label: 'BSN'}, | ||
{id: 'firstName', label: 'First name'}, | ||
{id: 'dob', label: 'Date of Birth'}, | ||
]; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('Available registration attributes are retrieved via context', async () => { | ||
const user = userEvent.setup({advanceTimers: jest.advanceTimersByTime}); | ||
|
||
contextRender( | ||
<Formik onSubmit={jest.fn()} initialValues={{registration: {attribute: ''}}}> | ||
<RegistrationAttributeSelect /> | ||
</Formik>, | ||
{ | ||
enableContext: true, | ||
locale: 'en', | ||
builderOptions: { | ||
registrationAttributes: REGISTRATION_ATTRIBUTES, | ||
registrationAttributesDelay: 100, | ||
}, | ||
} | ||
); | ||
|
||
// open the dropdown | ||
const input = await screen.findByLabelText('Registration attribute'); | ||
await act(async () => { | ||
input.focus(); | ||
await user.keyboard('[ArrowDown]'); | ||
}); | ||
|
||
// options are loaded async, while doing network IO the loading state is displayed | ||
expect(await screen.findByText('Loading...')).toBeVisible(); | ||
|
||
// once resolved, the options are visible and the loading state is no longer | ||
expect(await screen.findByText('BSN')).toBeVisible(); | ||
expect(await screen.findByText('First name')).toBeVisible(); | ||
expect(await screen.findByText('Date of Birth')).toBeVisible(); | ||
expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/components/builder/referentielijsten/referentielijsten-services.stories.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,27 @@ | ||
import {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {withFormik} from '@/sb-decorators'; | ||
import {DEFAULT_SERVICES} from '@/tests/sharedUtils'; | ||
|
||
import ReferentielijstenServiceSelect, { | ||
ReferentielijstenServiceOption, | ||
} from './referentielijsten-services'; | ||
|
||
export default { | ||
title: 'Formio/Builder/Referentielijsten/ReferentielijstenServiceSelect', | ||
component: ReferentielijstenServiceSelect, | ||
decorators: [withFormik], | ||
parameters: { | ||
controls: {hideNoControlsWarning: true}, | ||
modal: {noModal: true}, | ||
builder: {enableContext: true, registrationAttributesDelay: 100}, | ||
formik: {initialValues: {registration: {attribute: ''}}}, | ||
}, | ||
args: { | ||
services: DEFAULT_SERVICES, | ||
}, | ||
} as Meta<typeof ReferentielijstenServiceSelect>; | ||
|
||
type Story = StoryObj<typeof ReferentielijstenServiceOption>; | ||
|
||
export const Default: Story = {}; |
75 changes: 75 additions & 0 deletions
75
src/components/builder/referentielijsten/referentielijsten-services.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,75 @@ | ||
import {useFormikContext} from 'formik'; | ||
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; | ||
} | ||
|
||
interface ReferentielijstenServiceSelectProps { | ||
name: string; | ||
} | ||
|
||
/** | ||
* Fetch the available Referentielijsten Services and display them in 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<ReferentielijstenServiceSelectProps> = ({name}) => { | ||
const intl = useIntl(); | ||
const {getServices} = useContext(BuilderContext); | ||
const {setFieldValue} = useFormikContext(); | ||
|
||
const { | ||
value: options, | ||
loading, | ||
error, | ||
} = useAsync(async () => await getServices('referentielijsten'), []); | ||
if (error) { | ||
throw error; | ||
} | ||
const _options = isServiceOptions(options) ? options : []; | ||
|
||
return ( | ||
<Select | ||
name={name} | ||
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} | ||
isClearable | ||
options={_options} | ||
valueProperty="slug" | ||
// TODO not hardcoded name | ||
onChange={event => setFieldValue('openForms.service', event.target.value)} | ||
/> | ||
); | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {useFormikContext} from 'formik'; | ||
import {FormattedMessage, useIntl} from 'react-intl'; | ||
|
||
import {TextField} from '@/components/formio'; | ||
|
||
const NAME = 'openForms.code'; | ||
|
||
/** | ||
* 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(); | ||
const {setFieldValue} = useFormikContext(); | ||
const name = `editform-${NAME}`; | ||
return ( | ||
<TextField | ||
name={name} | ||
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.`, | ||
})} | ||
onChange={event => setFieldValue(NAME, event.target.value)} | ||
/> | ||
); | ||
}; | ||
|
||
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 ReferentielijstenService} from './service'; | ||
export {default as ReferentielijstenTabelCode} from './code'; |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
// import {useFormikContext} from 'formik'; | ||
// import {FormattedMessage, useIntl} from 'react-intl'; | ||
import {ReferentielijstenServiceSelect} from '../../referentielijsten'; | ||
|
||
const NAME = 'openForms.service'; | ||
|
||
/** | ||
* The `ReferentielijstenService` component is used to specify the slug of the service | ||
* that is used to retrieve options from | ||
*/ | ||
export const ReferentielijstenService: React.FC = () => { | ||
// const intl = useIntl(); | ||
// const {setFieldValue} = useFormikContext(); | ||
const name = `editform-${NAME}`; | ||
return <ReferentielijstenServiceSelect name={name} />; | ||
}; | ||
|
||
export default ReferentielijstenService; |
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.