feat: scope nested form

This commit is contained in:
SevicheCC 2023-06-05 19:17:30 +08:00
parent f2ca97238b
commit 2f34f8fc50
Signed by: SevicheCC
GPG key ID: C577000000000000
3 changed files with 45 additions and 10 deletions

View file

@ -69,7 +69,7 @@ const scopesInfo: ScopeInfo[] = [
}, },
{ {
method: "crypto", method: "crypto",
label: "Crypto", label: "Crypto",
description: "use end-to-end encryption", description: "use end-to-end encryption",
}, },
]; ];
@ -89,6 +89,7 @@ const InputForm = () => {
instance: "https://", instance: "https://",
clientName: "", clientName: "",
redirectUris: "", redirectUris: "",
scopes:[]
}, },
}); });

View file

@ -1,4 +1,4 @@
'use client' "use client";
import { MethodType } from "./InputForm"; import { MethodType } from "./InputForm";
import { Checkbox } from "@/components/ui/checkbox"; import { Checkbox } from "@/components/ui/checkbox";
@ -6,15 +6,28 @@ import { Checkbox } from "@/components/ui/checkbox";
interface ScopeCheckboxProps { interface ScopeCheckboxProps {
scope: string; scope: string;
method: MethodType; method: MethodType;
field: any;
} }
const ScopeItem: React.FC<ScopeCheckboxProps> = ({ scope, method }) => { const ScopeItem: React.FC<ScopeCheckboxProps> = ({ scope, method, field }) => {
return ( return (
<div className={`items-top flex space-x-2 hover:cursor-pointer`}> <div className={`items-top flex space-x-2 hover:cursor-pointer`}>
<Checkbox id={`${scope}`} /> <Checkbox
id={scope}
checked={field.value?.includes(scope)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, scope])
: field.onChange(
field.value?.filter((value: string) => value !== scope)
);
}}
/>
<div className="grid gap-1.5 leading-none"> <div className="grid gap-1.5 leading-none">
<label <label
htmlFor={`${scope}`} htmlFor={scope}
className="text-sm font-medium leading-none hover:cursor-pointer peer-disabled:cursor-not-allowed peer-disabled:opacity-70" className="text-sm font-medium leading-none hover:cursor-pointer peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
> >
{method == "admin" && ( {method == "admin" && (
@ -27,4 +40,4 @@ const ScopeItem: React.FC<ScopeCheckboxProps> = ({ scope, method }) => {
); );
}; };
export default ScopeItem; export default ScopeItem;

View file

@ -19,11 +19,22 @@ interface ScopeSectionProps {
const ScopeSection: React.FC<ScopeSectionProps> = ({ info, field }) => { const ScopeSection: React.FC<ScopeSectionProps> = ({ info, field }) => {
const { method, description, scopes, label } = info; const { method, description, scopes, label } = info;
console.log(field);
return ( return (
<Collapsible className="flex flex-col rounded-md bg-slate-50 px-4 py-3"> <Collapsible className="flex flex-col rounded-md bg-slate-50 px-4 py-3">
<div className="flex justify-between"> <div className="flex justify-between">
<div className="items-top flex space-x-2 "> <div className="items-top flex space-x-2 ">
<Checkbox id={method} /> <Checkbox
id={method}
checked={field.value?.includes(method)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, method])
: field.onChange(
field.value?.filter((value: string) => value !== method)
);
}}
/>
<div className="grid gap-1.5 leading-none"> <div className="grid gap-1.5 leading-none">
<label <label
htmlFor={method} htmlFor={method}
@ -54,15 +65,25 @@ const ScopeSection: React.FC<ScopeSectionProps> = ({ info, field }) => {
? (scopes as string[][]).map((items) => ( ? (scopes as string[][]).map((items) => (
<div <div
key={`${items}${method}`} key={`${items}${method}`}
className="flex flex-col gap-2" className="mb-2 flex flex-col gap-2 md:mb-0"
> >
{items.map((item) => ( {items.map((item) => (
<ScopeItem scope={item} key={item} method={method} /> <ScopeItem
scope={item}
key={item}
method={method}
field={field}
/>
))} ))}
</div> </div>
)) ))
: (scopes as string[]).map((scope) => ( : (scopes as string[]).map((scope) => (
<ScopeItem scope={scope} key={scope} method={method} /> <ScopeItem
scope={scope}
key={scope}
method={method}
field={field}
/>
))} ))}
</div> </div>
</CollapsibleContent> </CollapsibleContent>