-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow searching accountds based on more than one signature (#238)
* Allow searching accountds based on more than one signature * tidy * refactor * tidy * use json encoding * tidy
- Loading branch information
1 parent
4997934
commit 97c4de6
Showing
7 changed files
with
233 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Checkbox } from '@/common/forms' | ||
import { ConfirmationModal } from '@/common/modals/confirmation' | ||
import { | ||
ComponentProps, | ||
ReactNode, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
|
||
export type CheckboxesModalProps<T> = { | ||
title: string | ||
items: T[] | ||
itemCmp?: (a: T, b: T) => boolean | ||
itemLabel?: (item: T) => string | ReactNode | ||
label?: string | ||
value?: T[] | ||
required?: boolean | ||
confirmButtonText?: string | ||
onChange?: (value: T[]) => void | ||
onConfirm?: (value: T[]) => void | ||
} & ComponentProps<'button'> | ||
|
||
export function CheckboxesModal<T>({ | ||
title, | ||
value, | ||
required, | ||
onChange, | ||
onConfirm, | ||
items, | ||
itemCmp = (a, b) => a === b, | ||
itemLabel, | ||
confirmButtonText, | ||
|
||
// button props | ||
children, | ||
onClick, | ||
...buttonProps | ||
}: CheckboxesModalProps<T>) { | ||
const dialogRef = useRef<HTMLDivElement>(null) | ||
const [isOpen, setIsOpen] = useState(false) | ||
const [internal, setInternal] = useState<T[]>(value ?? []) | ||
|
||
const reset = useCallback(() => { | ||
setInternal(value ?? []) | ||
}, [value]) | ||
|
||
const updateValue = (newValue: T[]) => { | ||
setInternal(newValue) | ||
onChange?.(newValue) | ||
} | ||
|
||
const updateOpen = (isOpen: boolean) => { | ||
reset() | ||
setIsOpen(isOpen) | ||
} | ||
|
||
useEffect(() => { | ||
reset() | ||
}, [reset]) | ||
|
||
return ( | ||
<button | ||
{...buttonProps} | ||
onClick={(event) => { | ||
onClick?.(event) | ||
if ( | ||
!event.defaultPrevented && | ||
!dialogRef.current?.contains(event.target as Node) | ||
) { | ||
updateOpen(true) | ||
} | ||
}} | ||
> | ||
{children} | ||
<ConfirmationModal | ||
ref={dialogRef} | ||
isOpen={isOpen} | ||
title={title} | ||
confirmButtonText={confirmButtonText} | ||
confirmButtonDisabled={!!required && !internal.length} | ||
onConfirm={() => { | ||
if (internal.length || !required) { | ||
onConfirm?.(internal) | ||
updateOpen(false) | ||
} | ||
}} | ||
setIsOpen={updateOpen} | ||
> | ||
{items?.map((item, i) => ( | ||
<Checkbox | ||
key={i} | ||
className="mt-3 flex items-center" | ||
label={itemLabel?.(item) ?? `Item ${i + 1}`} | ||
checked={internal.some((other) => itemCmp(other, item))} | ||
onChange={(e) => { | ||
const filtered = internal.filter((other) => !itemCmp(other, item)) | ||
updateValue(e.target.checked ? [...filtered, item] : filtered) | ||
}} | ||
/> | ||
))} | ||
</ConfirmationModal> | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.