Skip to content

Commit

Permalink
feat(useManagedFiles): easyMultiSelect option
Browse files Browse the repository at this point in the history
allows multiple selections without holding modifier keys
  • Loading branch information
liamross committed Apr 16, 2020
1 parent decac19 commit f272fbe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/components/FileOrganizer/FileOrganizer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const UseManagedFilesHook = () => {
preventMultiSelect: boolean('preventMultiSelect', false, 'useManagedFiles options'),
preventDeselectOnDragOther: boolean('preventDeselectOnDragOther', false, 'useManagedFiles options'),
preventSelectOnDrag: boolean('preventSelectOnDrag', false, 'useManagedFiles options'),
easyMultiSelect: boolean('easyMultiSelect', false, 'useManagedFiles options'),
});

return (
Expand Down
27 changes: 15 additions & 12 deletions src/hooks/useManagedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface UseManagedFilesOptions<F> {
* Prevent deselecting all selected items when you drag an unselected item.
*/
preventDeselectOnDragOther?: boolean;
/**
* Allows multi-select by clicking other files without holding command/control.
*/
easyMultiSelect?: boolean;
}

export interface UseManagedFilesOutput<F> {
Expand Down Expand Up @@ -87,15 +91,14 @@ export interface UseManagedFilesOutput<F> {
* `FileOrganizer` component.
* @param options Options for managing files.
*/
export function useManagedFiles<F extends ObjectWithId>(options: UseManagedFilesOptions<F> = {}) {
const {
initialFiles,
preventMultiDrag,
preventDeselectOnDragOther,
preventSelectOnDrag,
preventMultiSelect,
} = options;

export function useManagedFiles<F extends ObjectWithId>({
initialFiles,
preventMultiDrag,
preventDeselectOnDragOther,
preventSelectOnDrag,
preventMultiSelect,
easyMultiSelect,
}: UseManagedFilesOptions<F> = {}) {
const [files, setFiles] = useState(initialFiles ?? []);

const addFile = useCallback((file: F, index?: number) => {
Expand Down Expand Up @@ -138,18 +141,18 @@ export function useManagedFiles<F extends ObjectWithId>(options: UseManagedFiles
}

if (toggleIndex === -1) {
if (multiKey) return [...prev, id];
if (easyMultiSelect || multiKey) return [...prev, id];
return [id];
}
if (toggleIndex !== -1) {
if (multiKey) return [...prev.slice(0, toggleIndex), ...prev.slice(toggleIndex + 1)];
if (easyMultiSelect || multiKey) return [...prev.slice(0, toggleIndex), ...prev.slice(toggleIndex + 1)];
if (prev.length > 1) return [id];
return [];
}
return prev;
});
},
[files, preventMultiSelect],
[easyMultiSelect, files, preventMultiSelect],
);

const onDeselectAll = useCallback(() => setSelectedIds([]), []);
Expand Down

0 comments on commit f272fbe

Please sign in to comment.