import { Box, DefaultMantineColor, Flex, Input, MantineSpacing, Paper, Select, StyleProp, Switch, Text } from "@mantine/core" import classes from './SettingsInput.module.css' import { Children, cloneElement, HTMLInputTypeAttribute, isValidElement, MouseEvent, ReactNode, useEffect, useRef, useState } from "react"; import { useRosettaColors } from "@/app/hooks/useRosettaColors"; import { useClipboard } from "@mantine/hooks"; import { IconChevronRight } from "@tabler/icons-react"; export function SettingsInput() {} export interface SettingsInputCopy { hit: string; placeholder?: string; value?: string; style?: any; mt?: StyleProp; } export interface SettingsInputDefaultProps { hit: string; placeholder?: string; value?: string; disabled?: boolean; onChange?: (event : any) => void; style?: any; mt?: StyleProp; rightSection?: ReactNode; type?: HTMLInputTypeAttribute; } export interface SettingsInputGroupProps { mt?: StyleProp; children: any; } export interface SettingsInputClickableProps { onClick: () => void; hit: string; placeholder?: string; style?: any; mt?: StyleProp; value?: string; rightSection?: ReactNode; c?: StyleProp; rightChevronHide?: boolean; settingsIcon?: React.ReactNode; } export interface SettingsInputSelectProps { hit: string; variants: string[]; mt?: StyleProp; style?: any; leftSection?: ReactNode; onChange?: (value: string|undefined) => void; width?: number; defaultValue?: string; } export interface SettingsInputSwitch { hit: string; mt?: StyleProp; style?: any; onChange?: (value: boolean) => void; defaultValue: boolean; } SettingsInput.Copy = SettingsInputCopy; SettingsInput.Clickable = SettingsInputClickable; SettingsInput.Default = SettingsInputDefault; SettingsInput.Group = SettingsInputGroup; SettingsInput.Select = SettingsInputSelect; SettingsInput.Switch = SettingsInputSwitch; function SettingsInputSwitch(props: SettingsInputSwitch) { const colors = useRosettaColors(); const [checked, setChecked] = useState(props.defaultValue); useEffect(() => { setChecked(props.defaultValue); }, [props.defaultValue]); const onSwitch = (checked: boolean) => { if(props.onChange){ props.onChange(checked); } setChecked(checked); } return ( {props.hit}
onSwitch(event.currentTarget.checked)} size="sm" />
); } function SettingsInputSelect(props: SettingsInputSelectProps) { const colors = useRosettaColors(); const [value, setValue] = useState(props.defaultValue); const onChange = (selectValue : any) => { if(selectValue != value && selectValue != null){ props.onChange!(selectValue); setValue(selectValue); } } return ( {props.hit}
); } function SettingsInputCopy(props : SettingsInputCopy) { const colors = useRosettaColors(); const {copied, copy} = useClipboard({ timeout: 1500 }); const onClick = (e : MouseEvent) => { e.stopPropagation(); copy(props.value); } return ( {props.hit}
{!copied && ( )} {copied && ( )}
); } function SettingsInputClickable( props : SettingsInputClickableProps ) { const colors = useRosettaColors(); const onClick = (e : MouseEvent) => { e.stopPropagation(); props.onClick(); } return ( {props.settingsIcon} {props.hit}
{props.rightSection && ( )} {!props.rightSection && ( ) } {!props.rightChevronHide && ()}
); } function SettingsInputDefault(props : SettingsInputDefaultProps) { const colors = useRosettaColors(); const input = useRef(undefined); const onClick = (e : MouseEvent) => { e.stopPropagation(); if(!props.disabled){ input.current.focus(); return; } } return (<> {props.hit}
{props.rightSection && ( )} {!props.rightSection && ( { onClick(e) }} onChange={props.onChange} variant={'unstyled'} spellCheck={false} color="gray" classNames={{ input: classes.input }} placeholder={props.placeholder}>) }
) } function SettingsInputGroup(props : SettingsInputGroupProps) { const colors = useRosettaColors(); const childrenArray = Children.toArray(props.children).filter( (child): child is React.ReactElement<{ style?: React.CSSProperties }> => isValidElement(child) ); return ( {childrenArray.map((child, index) => { const isFirst = index === 0; const isLast = index === childrenArray.length - 1; return cloneElement(child, { style: { borderRadius: isFirst ? "var(--mantine-radius-default) var(--mantine-radius-default) 0 0" : isLast ? "0 0 var(--mantine-radius-default) var(--mantine-radius-default)" : "0", borderTop: isLast ? 'unset' : '1px solid ' + colors.borderColor, borderBottom: isFirst ? '1px solid ' + colors.borderColor : '1px solid ' + colors.borderColor, ...(child.props.style || {}), }, }); })} ); }