Skip to content

Commit 88ed8c8

Browse files
Merge pull request #848 from gadget-inc/mill/addControlToHasManyThroughFormForDupeSelect
Added `allowMultipleSelections` prop to `AutoHasManyThroughForm`
2 parents 700010e + 84ecb10 commit 88ed8c8

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@gadget-client/app-with-no-user-model": "^1.10.0",
2828
"@gadget-client/bulk-actions-test": "^1.113.0",
2929
"@gadget-client/full-auth": "^1.9.0",
30-
"@gadget-client/js-clients-test": "1.512.0-development.2586",
30+
"@gadget-client/js-clients-test": "1.512.0-development.2591",
3131
"@gadget-client/kitchen-sink": "1.9.0-development.206",
3232
"@gadget-client/related-products-example": "^1.865.0",
3333
"@gadget-client/zxcv-deeply-nested": "^1.212.0",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gadgetinc/react": patch
3+
---
4+
5+
- Added `allowMultipleSelections` prop to `AutoHasManyThroughForm` to control if sibling records can be linked multiple times through different join model records

packages/react/spec/auto/storybook/form/AutoRelationshipForm.stories.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,7 @@ const ExampleCourseCreateRelatedForm = (props: any) => {
234234
primary: ["title", "firstName", "lastName"],
235235
secondary: "weight",
236236
}}
237-
>
238-
<></>
239-
</AutoHasManyThroughForm>
237+
/>
240238
</Card>
241239
<AutoSubmit />
242240
</SelectableDesignSystemAutoFormStory>

packages/react/spec/auto/storybook/form/SelectableDesignSystemAutoFormStory.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { BlockStack, Box, Button as PolarisButton, Card as PolarisCard, Label as
33
import React from "react";
44
import { SUITE_NAMES } from "../../../../cypress/support/constants.js";
55
import { type AutoFormProps } from "../../../../src/auto/AutoForm.js";
6-
import type { AutoRelationshipFormProps, AutoRelationshipInputProps } from "../../../../src/auto/interfaces/AutoRelationshipInputProps.js";
6+
import type {
7+
AutoHasManyThroughFormProps,
8+
AutoRelationshipFormProps,
9+
AutoRelationshipInputProps,
10+
} from "../../../../src/auto/interfaces/AutoRelationshipInputProps.js";
711
import { PolarisAutoForm } from "../../../../src/auto/polaris/PolarisAutoForm.js";
812
import { PolarisAutoInput } from "../../../../src/auto/polaris/inputs/PolarisAutoInput.js";
913
import { PolarisAutoBelongsToForm } from "../../../../src/auto/polaris/inputs/relationships/PolarisAutoBelongsToForm.js";
@@ -107,7 +111,7 @@ export const AutoHasManyForm = (props: AutoRelationshipFormProps) => {
107111
return null;
108112
};
109113

110-
export const AutoHasManyThroughForm = (props: AutoRelationshipFormProps) => {
114+
export const AutoHasManyThroughForm = (props: AutoHasManyThroughFormProps) => {
111115
const { designSystem } = useDesignSystem();
112116
if (designSystem === SUITE_NAMES.POLARIS) {
113117
return <PolarisAutoHasManyThroughForm {...props} />;

packages/react/src/auto/interfaces/AutoRelationshipInputProps.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ export type AutoRelationshipFormProps = {
9191
recordFilter?: RecordFilter;
9292
};
9393

94-
export type AutoHasManyThroughFormProps = Omit<AutoRelationshipFormProps, "children"> & { children?: ReactNode };
94+
export type AutoHasManyThroughFormProps = Omit<AutoRelationshipFormProps, "children"> & {
95+
children?: ReactNode;
96+
97+
/**
98+
* Allows sibling records to be selected multiple times.
99+
*/
100+
allowMultipleSelections?: boolean;
101+
};
95102

96103
export const getRecordLabelObject = (recordLabel?: OptionLabel | RecordLabel): RecordLabel | undefined => {
97104
if (!recordLabel) {

packages/react/src/useHasManyThroughForm.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AutoHasManyThroughFormProps } from "./auto/interfaces/AutoRelation
77
import { useFormContext } from "./useActionForm.js";
88

99
export const useHasManyThroughForm = (props: AutoHasManyThroughFormProps) => {
10-
const { field, children } = props;
10+
const { field, children, allowMultipleSelections } = props;
1111
const { metadata } = useAutoRelationship({ field });
1212
const { setValue } = useFormContext();
1313

@@ -43,7 +43,7 @@ export const useHasManyThroughForm = (props: AutoHasManyThroughFormProps) => {
4343

4444
const {
4545
search,
46-
searchFilterOptions: siblingModelOptions,
46+
searchFilterOptions: rawSiblingModelOptions,
4747
relatedModel: { fetching: siblingRecordsLoading, records: siblingRecords },
4848
pagination: siblingPagination,
4949
} = relatedModelOptions;
@@ -67,6 +67,20 @@ export const useHasManyThroughForm = (props: AutoHasManyThroughFormProps) => {
6767

6868
const recordLabel = useRecordLabelObjectFromProps(props);
6969

70+
const siblingModelOptions = useMemo(() => {
71+
if (!allowMultipleSelections && inverseRelatedModelField) {
72+
return rawSiblingModelOptions.filter(
73+
(option) =>
74+
!joinRecords.some((joinRecord) => {
75+
const rawJoinRecord = joinRecord[2];
76+
const siblingFromJoinRecord = rawJoinRecord[inverseRelatedModelField];
77+
return siblingFromJoinRecord && "id" in siblingFromJoinRecord && siblingFromJoinRecord.id === option.id;
78+
})
79+
);
80+
}
81+
return rawSiblingModelOptions;
82+
}, [rawSiblingModelOptions, allowMultipleSelections, inverseRelatedModelField, joinRecords]);
83+
7084
return {
7185
fields,
7286
append,

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)