Skip to content

Commit 3eb0827

Browse files
committed
selection
1 parent f60eb70 commit 3eb0827

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/controls/sitePicker/SitePicker.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { getAllSites, getHubSites } from '../../services/SPSitesService';
66
import { IDropdownOption, Dropdown } from 'office-ui-fabric-react/lib/Dropdown';
77
import { ISelectableOption, SelectableOptionMenuItemType } from 'office-ui-fabric-react/lib/utilities/selectableOption/SelectableOption.types';
88
import orderBy from 'lodash/orderBy';
9+
import findIndex from 'lodash/findIndex';
910
import { SearchBox } from 'office-ui-fabric-react/lib/SearchBox';
10-
import { Checkbox } from 'office-ui-fabric-react/lib/Checkbox';
1111
import { toRelativeUrl } from '../../common/utilities/GeneralHelper';
1212

1313
const styles = mergeStyleSets({
@@ -110,6 +110,30 @@ export const SitePicker: React.FunctionComponent<ISitePickerProps> = (props: Rea
110110
return result;
111111
};
112112

113+
const onSelectionChange = React.useCallback((e, item: IDropdownOption, index: number) => {
114+
const newSelectedSites = selectedSites ? [...selectedSites] : [];
115+
116+
if (multiSelect !== false) {
117+
const existingIndex = findIndex(newSelectedSites, s => s.id === item.key);
118+
119+
if (existingIndex >= 0) {
120+
newSelectedSites.splice(existingIndex, 1);
121+
}
122+
else {
123+
newSelectedSites.push({
124+
...item.data!
125+
});
126+
}
127+
}
128+
129+
if (onChange) {
130+
onChange(newSelectedSites);
131+
}
132+
133+
setSelectedSites(newSelectedSites);
134+
135+
}, [selectedSites, multiSelect, onChange]);
136+
113137
React.useEffect(() => {
114138
if (!initialSites) {
115139
return;
@@ -180,6 +204,7 @@ export const SitePicker: React.FunctionComponent<ISitePickerProps> = (props: Rea
180204
disabled={disabled}
181205
multiSelect={multiSelect !== false}
182206
onRenderOption={onRenderOption}
207+
onChange={onSelectionChange}
183208
/>
184209
</>
185210
);

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
13181318
mode={'web'}
13191319
allowSearch={true}
13201320
multiSelect={true}
1321-
onChange={() => {}} />
1321+
onChange={(sites) => { console.log(sites); }} />
13221322
</div>
13231323

13241324
<div className="ms-font-m">List picker tester:

0 commit comments

Comments
 (0)