import * as React from 'react'; import { ICommandBarItemProps, CommandBar } from '@fluentui/react/lib/CommandBar'; import { Check } from '@fluentui/react/lib/Check'; import { MarqueeSelection } from '@fluentui/react/lib/MarqueeSelection'; import { ISelection, Selection, SelectionMode, SelectionZone } from '@fluentui/react/lib/Selection'; import { IExampleItem, createListItems } from '@fluentui/example-data'; import { IRawStyle, mergeStyleSets } from '@fluentui/react/lib/Styling'; import { useConst, useForceUpdate } from '@fluentui/react-hooks'; interface ISelectionItemExampleProps { item: IExampleItem; itemIndex?: number; selection?: ISelection; selectionMode?: SelectionMode; } const commonStyles: IRawStyle = { display: 'inline-block', cursor: 'default', boxSizing: 'border-box', verticalAlign: 'top', background: 'none', backgroundColor: 'transparent', border: 'none', }; const classNames = mergeStyleSets({ item: { selectors: { '&:hover': { background: '#eee' }, }, }, // Overwrites the default style for Button check: [commonStyles, { padding: '11px 8px' }], cell: [ commonStyles, { overflow: 'hidden', height: 36, padding: 8, userSelect: 'none', }, ], }); const alertItem = (item: IExampleItem): void => { alert('item invoked: ' + item.name); }; const startsWithVowel = (item: IExampleItem): boolean => { return /^[aeiou]/.test(item.name || ''); }; const selectionModes = { [SelectionMode[SelectionMode.none]]: 'None', [SelectionMode[SelectionMode.single]]: 'Single select', [SelectionMode[SelectionMode.multiple]]: 'Multi select', }; const selectableItemTypes = { all: 'All items', vowels: 'Names starting with vowels', }; const ITEM_COUNT = 100; const SelectionItemExample: React.FunctionComponent = ( props: ISelectionItemExampleProps, ) => { const { item, itemIndex, selection } = props; let isSelected = false; if (selection && itemIndex !== undefined) { isSelected = selection.isIndexSelected(itemIndex); } return (
{selection && selection.canSelectItem(item) && selection.mode !== SelectionMode.none && (
)} {item.name} Link that avoids selection Link that selects first
); }; export const SelectionBasicExample: React.FunctionComponent = () => { const [selectableItemType, setSelectableItemType] = React.useState<'all' | 'vowels'>('all'); const [selectionMode, setSelectionMode] = React.useState(SelectionMode.multiple); const forceUpdate = useForceUpdate(); const items = useConst(() => createListItems(ITEM_COUNT)); const selection = React.useMemo( () => new Selection({ canSelectItem: selectableItemType === 'vowels' ? startsWithVowel : undefined, selectionMode, onSelectionChanged: forceUpdate, items, }), [selectableItemType, selectionMode, forceUpdate, items], ); const commandItems = React.useMemo( () => [ { key: 'selectionMode', text: 'Selection mode: ' + selectionModes[SelectionMode[selectionMode]], subMenuProps: { items: Object.keys(selectionModes).map((mode: keyof typeof SelectionMode) => ({ key: mode, name: selectionModes[mode], canCheck: true, checked: selectionMode === SelectionMode[mode], onClick: () => setSelectionMode(SelectionMode[mode]), })), }, }, { key: 'allowCanSelect', text: 'Selectable item type: ' + selectableItemType, subMenuProps: { items: Object.keys(selectableItemTypes).map((itemType: keyof typeof selectableItemTypes) => ({ key: itemType, name: selectableItemTypes[itemType], checked: selectableItemType === itemType, onClick: () => setSelectableItemType(itemType), })), }, }, { key: 'selectAll', text: 'Toggle select all', iconProps: { iconName: 'CheckMark' }, onClick: () => selection.toggleAllSelected(), disabled: selectionMode !== SelectionMode.multiple, }, ], [selectionMode, selection, selectableItemType], ); return (
{items.map((item: IExampleItem, index: number) => ( ))}
); };