Skip to content

Commit

Permalink
Fix tree select item rendering (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Oct 2, 2024
1 parent 74ad192 commit 9c62096
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions client/components/fields/editor/CustomVocabularies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {ISubject, IVocabulary} from 'superdesk-api';
import {superdeskApi} from '../../../superdeskApi';
import {IEditorFieldProps, IProfileSchemaTypeList} from '../../../interfaces';
import {Row} from '../../UI/Form';
import {getVocabularyItemNameFromString} from '../../../utils/vocabularies';
import {EditorFieldTreeSelect} from '../editor/base/treeSelect';
import {getVocabularyItemFieldTranslated} from '../../../utils/vocabularies';
import {arrayToTree} from 'superdesk-core/scripts/core/helpers/tree';
import {TreeSelect} from 'superdesk-ui-framework/react';

interface IProps extends IEditorFieldProps {
schema?: IProfileSchemaTypeList;
Expand All @@ -33,6 +34,8 @@ class CustomVocabulariesComponent extends React.PureComponent<IProps> {
required,
testId,
language,
disabled,
invalid,
} = this.props;

const customVocabularies = vocabularies.filter((cv) =>
Expand All @@ -41,43 +44,49 @@ class CustomVocabulariesComponent extends React.PureComponent<IProps> {

return customVocabularies.map((cv) => {
const cvFieldName = `custom_vocabularies.${cv._id}`;
const parentField = cv.schema_field || 'subject';
const itemFieldName = cv.schema_field ?? 'subject';

return (
<Row
key={cv._id}
id={`form-row-${cvFieldName}`}
data-test-id={testId?.length ? `${testId}.${cv._id}` : cv._id}
>
<EditorFieldTreeSelect
filterScheme={(values) => values.filter((value) => cv._id == null || value?.scheme === cv._id)}
item={item}
field={parentField}
label={gettext(cv.display_name)}
required={required || schema?.required}
allowMultiple={true}
<TreeSelect
sortable={true}
getOptions={() => cv.items.map((item: ISubject) => ({value: {...item, scheme: cv._id}}))}
getId={(item: ISubject) => item.qcode}
getLabel={(item: ISubject) => (
getVocabularyItemNameFromString(
item.qcode,
cv.items,
'qcode',
'name',
language
)
kind="synchronous"
allowMultiple={true}
value={item.subject.filter((x) => x.scheme === cv._id)}
label={gettext(cv.display_name)}
required={required ?? schema?.required}
getOptions={() => arrayToTree(
cv.items.map((cvItem) => ({
...cvItem,
scheme: cv._id,
})) as Array<ISubject>,
({qcode}) => qcode.toString(),
({parent}) => parent?.toString(),
).result}
getLabel={(item) => getVocabularyItemFieldTranslated(
item,
'name',
language,
)}
onChange={(field, value) => {
const otherCvValues = item[parentField] ?? [];
getId={(item) => item.qcode}
invalid={errors?.length > 0 || invalid}
error={showErrors ? errors[itemFieldName] : undefined}
readOnly={disabled}
disabled={disabled}
onChange={(vals) => {
const restOfItems = (item.subject ?? []).filter((x) => x.scheme !== cv._id);

const newValues = value.concat(
otherCvValues.filter((value) => value?.scheme != cv._id)
onChange(
'subject',
[...restOfItems, ...vals],
);

onChange(field, newValues);
}}
errors={errors}
tabindex={0}
zIndex={1051}
/>
</Row>
);
Expand Down

0 comments on commit 9c62096

Please sign in to comment.