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

Enable Parent Item Selection In Dropdown #4705

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion scripts/apps/authoring-react/field-adapters/authors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const authors: IFieldAdapter<IArticle> = {
lookup: {},
});
},
canSelectBranchWithChildren: () => false,
getLabel: (item: IUserOption | IAuthorRole) => item.name,
valueTemplate: valueTemplate,
optionTemplate: optionTemplate,
Expand Down
68 changes: 19 additions & 49 deletions scripts/apps/authoring-react/field-adapters/subject.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import {mapValues, memoize} from 'lodash';
import {IArticle, IAuthoringFieldV2, IFieldAdapter, IDropdownTreeConfig, ITreeWithLookup} from 'superdesk-api';
import {
IArticle,
IAuthoringFieldV2,
IFieldAdapter,
ISubjectCode,
IDropdownConfigManualSource,
} from 'superdesk-api';
import {gettext} from 'core/utils';
import {arrayToTree, sortTree} from 'core/helpers/tree';
import {store} from 'core/data';

type ISubjectCode = {qcode: string; name: string; parent?: string};

export function getSubjectAdapter(): IFieldAdapter<IArticle> {
const getItems: () => ITreeWithLookup<ISubjectCode> = memoize(() => {
const subjectCodes = store.getState().subjectCodes;

return ({
nodes: sortTree(
arrayToTree(
Object.values(subjectCodes),
(item) => item.qcode,
(item) => item.parent ?? null,
).result,
(a, b) => a.name.localeCompare(b.name),
),
lookup: mapValues(subjectCodes, (value) => ({value})),
});
});

return {
getFieldV2: (fieldEditor, fieldSchema) => {
const fieldConfig: IDropdownTreeConfig = {
source: 'dropdown-tree',
getItems,
getLabel: (item: ISubjectCode) => item.name,
getId: (item: ISubjectCode) => item.qcode,
canSelectBranchWithChildren: () => true,
getFieldV2: () => {
const fieldConfig: IDropdownConfigManualSource = {
source: 'manual-entry',
options: Object.values(store.getState().subjectCodes)
.map((x) => ({id: x.qcode, label: x.name, parent: x.parent})),
roundCorners: true,
type: 'text',
canSelectBranchWithChildren: true,
multiple: true,
};

Expand All @@ -43,30 +30,13 @@ export function getSubjectAdapter(): IFieldAdapter<IArticle> {

return fieldV2;
},
retrieveStoredValue: (article): Array<ISubjectCode> => {
return (article.subject ?? [])
.filter(({scheme}) => scheme == null) // filter out custom vocabulary data
.map(({qcode, name, parent}) => ({qcode, name, parent}));
retrieveStoredValue: (article): Array<ISubjectCode['qcode']> => {
return (article.subject ?? []).map((x) => x.qcode);
},
storeValue: (val: Array<ISubjectCode>, article) => {
interface IStorageFormat {
qcode: string;
name: string;
parent?: string;
scheme: string;
}

storeValue: (val: Array<ISubjectCode['qcode']>, article) => {
return {
...article,
subject: val.map(({qcode, name, parent}) => {
var itemToStore: IStorageFormat = {qcode, name, parent, scheme: null};

if (parent != null) {
itemToStore.parent = parent;
}

return itemToStore;
}),
subject: Object.values(store.getState().subjectCodes).filter((x) => val.includes(x.qcode)),
};
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class EditorUsingManualSourceOrVocabulary extends React.PureComponent<IPr
kind="synchronous"
getOptions={() => options}
values={selected}
canSelectBranchWithChildren={config.canSelectBranchWithChildren}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep capability to allow/disallow based on a tree node

canSelectBranchWithChildren?(branch: ITreeNode<unknown>): boolean;

Copy link
Contributor Author

@thecalcc thecalcc Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not supported in uif component, we talked about it and decided to leave as is

onChange={(_values) => {
const ids = _values.map((val) => val.id);

Expand Down
10 changes: 8 additions & 2 deletions scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ declare module 'superdesk-api' {
vocabularyId: IVocabulary['_id'];
multiple: boolean;
filter?(vocabulary: IVocabularyItem): boolean;
canSelectBranchWithChildren?: boolean;
}

export interface IDropdownConfigRemoteSource extends ICommonFieldConfig {
Expand All @@ -366,18 +367,22 @@ declare module 'superdesk-api' {
): void;
getLabel(item: unknown): string;
getId(item: unknown): string;
canSelectBranchWithChildren?(branch: ITreeNode<unknown>): boolean;
canSelectBranchWithChildren?: boolean;
optionTemplate?: React.ComponentType<{item: unknown}>;
valueTemplate?: React.ComponentType<{item: unknown}>;
multiple: boolean;
}

/**
* Prioritize using IDropdownConfigManualSource/IDropdownConfigVocabulary/IDropdownConfigRemoteSource
* if any of those can satisfy your use-case
*/
export interface IDropdownTreeConfig extends ICommonFieldConfig {
source: 'dropdown-tree';
getItems(): ITreeWithLookup<unknown>;
getLabel(item: unknown): string;
getId(item: unknown): string;
canSelectBranchWithChildren?(branch: ITreeNode<unknown>): boolean;
canSelectBranchWithChildren?: boolean;
optionTemplate?: React.ComponentType<{item: unknown}>;
valueTemplate?: React.ComponentType<{item: unknown}>;
multiple: boolean;
Expand All @@ -389,6 +394,7 @@ declare module 'superdesk-api' {
options: Array<IDropdownOption>;
roundCorners: boolean;
multiple: boolean;
canSelectBranchWithChildren?: boolean;
}

export type IDropdownConfig =
Expand Down
6 changes: 3 additions & 3 deletions scripts/core/ui/components/MultiSelectTreeWithTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IPropsBase<T> {
valueTemplate?: React.ComponentType<{item: T}>;
getId(item: T): string;
getLabel(item: T): string;
canSelectBranchWithChildren?(branch: ITreeNode<T>): boolean;
canSelectBranchWithChildren?: boolean;
allowMultiple?: boolean;
readOnly?: boolean;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ export class MultiSelectTreeWithTemplate<T> extends React.PureComponent<IProps<T
}}
getLabel={getLabel}
getId={getId}
selectBranchWithChildren={false}
selectBranchWithChildren={props.canSelectBranchWithChildren ?? false}
optionTemplate={(item) => <OptionTemplate item={item} />}
valueTemplate={(item, Wrapper) => <Wrapper><ValueTemplate item={item} /></Wrapper>}
allowMultiple={this.props.allowMultiple}
Expand All @@ -84,7 +84,7 @@ export class MultiSelectTreeWithTemplate<T> extends React.PureComponent<IProps<T
}}
getLabel={getLabel}
getId={getId}
selectBranchWithChildren={false}
selectBranchWithChildren={props.canSelectBranchWithChildren ?? false}
optionTemplate={(item) => <OptionTemplate item={item} />}
valueTemplate={(item) => <ValueTemplate item={item} />}
allowMultiple={this.props.allowMultiple}
Expand Down
Loading