Skip to content

Commit

Permalink
feat(react): add priority override for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinechassagne committed Nov 21, 2023
1 parent 857732f commit 0720afb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/lib/Builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export default class Builder extends Emitter {

getComponentDisplayableSettings (element, { component }) {
return [
...this.#settings.getDisplayable?.(element) || [],

Check warning on line 137 in packages/core/lib/Builder/index.js

View check run for this annotation

Codecov / codecov/patch

packages/core/lib/Builder/index.js#L137

Added line #L137 was not covered by tests
...this.#components
.getDisplayableSettings?.(element, { component }) || [],
...this.#settings.getDisplayable?.(element) || [],
];
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export declare interface SettingOverrideObject {
default?: any;
displayable?: boolean;
valueType?: string;
priority?: number;
fields?: Array<ComponentSettingsFieldObject>;
props?: object;
condition: (element: Element | ElementObject, opts?: {
Expand All @@ -120,6 +121,7 @@ export declare class SettingOverride {
default: any;
displayable: boolean;
valueType: string;
priority: number;
fields: Array<ComponentSettingsField>;
props: object;
condition: (element: Element | ElementObject, opts?: {
Expand Down
15 changes: 14 additions & 1 deletion packages/react/lib/DisplayableSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ import Property from './Property';
const DisplayableSettings = ({ className, element, component, override }) => {
const { builder } = useBuilder();

const getSettingPriority = setting => {
const fieldOverride = {

Check warning on line 12 in packages/react/lib/DisplayableSettings/index.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/DisplayableSettings/index.js#L12

Added line #L12 was not covered by tests
...builder.getOverride('setting', element.type, { setting }),
...builder.getOverride('component', element.type, {
output: 'field', setting,
}),
};

return Number.isSafeInteger(fieldOverride?.priority)
? fieldOverride.priority
: setting.priority || 0;

Check warning on line 21 in packages/react/lib/DisplayableSettings/index.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/DisplayableSettings/index.js#L20-L21

Added lines #L20 - L21 were not covered by tests
};

const displayableSettings = useMemo(() => (
builder
.getComponentDisplayableSettings(element, { component })
.filter(s => !s.condition || s.condition(element))
.sort((a, b) => (b.priority || 0) - (a.priority || 0))
.sort((a, b) => getSettingPriority(b) - getSettingPriority(a))

Check warning on line 28 in packages/react/lib/DisplayableSettings/index.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/DisplayableSettings/index.js#L28

Added line #L28 was not covered by tests
), [element, component]);

if (displayableSettings.length <= 0) {
Expand Down
15 changes: 14 additions & 1 deletion packages/react/lib/Editable/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ const Form = ({
onCancel();
};

const getFieldPriority = field => {
const fieldOverride = {

Check warning on line 64 in packages/react/lib/Editable/Form.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/Editable/Form.js#L63-L64

Added lines #L63 - L64 were not covered by tests
...builder.getOverride('setting', element.type, { setting: field }),
...builder.getOverride('component', element.type, {
output: 'field', setting: field,
}),
};

return Number.isSafeInteger(fieldOverride?.priority)
? fieldOverride.priority
: field.priority || 0;

Check warning on line 73 in packages/react/lib/Editable/Form.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/Editable/Form.js#L72-L73

Added lines #L72 - L73 were not covered by tests
};

const hasSubfields = setting =>
Array.isArray(setting.fields) && setting.fields.length > 0;

Expand Down Expand Up @@ -99,7 +112,7 @@ const Form = ({
.filter(field => (tab.id === 'general' && !field.tab) ||
field.tab === tab.id)
)
.sort((a, b) => (b.priority || 0) - (a.priority || 0))
.sort((a, b) => getFieldPriority(b) - getFieldPriority(a))

Check warning on line 115 in packages/react/lib/Editable/Form.js

View check run for this annotation

Codecov / codecov/patch

packages/react/lib/Editable/Form.js#L115

Added line #L115 was not covered by tests
.filter(f =>
!f.condition ||
f.condition(state.element, { component, builder })
Expand Down
7 changes: 7 additions & 0 deletions packages/react/lib/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ export const withMultipleCustomSettings = () => {
fields: [{
key: 'headingLevel',
options: ['t1', 't2', 't3', 't4', 't5', 't6'],
priority: 3,
}, {
key: 'settings.foo',
priority: 1,
}, {
key: 'settings.bar',
priority: 2,
}],
}, {
id: 'classNameOverride',
Expand Down

0 comments on commit 0720afb

Please sign in to comment.