diff --git a/frontend/app/element/input.tsx b/frontend/app/element/input.tsx index 9b550a53d..3850af2fc 100644 --- a/frontend/app/element/input.tsx +++ b/frontend/app/element/input.tsx @@ -1,13 +1,25 @@ -import { clsx } from "clsx"; -import React, { forwardRef, useEffect, useRef, useState } from "react"; - +import clsx from "clsx"; +import React, { forwardRef, ReactNode } from "react"; import "./input.less"; -interface InputDecorationProps { - startDecoration?: React.ReactNode; - endDecoration?: React.ReactNode; +interface InputLeftElementProps { + children: React.ReactNode; + className?: string; } +const InputLeftElement = ({ children, className }: InputLeftElementProps) => { + return
{children}
; +}; + +interface InputRightElementProps { + children: React.ReactNode; + className?: string; +} + +const InputRightElement = ({ children, className }: InputRightElementProps) => { + return
{children}
; +}; + interface InputProps { label?: string; value?: string; @@ -18,13 +30,13 @@ interface InputProps { onBlur?: () => void; placeholder?: string; defaultValue?: string; - decoration?: InputDecorationProps; required?: boolean; maxLength?: number; autoFocus?: boolean; disabled?: boolean; isNumber?: boolean; inputRef?: React.MutableRefObject; + children?: ReactNode; } const Input = forwardRef( @@ -39,65 +51,17 @@ const Input = forwardRef( onBlur, placeholder, defaultValue = "", - decoration, required, maxLength, autoFocus, disabled, isNumber, inputRef, + children, }: InputProps, ref ) => { - const [focused, setFocused] = useState(false); - const [internalValue, setInternalValue] = useState(defaultValue); - const [error, setError] = useState(false); - const [hasContent, setHasContent] = useState(Boolean(value || defaultValue)); - const internalInputRef = useRef(null); - - useEffect(() => { - if (value !== undefined) { - setFocused(Boolean(value)); - } - }, [value]); - - const handleComponentFocus = () => { - if (internalInputRef.current && !internalInputRef.current.contains(document.activeElement)) { - internalInputRef.current.focus(); - } - }; - - const handleComponentBlur = () => { - if (internalInputRef.current?.contains(document.activeElement)) { - internalInputRef.current.blur(); - } - }; - - const handleSetInputRef = (elem: HTMLInputElement) => { - if (inputRef) { - inputRef.current = elem; - } - internalInputRef.current = elem; - }; - - const handleFocus = () => { - setFocused(true); - onFocus && onFocus(); - }; - - const handleBlur = () => { - if (internalInputRef.current) { - const inputValue = internalInputRef.current.value; - if (required && !inputValue) { - setError(true); - setFocused(false); - } else { - setError(false); - setFocused(false); - } - } - onBlur && onBlur(); - }; + const [internalValue, setInternalValue] = React.useState(defaultValue); const handleInputChange = (e: React.ChangeEvent) => { const inputValue = e.target.value; @@ -106,14 +70,6 @@ const Input = forwardRef( return; } - if (required && !inputValue) { - setError(true); - setHasContent(false); - } else { - setError(false); - setHasContent(Boolean(inputValue)); - } - if (value === undefined) { setInternalValue(inputValue); } @@ -123,54 +79,49 @@ const Input = forwardRef( const inputValue = value ?? internalValue; + let leftElement = null; + let rightElement = null; + React.Children.forEach(children, (child) => { + if (React.isValidElement(child)) { + if (child.type === InputLeftElement) { + leftElement = child; + } else if (child.type === InputRightElement) { + rightElement = child; + } + } + }); + return (
- {decoration?.startDecoration && <>{decoration.startDecoration}}
- {label && ( - - )} + {leftElement &&
{leftElement}
} + {rightElement &&
{rightElement}
}
- {decoration?.endDecoration && <>{decoration.endDecoration}}
); } ); -export { Input }; -export type { InputDecorationProps, InputProps }; +export { Input, InputLeftElement, InputRightElement }; +export type { InputLeftElementProps, InputProps, InputRightElementProps }; diff --git a/frontend/app/element/palette.tsx b/frontend/app/element/palette.tsx index fe831f6b3..9c5fe5306 100644 --- a/frontend/app/element/palette.tsx +++ b/frontend/app/element/palette.tsx @@ -129,4 +129,5 @@ Palette.displayName = "Palette"; PaletteButton.displayName = "PaletteButton"; PaletteContent.displayName = "PaletteContent"; -export { Palette, PaletteButton, PaletteContent, type PaletteButtonProps, type PaletteContentProps }; +export { Palette, PaletteButton, PaletteContent }; +export type { PaletteButtonProps, PaletteContentProps };