import React, { createContext, useContext, useRef, useEffect } from 'react'; import { useControllableState } from '@radix-ui/react-use-controllable-state'; import { Label, Input as InputComponent, FooterAction } from '../../components'; import { cn } from '../../lib/utils'; interface InputDialogContextValue { value: string; setValue: (value: string) => void; submitOnEnter?: boolean; } const InputDialogContext = createContext(null); export type InputDialogRootProps = { /** The controlled value of the input */ value?: string; /** The default value for uncontrolled usage */ defaultValue?: string; /** Callback when input value changes */ onChange?: (value: string) => void; /** Optional className for the root container */ className?: string; /** Enable save on Enter key press */ submitOnEnter?: boolean; children: React.ReactNode; }; const InputDialogRoot = React.forwardRef( ({ value, defaultValue = '', onChange, className, submitOnEnter, children }, ref) => { const [internalValue, setInternalValue] = useControllableState({ prop: value, defaultProp: defaultValue, onChange, }); return (
{children}
); } ); InputDialogRoot.displayName = 'InputDialog'; export interface InputDialogFieldProps extends React.HTMLAttributes { /** Optional className for the field container */ className?: string; children: React.ReactNode; } const Field = React.forwardRef( ({ className, children, ...props }, ref) => { return (
{children}
); } ); Field.displayName = 'InputDialog.Field'; export interface InputDialogInputProps extends Omit, 'onChange' | 'value'> { /** ID for the input field */ id?: string; /** Optional className for the input container */ className?: string; /** Save handler */ onSave?: (value: string) => void; /** Placeholder text for the input field */ placeholder?: string; } const InputDialogInput = React.forwardRef( ({ id = 'dialog-input', className, onSave, ...props }, ref) => { const context = useContext(InputDialogContext); if (!context) { throw new Error('InputDialog.Input must be used within an InputDialog'); } const { value, setValue } = context; const inputRef = useRef(null); // Combine the forwarded ref with our local ref React.useImperativeHandle(ref, () => inputRef.current); // Focus the input when it mounts useEffect(() => { if (inputRef.current) { inputRef.current.focus(); } }, []); const handleKeyDown = (e: React.KeyboardEvent) => { if (context.submitOnEnter && e.key === 'Enter') { e.preventDefault(); const saveButton = document.querySelector( '[data-cy="input-dialog-save-button"]' ) as HTMLButtonElement; if (saveButton) { saveButton.click(); } } }; return (
setValue(e.target.value)} onKeyDown={handleKeyDown} {...props} />
); } ); InputDialogInput.displayName = 'InputDialog.Input'; export interface InputDialogLabelProps extends React.LabelHTMLAttributes { /** Optional className for the label */ className?: string; /** For attribute to match input ID */ htmlFor?: string; children: React.ReactNode; } const InputDialogLabel = React.forwardRef( ({ className, htmlFor = 'dialog-input', children, ...props }, ref) => { return ( ); } ); InputDialogLabel.displayName = 'InputDialog.Label'; export interface InputDialogActionsProps extends React.HTMLAttributes { /** Optional className for the actions container */ className?: string; children: React.ReactNode; } const Actions = React.forwardRef( ({ className, children, ...props }, ref) => { return (
{children}
); } ); Actions.displayName = 'InputDialog.Actions'; export interface InputDialogActionButtonProps { /** Optional className for the button */ className?: string; /** Click handler that receives the current input value */ onClick: (value: string) => void; children: React.ReactNode; } const ActionsSecondary = React.forwardRef( ({ className, onClick, children, ...props }, ref) => { const context = useContext(InputDialogContext); if (!context) { throw new Error('InputDialog.ActionsSecondary must be used within an InputDialog'); } const { value } = context; return (
onClick(value)} className={cn(className)} > {children}
); } ); ActionsSecondary.displayName = 'InputDialog.ActionsSecondary'; const ActionsPrimary = React.forwardRef( ({ className, onClick, children, ...props }, ref) => { const context = useContext(InputDialogContext); if (!context) { throw new Error('InputDialog.ActionsPrimary must be used within an InputDialog'); } const { value } = context; return (
onClick(value)} > onClick(value)} className={cn(className)} > {children}
); } ); ActionsPrimary.displayName = 'InputDialog.ActionsPrimary'; export const InputDialog = Object.assign(InputDialogRoot, { Label: InputDialogLabel, Input: InputDialogInput, Field, Actions, ActionsSecondary, ActionsPrimary, });