Skip to content

Commit

Permalink
✨ [open-formulieren/open-forms#5016] Referentielijsten service automa…
Browse files Browse the repository at this point in the history
…tically select first service
  • Loading branch information
stevenbal committed Feb 4, 2025
1 parent 8474642 commit 034d713
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/components/builder/values/referentielijsten/service.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {useField} 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 Select, {Option} from '@/components/formio/select';
import {BuilderContext} from '@/context';

// TODO transform this to id and label?
export interface ReferentielijstenServiceOption {
url: string;
slug: string;
Expand Down Expand Up @@ -43,6 +43,18 @@ const ReferentielijstenServiceSelect: React.FC = () => {
throw error;
}
const _options = isServiceOptions(options) ? options : [];
const transformedOptions: Option[] = _options.map(({slug, label}) => ({
value: slug,
label: label,
}));

// react-select doesn't update the defaultValue if it is initially `null`,
// so instead we update the value of the Formik field to ensure the correct default
// is selected
const [field, , {setValue}] = useField('openForms.service');
if (transformedOptions && transformedOptions.length == 1 && !field.value) {
setValue(transformedOptions[0].value);
}

return (
<Select
Expand All @@ -58,8 +70,7 @@ const ReferentielijstenServiceSelect: React.FC = () => {
defaultMessage: `The identifier of the Referentielijsten service from which the options will be retrieved.`,
})}
isLoading={loading}
options={_options}
valueProperty="slug"
options={transformedOptions}
required
/>
);
Expand Down
34 changes: 33 additions & 1 deletion src/registry/radio/radio-referentielijsten.stories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Meta, StoryObj} from '@storybook/react';
import {expect, fn, userEvent, within} from '@storybook/test';
import {expect, fn, userEvent, waitFor, within} from '@storybook/test';

import ComponentEditForm from '@/components/ComponentEditForm';
import {BuilderContextDecorator} from '@/sb-decorators';
Expand Down Expand Up @@ -156,3 +156,35 @@ export const SwitchToManualResetOptions: Story = {
});
},
};

export const AutoSelectIfOnlyOneReferentielijstenService: Story = {
name: 'If there is only one Referentielijsten service: automatically select it',
parameters: {
builder: {
enableContext: true,
defaultServices: [
{
url: 'http://localhost:8000/api/v2/services/70',
slug: 'referentielijsten',
label: 'Referentielijsten',
apiRoot: 'http://localhost:8004/api/v1/',
apiType: 'orc',
},
],
},
},
play: async ({canvasElement, step, args}) => {
const canvas = within(canvasElement);

await step('Fill in options', async () => {
const dataSourceInput = canvas.getByLabelText('Data source');
await rsSelect(dataSourceInput, 'Referentielijsten API');

const serviceInput = canvas.getByLabelText('Referentielijsten service');

await waitFor(async () => {
expect(serviceInput.parentElement?.parentElement).toHaveTextContent('Referentielijsten');
});
});
},
};
4 changes: 2 additions & 2 deletions src/tests/sharedUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const DEFAULT_SERVICES: ReferentielijstenServiceOption[] = [
},
{
url: 'http://localhost:8000/api/v2/services/6',
slug: 'documenten',
label: 'Documenten',
slug: 'referentielijsten2',
label: 'Referentielijsten 2',
apiRoot: 'http://localhost:8003/documenten/api/v1/',
apiType: 'drc',
},
Expand Down

0 comments on commit 034d713

Please sign in to comment.