From 034b4ad3b0a3c06ffcfbadae50bdd4e4c88e915c Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 27 Feb 2022 13:32:22 -0500 Subject: [PATCH] Hook up the select-all checkboxes --- .../components/admin/users/UserTableRow.tsx | 6 +- .../admin/users/UsersContainerV2.tsx | 62 ++++++++++++++----- .../elements/transitions/FadeTransition.tsx | 31 ++++++++++ .../components/elements/transitions/index.ts | 8 +++ 4 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 resources/scripts/components/elements/transitions/FadeTransition.tsx create mode 100644 resources/scripts/components/elements/transitions/index.ts diff --git a/resources/scripts/components/admin/users/UserTableRow.tsx b/resources/scripts/components/admin/users/UserTableRow.tsx index d076f2ded..42ed0e36c 100644 --- a/resources/scripts/components/admin/users/UserTableRow.tsx +++ b/resources/scripts/components/admin/users/UserTableRow.tsx @@ -8,9 +8,11 @@ import { Button } from '@/components/elements/button/index'; interface Props { user: User; + selected?: boolean; + onRowChange: (user: User, selected: boolean) => void; } -const UserTableRow = ({ user }: Props) => { +const UserTableRow = ({ user, selected, onRowChange }: Props) => { const [ visible, setVisible ] = useState(false); return ( @@ -30,7 +32,7 @@ const UserTableRow = ({ user }: Props) => {
- + onRowChange(user, e.currentTarget.checked)}/>
diff --git a/resources/scripts/components/admin/users/UsersContainerV2.tsx b/resources/scripts/components/admin/users/UsersContainerV2.tsx index cd010ac5b..32f41ed52 100644 --- a/resources/scripts/components/admin/users/UsersContainerV2.tsx +++ b/resources/scripts/components/admin/users/UsersContainerV2.tsx @@ -1,6 +1,8 @@ -import React, { useEffect, useState } from 'react'; +import React, { Fragment, useEffect, useState } from 'react'; import http from '@/api/http'; +import { UUID } from '@/api/definitions'; import { Transformers, User } from '@definitions/admin'; +import { Transition } from '@/components/elements/transitions'; import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid'; import { Button } from '@/components/elements/button/index'; import { Checkbox, InputField } from '@/components/elements/inputs'; @@ -8,6 +10,8 @@ import UserTableRow from '@/components/admin/users/UserTableRow'; const UsersContainerV2 = () => { const [ users, setUsers ] = useState([]); + const [ selected, setSelected ] = useState([]); + useEffect(() => { document.title = 'Admin | Users'; }, []); @@ -20,6 +24,15 @@ const UsersContainerV2 = () => { .catch(console.error); }, []); + const onRowChange = (user: User, checked: boolean) => { + setSelected((state) => { + return checked ? [ ...state, user.uuid ] : selected.filter((uuid) => uuid !== user.uuid); + }); + }; + + const selectAllChecked = users.length > 0 && selected.length > 0; + const onSelectAll = () => setSelected((state) => state.length > 0 ? [] : users.map(({ uuid }) => uuid)); + return (
@@ -29,7 +42,11 @@ const UsersContainerV2 = () => {
- +
{ className={'w-56 focus:w-96'} />
-
-
- + 0} duration={'duration-75'}> +
+
+ +
+ + + + + + + + +
- - - - - - - - - -
+
@@ -64,7 +87,14 @@ const UsersContainerV2 = () => { - {users.map(user => )} + {users.map(user => ( + + ))}
diff --git a/resources/scripts/components/elements/transitions/FadeTransition.tsx b/resources/scripts/components/elements/transitions/FadeTransition.tsx new file mode 100644 index 000000000..d53acc473 --- /dev/null +++ b/resources/scripts/components/elements/transitions/FadeTransition.tsx @@ -0,0 +1,31 @@ +import React from 'react'; +import { Transition } from '@headlessui/react'; + +type Duration = `duration-${number}`; + +interface Props { + as?: React.ElementType; + duration?: Duration | [ Duration, Duration ]; + show: boolean; + children: React.ReactNode; +} + +export default ({ children, duration, ...props }: Props) => { + const [ enterDuration, exitDuration ] = Array.isArray(duration) + ? duration + : (!duration ? [ 'duration-200', 'duration-100' ] : [ duration, duration ]); + + return ( + + {children} + + ); +}; diff --git a/resources/scripts/components/elements/transitions/index.ts b/resources/scripts/components/elements/transitions/index.ts new file mode 100644 index 000000000..98060223d --- /dev/null +++ b/resources/scripts/components/elements/transitions/index.ts @@ -0,0 +1,8 @@ +import { Transition as TransitionComponent } from '@headlessui/react'; +import FadeTransition from '@/components/elements/transitions/FadeTransition'; + +const Transition = Object.assign(TransitionComponent, { + Fade: FadeTransition, +}); + +export { Transition };