Support checking all

This commit is contained in:
Dane Everitt 2020-07-11 17:09:54 -07:00
parent cb3288500a
commit 3df4d433f0
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 20 additions and 2 deletions

View file

@ -3,6 +3,8 @@ import { ServerContext } from '@/state/server';
import { NavLink } from 'react-router-dom'; import { NavLink } from 'react-router-dom';
import { cleanDirectoryPath } from '@/helpers'; import { cleanDirectoryPath } from '@/helpers';
import tw from 'twin.macro'; import tw from 'twin.macro';
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
interface Props { interface Props {
withinFileEditor?: boolean; withinFileEditor?: boolean;
@ -14,6 +16,10 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
const id = ServerContext.useStoreState(state => state.server.data!.id); const id = ServerContext.useStoreState(state => state.server.data!.id);
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
const { data: files } = useFileManagerSwr();
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
useEffect(() => { useEffect(() => {
const parts = cleanDirectoryPath(window.location.hash).split('/'); const parts = cleanDirectoryPath(window.location.hash).split('/');
@ -32,8 +38,20 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
return { name: decodeURIComponent(directory), path: `/${dirs.slice(0, index + 1).join('/')}` }; return { name: decodeURIComponent(directory), path: `/${dirs.slice(0, index + 1).join('/')}` };
}); });
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
};
return ( return (
<div css={tw`flex items-center text-sm mb-4 text-neutral-500`}> <div css={tw`flex items-center text-sm mb-4 text-neutral-500`}>
{!!(files && files.length) &&
<FileActionCheckbox
type={'checkbox'}
css={tw`mx-4`}
checked={selectedFilesLength === (files ? files.length : -1)}
onChange={onSelectAllClick}
/>
}
/<span css={tw`px-1 text-neutral-300`}>home</span>/ /<span css={tw`px-1 text-neutral-300`}>home</span>/
<NavLink <NavLink
to={`/server/${id}/files`} to={`/server/${id}/files`}

View file

@ -4,7 +4,7 @@ import { ServerContext } from '@/state/server';
import styled from 'styled-components/macro'; import styled from 'styled-components/macro';
import Input from '@/components/elements/Input'; import Input from '@/components/elements/Input';
const Checkbox = styled(Input)` export const FileActionCheckbox = styled(Input)`
&& { && {
${tw`border-neutral-500`}; ${tw`border-neutral-500`};
@ -21,7 +21,7 @@ export default ({ name }: { name: string }) => {
return ( return (
<label css={tw`flex-none p-4 absolute self-center z-30 cursor-pointer`}> <label css={tw`flex-none p-4 absolute self-center z-30 cursor-pointer`}>
<Checkbox <FileActionCheckbox
name={'selectedFiles'} name={'selectedFiles'}
value={name} value={name}
checked={isChecked} checked={isChecked}