Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
feat: confine conditions inside list to the same local list item (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman authored Sep 6, 2023
1 parent 82a8e11 commit 5602812
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 60 deletions.
58 changes: 58 additions & 0 deletions packages/core/dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,64 @@ collections:
- label: File
name: file
widget: file
- name: typed_list_with_condition
label: Typed List With Condition
widget: list
types:
- label: Type 1 Object
name: type_1_object
widget: object
fields:
- name: template
label: template
widget: select
options:
- column
- row
- banner
default: column
- label: String
name: string
widget: string
- label: Boolean
name: boolean
widget: boolean
condition:
field: template
value: banner
- label: Text
name: text
widget: text
- label: Type 2 Object
name: type_2_object
widget: object
fields:
- label: Number
name: number
widget: number
- label: Select
name: select
widget: select
options:
- a
- b
- c
- label: Datetime
name: datetime
widget: datetime
- label: Markdown
name: markdown
widget: markdown
- label: Type 3 Object
name: type_3_object
widget: object
fields:
- label: Image
name: image
widget: image
- label: File
name: file
widget: file
- name: map
label: Map
file: _widgets/map.json
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ export class Backend<EF extends BaseField = UnknownField, BC extends BackendClas
}

filterEntries(collection: { entries: Entry[] }, filterRule: FilterRule | FilterRule[]) {
return filterEntries(collection.entries, filterRule);
return filterEntries(collection.entries, filterRule, undefined);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const EditorControl = ({
t,
value,
forList = false,
listItemPath,
forSingleList = false,
changeDraftField,
i18n,
Expand Down Expand Up @@ -94,7 +95,7 @@ const EditorControl = ({
() => isFieldHidden(field, locale, i18n?.defaultLocale),
[field, i18n?.defaultLocale, locale],
);
const hidden = useHidden(field, entry);
const hidden = useHidden(field, entry, listItemPath);

useEffect(() => {
if (hidden) {
Expand Down Expand Up @@ -202,6 +203,7 @@ const EditorControl = ({
t,
value: finalValue,
forList,
listItemPath,
forSingleList,
i18n,
hasErrors,
Expand Down Expand Up @@ -231,6 +233,7 @@ const EditorControl = ({
query,
finalValue,
forList,
listItemPath,
forSingleList,
i18n,
hasErrors,
Expand All @@ -248,6 +251,7 @@ interface EditorControlOwnProps {
parentPath: string;
value: ValueOrNestedValue;
forList?: boolean;
listItemPath?: string;
forSingleList?: boolean;
i18n: I18nSettings | undefined;
fieldName?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface EditorControlPaneProps {
onLocaleChange?: (locale: string) => void;
allowDefaultLocale?: boolean;
context?: 'default' | 'i18nSplit';
listItemPath?: string;
}

const EditorControlPane = ({
Expand All @@ -47,6 +48,7 @@ const EditorControlPane = ({
onLocaleChange,
allowDefaultLocale = false,
context = 'default',
listItemPath,
t,
}: TranslatedProps<EditorControlPaneProps>) => {
const pathField = useMemo(
Expand Down Expand Up @@ -138,6 +140,7 @@ const EditorControlPane = ({
locale={locale}
parentPath=""
i18n={i18n}
listItemPath={listItemPath}
controlled
isMeta
/>
Expand All @@ -156,6 +159,7 @@ const EditorControlPane = ({
locale={locale}
parentPath=""
i18n={i18n}
listItemPath={listItemPath}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export interface WidgetControlProps<T, F extends BaseField = UnknownField, EV =
fieldsErrors: FieldsErrors;
submitted: boolean;
forList: boolean;
listItemPath: string | undefined;
forSingleList: boolean;
disabled: boolean;
duplicate: boolean;
Expand Down
74 changes: 65 additions & 9 deletions packages/core/src/lib/util/__tests__/field.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,86 @@ describe('filterEntries', () => {

describe('isHidden', () => {
it('should show field by default', () => {
expect(isHidden(mockTitleField, mockExternalEntry)).toBeFalsy();
expect(isHidden(mockTitleField, mockExternalEntry, undefined)).toBeFalsy();
});

it('should hide field if single condition is not met', () => {
expect(isHidden(mockUrlField, mockInternalEntry)).toBeTruthy();
expect(isHidden(mockUrlField, mockInternalEntry, undefined)).toBeTruthy();
});

it('should show field if single condition is met', () => {
expect(isHidden(mockUrlField, mockExternalEntry)).toBeFalsy();
expect(isHidden(mockUrlField, mockExternalEntry, undefined)).toBeFalsy();
});

it('should hide field if all multiple conditions are not met', () => {
expect(isHidden(mockBodyField, mockExternalEntry)).toBeTruthy();
expect(isHidden(mockBodyField, mockExternalEntry, undefined)).toBeTruthy();
});

it('should show field if single condition is met', () => {
expect(isHidden(mockBodyField, mockHasSummaryEntry)).toBeFalsy();
expect(isHidden(mockBodyField, mockInternalEntry)).toBeFalsy();
expect(isHidden(mockBodyField, mockHasSummaryEntry, undefined)).toBeFalsy();
expect(isHidden(mockBodyField, mockInternalEntry, undefined)).toBeFalsy();
});

it('should show field if entry is undefined', () => {
expect(isHidden(mockTitleField, undefined)).toBeFalsy();
expect(isHidden(mockUrlField, undefined)).toBeFalsy();
expect(isHidden(mockBodyField, undefined)).toBeFalsy();
expect(isHidden(mockTitleField, undefined, undefined)).toBeFalsy();
expect(isHidden(mockUrlField, undefined, undefined)).toBeFalsy();
expect(isHidden(mockBodyField, undefined, undefined)).toBeFalsy();
});

describe('inside list', () => {
const mockInsideListEntry = createMockEntry({
path: 'path/to/file-1.md',
data: {
list: [
{
title: 'I am a title',
type: 'external',
url: 'http://example.com',
hasSummary: false,
},
{
title: 'I am a title',
type: 'internal',
body: 'I am the body of your post',
hasSummary: false,
},
{
title: 'I am a title',
type: 'external',
url: 'http://example.com',
body: 'I am the body of your post',
hasSummary: true,
},
],
},
});

it('should show field by default', () => {
expect(isHidden(mockTitleField, mockInsideListEntry, 'list.0')).toBeFalsy();
});

it('should hide field if single condition is not met', () => {
expect(isHidden(mockUrlField, mockInsideListEntry, 'list.1')).toBeTruthy();
});

it('should show field if single condition is met', () => {
expect(isHidden(mockUrlField, mockInsideListEntry, 'list.0')).toBeFalsy();
});

it('should hide field if all multiple conditions are not met', () => {
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.0')).toBeTruthy();
});

it('should show field if single condition is met', () => {
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.2')).toBeFalsy();
expect(isHidden(mockBodyField, mockInsideListEntry, 'list.1')).toBeFalsy();
});

it('should show field if entry is undefined', () => {
expect(isHidden(mockTitleField, undefined, 'list.0')).toBeFalsy();
expect(isHidden(mockUrlField, undefined, 'list.0')).toBeFalsy();
expect(isHidden(mockBodyField, undefined, 'list.0')).toBeFalsy();
});
});
});
});
Loading

0 comments on commit 5602812

Please sign in to comment.