-
Notifications
You must be signed in to change notification settings - Fork 380
/
TaxonomyPanelContents.tsx
123 lines (115 loc) · 5.77 KB
/
TaxonomyPanelContents.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import * as React from 'react';
import styles from './TaxonomyPanelContents.module.scss';
import { IBasePickerStyleProps,
IBasePickerStyles,
IPickerItemProps,
IStyleFunctionOrObject,
ISuggestionItemProps,
Label,
Selection,
} from '@fluentui/react';
import { ITermInfo,
ITermSetInfo,
ITermStoreInfo
} from '@pnp/sp/taxonomy';
import { Guid } from '@microsoft/sp-core-library';
import * as strings from 'ControlStrings';
import { useForceUpdate } from '@uifabric/react-hooks';
import { ModernTermPicker } from '../modernTermPicker/ModernTermPicker';
import { IReadonlyTheme } from "@microsoft/sp-component-base";
import { IModernTermPickerProps } from '../modernTermPicker/ModernTermPicker.types';
import { Optional } from '../ModernTaxonomyPicker';
import { TaxonomyTree } from '../taxonomyTree/TaxonomyTree';
export interface ITaxonomyPanelContentsProps {
allowMultipleSelections?: boolean;
pageSize: number;
selectedPanelOptions: ITermInfo[];
setSelectedPanelOptions: React.Dispatch<React.SetStateAction<ITermInfo[]>>;
onResolveSuggestions: (filter: string, selectedItems?: ITermInfo[]) => ITermInfo[] | PromiseLike<ITermInfo[]>;
onLoadMoreData: (termSetId: Guid, parentTermId?: Guid, skiptoken?: string, hideDeprecatedTerms?: boolean, pageSize?: number) => Promise<{ value: ITermInfo[], skiptoken: string }>;
anchorTermInfo: ITermInfo;
termSetInfo: ITermSetInfo;
termStoreInfo: ITermStoreInfo;
placeHolder: string;
onRenderSuggestionsItem?: (props: ITermInfo, itemProps: ISuggestionItemProps<ITermInfo>) => JSX.Element;
onRenderItem?: (props: IPickerItemProps<ITermInfo>) => JSX.Element;
getTextFromItem: (item: ITermInfo, currentValue?: string) => string;
languageTag: string;
themeVariant?: IReadonlyTheme;
termPickerProps?: Optional<IModernTermPickerProps, 'onResolveSuggestions'>;
onRenderActionButton?: (termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo: ITermInfo, updateTaxonomyTreeViewCallback?: (newTermItems?: ITermInfo[], updatedTermItems?: ITermInfo[], deletedTermItems?: ITermInfo[]) => void) => JSX.Element;
allowSelectingChildren?: boolean;
}
export function TaxonomyPanelContents(props: ITaxonomyPanelContentsProps): React.ReactElement<ITaxonomyPanelContentsProps> {
const [terms, setTerms] = React.useState<ITermInfo[]>(props.selectedPanelOptions?.length > 0 ? [...props.selectedPanelOptions] : []);
const forceUpdate = useForceUpdate();
const selection = React.useMemo(() => {
const s = new Selection({
onSelectionChanged: () => {
props.setSelectedPanelOptions((prevOptions) => [...selection.getSelection()]);
forceUpdate();
}, getKey: (term: any) => term.id // eslint-disable-line @typescript-eslint/no-explicit-any
});
s.setItems(terms);
for (const selectedOption of props.selectedPanelOptions) {
if (s.canSelectItem) {
s.setKeySelected(selectedOption.id.toString(), true, true);
}
}
return s;
}, [terms]);
const onPickerChange = (items?: ITermInfo[]): void => {
const itemsToAdd = items.filter((item) => terms.every((term) => term.id !== item.id));
setTerms((prevTerms) => [...prevTerms, ...itemsToAdd]);
selection.setItems([...selection.getItems(), ...itemsToAdd], true);
for (const item of items) {
if (selection.canSelectItem(item)) {
selection.setKeySelected(item.id.toString(), true, false);
}
}
};
const termPickerStyles: IStyleFunctionOrObject<IBasePickerStyleProps, IBasePickerStyles> = { root: {paddingTop: 4, paddingBottom: 4, paddingRight: 4, minheight: 34}, input: {minheight: 34}, text: { minheight: 34, borderStyle: 'none', borderWidth: '0px' } };
return (
<div className={styles.taxonomyPanelContents}>
<div className={styles.taxonomyTreeSelector}>
<div>
<ModernTermPicker
{...props.termPickerProps}
removeButtonAriaLabel={strings.ModernTaxonomyPickerRemoveButtonText}
onResolveSuggestions={props.termPickerProps?.onResolveSuggestions ?? props.onResolveSuggestions}
itemLimit={props.allowMultipleSelections ? undefined : 1}
selectedItems={props.selectedPanelOptions}
styles={props.termPickerProps?.styles ?? termPickerStyles}
onChange={onPickerChange}
getTextFromItem={props.getTextFromItem}
pickerSuggestionsProps={props.termPickerProps?.pickerSuggestionsProps ?? { noResultsFoundText: strings.ModernTaxonomyPickerNoResultsFound }}
inputProps={props.termPickerProps?.inputProps ?? {
'aria-label': props.placeHolder || strings.ModernTaxonomyPickerDefaultPlaceHolder,
placeholder: props.placeHolder || strings.ModernTaxonomyPickerDefaultPlaceHolder
}}
onRenderSuggestionsItem={props.termPickerProps?.onRenderSuggestionsItem ?? props.onRenderSuggestionsItem}
onRenderItem={props.onRenderItem}
themeVariant={props.themeVariant}
/>
</div>
</div>
<Label className={styles.taxonomyTreeLabel}>{props.allowMultipleSelections ? strings.ModernTaxonomyPickerTreeTitleMulti : strings.ModernTaxonomyPickerTreeTitleSingle}</Label>
<TaxonomyTree
anchorTermInfo={props.anchorTermInfo}
languageTag={props.languageTag}
onLoadMoreData={props.onLoadMoreData}
pageSize={props.pageSize}
selection={selection}
setTerms={setTerms}
termSetInfo={props.termSetInfo}
termStoreInfo={props.termStoreInfo}
terms={terms}
allowMultipleSelections={props.allowMultipleSelections}
onRenderActionButton={props.onRenderActionButton}
hideDeprecatedTerms={true}
showIcons={false}
allowSelectingChildren={props.allowSelectingChildren}
/>
</div>
);
}