Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use current values to populate DataAutocomplete options, not just static initial values #4216

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ListManager from "ui/editor/ListManager/ListManager";
import ModalSectionContent from "ui/editor/ModalSectionContent";

import { Option } from "../../shared";
import { useInitialOptions } from "../Public/hooks/useInitialOptions";
import { useCurrentOptions } from "../Public/hooks/useInitialOptions";
import { ExclusiveOrOptionManager } from "./components/ExclusiveOrOptionManager";
import { GroupedOptions } from "./components/GroupedOptions";
import ChecklistOptionsEditor from "./components/OptionsEditor";
Expand All @@ -19,7 +19,7 @@ export const Options: React.FC<{ formik: FormikHookReturn }> = ({ formik }) => {

const exclusiveOrOptionManagerShouldRender = nonExclusiveOptions.length > 0;

const { schema, initialOptionVals } = useInitialOptions(formik);
const { schema, currentOptionVals } = useCurrentOptions(formik);

return (
<ModalSectionContent subtitle="Options">
Expand Down Expand Up @@ -53,7 +53,7 @@ export const Options: React.FC<{ formik: FormikHookReturn }> = ({ formik }) => {
schema: getOptionsSchemaByFn(
formik.values.fn,
schema,
initialOptionVals,
currentOptionVals,
),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ErrorWrapper from "ui/shared/ErrorWrapper";

import { Option } from "../../../shared";
import { Group } from "../../model";
import { useInitialOptions } from "../../Public/hooks/useInitialOptions";
import { useCurrentOptions } from "../../Public/hooks/useInitialOptions";

interface Props {
formik: FormikHookReturn;
Expand All @@ -25,7 +25,7 @@ export const ExclusiveOrOptionManager = ({
groupIndex,
grouped,
}: Props) => {
const { schema, initialOptionVals } = useInitialOptions(formik);
const { schema, currentOptionVals } = useCurrentOptions(formik);

return (
<Box mt={2}>
Expand Down Expand Up @@ -81,7 +81,7 @@ export const ExclusiveOrOptionManager = ({
schema: getOptionsSchemaByFn(
formik.values.fn,
schema,
initialOptionVals,
currentOptionVals,
),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import InputRow from "ui/shared/InputRow";
import { Option } from "../../../shared";
import type { Group } from "../../model";
import { partitionGroupedOptions } from "../../Public/helpers";
import { useInitialOptions } from "../../Public/hooks/useInitialOptions";
import { useCurrentOptions } from "../../Public/hooks/useInitialOptions";
import { ExclusiveOrOptionManager } from "./ExclusiveOrOptionManager";
import ChecklistOptionsEditor from "./OptionsEditor";

Expand All @@ -24,7 +24,7 @@ interface Props {
}

export const GroupedOptions = ({ formik }: Props) => {
const { schema, initialOptionVals } = useInitialOptions(formik);
const { schema, currentOptionVals } = useCurrentOptions(formik);

const [exclusiveOptions, nonExclusiveOptionGroups] = partitionGroupedOptions(
formik.values.groupedOptions,
Expand Down Expand Up @@ -113,7 +113,7 @@ export const GroupedOptions = ({ formik }: Props) => {
schema: getOptionsSchemaByFn(
formik.values.fn,
schema,
initialOptionVals,
currentOptionVals,
),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { FormikHookReturn } from "types";

import { Option } from "../../../shared";

export const useInitialOptions = (formik: FormikHookReturn) => {
export const useCurrentOptions = (formik: FormikHookReturn) => {
const schema = useStore().getFlowSchema()?.options;

const initialOptions: Option[] | undefined =
formik.initialValues.options ||
formik.initialValues.groupedOptions
const currentOptions: Option[] | undefined =
formik.values.options ||
formik.values.groupedOptions
?.map((group: Group<Option>) => group.children)
?.flat();

const initialOptionVals = initialOptions?.map((option) => option.data?.val);
const currentOptionVals = currentOptions?.map((option) => option.data?.val);

return { initialOptions, schema, initialOptionVals };
return { currentOptions, schema, currentOptionVals };
};
4 changes: 2 additions & 2 deletions editor.planx.uk/src/@planx/components/Question/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Question: React.FC<Props> = (props) => {
});

const schema = useStore().getFlowSchema();
const initialOptionVals = formik.initialValues.options?.map(
const currentOptionVals = formik.values.options?.map(
(option) => option.data?.val,
);

Expand Down Expand Up @@ -160,7 +160,7 @@ export const Question: React.FC<Props> = (props) => {
schema: getOptionsSchemaByFn(
formik.values.fn,
schema?.options,
initialOptionVals,
currentOptionVals,
),
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions editor.planx.uk/src/@planx/components/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const getPreviouslySubmittedData = ({
return data;
};

export const getOptionsSchemaByFn = (fn?: string, defaultOptionsSchema?: string[], initialOptions?: (string | undefined)[]) => {
export const getOptionsSchemaByFn = (fn?: string, defaultOptionsSchema?: string[], currentOptions?: (string | undefined)[]) => {
let schema = defaultOptionsSchema;

// For certain data fields, suggest based on full ODP Schema enums rather than current flow schema
Expand All @@ -74,7 +74,7 @@ export const getOptionsSchemaByFn = (fn?: string, defaultOptionsSchema?: string[
if (fn === "property.type") schema = getValidSchemaValues("PropertyType");

// Ensure that any initial values outside of ODP Schema enums will still be recognised/pre-populated when modal loads
initialOptions?.forEach((option) => {
currentOptions?.forEach((option) => {
if (option && !schema?.includes(option)) {
schema?.push(option);
}
Expand Down
Loading