ui(components): New Dialog and Modal components for ui-next (#4772)
Co-authored-by: sedghi <ar.sedghi@gmail.com>
This commit is contained in:
1 parent
62b4fa3e89
commit
a3b51aa8db
90 files changed
+3459
-1357
No files matched your search
@@ -0,0 +1,258 @@
|
||||
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<InputDialogContextValue | null>(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<HTMLDivElement, InputDialogRootProps>(
|
||||
({ value, defaultValue = '', onChange, className, submitOnEnter, children }, ref) => {
|
||||
const [internalValue, setInternalValue] = useControllableState({
|
||||
prop: value,
|
||||
defaultProp: defaultValue,
|
||||
onChange,
|
||||
});
|
||||
|
||||
return (
|
||||
<InputDialogContext.Provider
|
||||
value={{
|
||||
value: internalValue,
|
||||
setValue: setInternalValue,
|
||||
submitOnEnter,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={ref}
|
||||
className={cn('flex flex-col', className)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</InputDialogContext.Provider>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
InputDialogRoot.displayName = 'InputDialog';
|
||||
|
||||
export interface InputDialogFieldProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
/** Optional className for the field container */
|
||||
className?: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const Field = React.forwardRef<HTMLDivElement, InputDialogFieldProps>(
|
||||
({ className, children, ...props }, ref) => {
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
className={cn('mb-4 flex flex-col space-y-2', className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Field.displayName = 'InputDialog.Field';
|
||||
|
||||
export interface InputDialogInputProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, '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<HTMLInputElement, InputDialogInputProps>(
|
||||
({ 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<HTMLInputElement>(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<HTMLInputElement>) => {
|
||||
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 (
|
||||
<div className={cn('w-full', className)}>
|
||||
<InputComponent
|
||||
ref={inputRef}
|
||||
id={id}
|
||||
value={value}
|
||||
onChange={e => setValue(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
InputDialogInput.displayName = 'InputDialog.Input';
|
||||
|
||||
export interface InputDialogLabelProps extends React.LabelHTMLAttributes<HTMLLabelElement> {
|
||||
/** Optional className for the label */
|
||||
className?: string;
|
||||
/** For attribute to match input ID */
|
||||
htmlFor?: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const InputDialogLabel = React.forwardRef<HTMLLabelElement, InputDialogLabelProps>(
|
||||
({ className, htmlFor = 'dialog-input', children, ...props }, ref) => {
|
||||
return (
|
||||
<Label
|
||||
ref={ref}
|
||||
className={cn(className)}
|
||||
htmlFor={htmlFor}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</Label>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
InputDialogLabel.displayName = 'InputDialog.Label';
|
||||
|
||||
export interface InputDialogActionsProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
/** Optional className for the actions container */
|
||||
className?: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const Actions = React.forwardRef<HTMLDivElement, InputDialogActionsProps>(
|
||||
({ className, children, ...props }, ref) => {
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
{...props}
|
||||
>
|
||||
<FooterAction className={cn(className)}>
|
||||
<FooterAction.Right>{children}</FooterAction.Right>
|
||||
</FooterAction>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
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<HTMLDivElement, InputDialogActionButtonProps>(
|
||||
({ 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 (
|
||||
<div
|
||||
ref={ref}
|
||||
{...props}
|
||||
>
|
||||
<FooterAction.Secondary
|
||||
onClick={() => onClick(value)}
|
||||
className={cn(className)}
|
||||
>
|
||||
{children}
|
||||
</FooterAction.Secondary>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ActionsSecondary.displayName = 'InputDialog.ActionsSecondary';
|
||||
|
||||
const ActionsPrimary = React.forwardRef<HTMLDivElement, InputDialogActionButtonProps>(
|
||||
({ 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 (
|
||||
<div
|
||||
ref={ref}
|
||||
{...props}
|
||||
data-cy="input-dialog-save-button"
|
||||
onClick={() => onClick(value)}
|
||||
>
|
||||
<FooterAction.Primary
|
||||
onClick={() => onClick(value)}
|
||||
className={cn(className)}
|
||||
>
|
||||
{children}
|
||||
</FooterAction.Primary>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ActionsPrimary.displayName = 'InputDialog.ActionsPrimary';
|
||||
|
||||
export const InputDialog = Object.assign(InputDialogRoot, {
|
||||
Label: InputDialogLabel,
|
||||
Input: InputDialogInput,
|
||||
Field,
|
||||
Actions,
|
||||
ActionsSecondary,
|
||||
ActionsPrimary,
|
||||
});
|
||||
@@ -0,0 +1,106 @@
|
||||
import * as React from 'react';
|
||||
import { Input } from '../Input';
|
||||
import { ScrollArea } from '../ScrollArea';
|
||||
import { cn } from '../../lib/utils';
|
||||
|
||||
interface PresetDialogProps {
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function PresetDialog({ children, className }: PresetDialogProps) {
|
||||
return <div className={cn('flex h-[500px] max-w-lg flex-col', className)}>{children}</div>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcomponent: PresetBody
|
||||
* A dark "box" container that wraps the filter and grid area.
|
||||
* Adjust bg color, padding, border, etc. to match your design.
|
||||
*/
|
||||
interface PresetBodyProps {
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
function PresetBody({ children, className }: PresetBodyProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
// Adjust these classes for your desired look
|
||||
'flex min-h-0 flex-1 flex-col rounded-md border border-white/10 bg-black p-2',
|
||||
className
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcomponent: PresetFilter
|
||||
*/
|
||||
interface PresetFilterProps {
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
function PresetFilter({ children, className }: PresetFilterProps) {
|
||||
return (
|
||||
<div className={cn('mb-2 flex w-full flex-shrink-0 items-center space-x-2', className)}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcomponent: PresetSearch
|
||||
*/
|
||||
interface PresetSearchProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||
className?: string;
|
||||
}
|
||||
function PresetSearch({ className, ...props }: PresetSearchProps) {
|
||||
return (
|
||||
<Input
|
||||
className={cn('w-full', className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcomponent: PresetGrid
|
||||
*/
|
||||
interface PresetGridProps {
|
||||
children: React.ReactNode;
|
||||
maxHeight?: string;
|
||||
className?: string;
|
||||
}
|
||||
function PresetGrid({ children, maxHeight = 'flex-1', className }: PresetGridProps) {
|
||||
return (
|
||||
<ScrollArea className={cn('min-h-0 flex-1', className)}>
|
||||
<div className="grid grid-cols-4 gap-2 pr-2">{children}</div>
|
||||
</ScrollArea>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subcomponent: PresetOption
|
||||
*/
|
||||
interface PresetOptionProps {
|
||||
label?: string;
|
||||
className?: string;
|
||||
}
|
||||
function PresetOption({ label = 'Label', className }: PresetOptionProps) {
|
||||
return (
|
||||
<div className={cn('flex flex-col items-start space-y-1', className)}>
|
||||
{/* Default dark placeholder box (swap in an <img> if you like) */}
|
||||
<div className="bg-popover h-16 w-24 rounded" />
|
||||
<div className="text-muted-foreground text-left text-base">{label}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/* Attach subcomponents to PresetDialog as static properties */
|
||||
PresetDialog.PresetBody = PresetBody;
|
||||
PresetDialog.PresetFilter = PresetFilter;
|
||||
PresetDialog.PresetSearch = PresetSearch;
|
||||
PresetDialog.PresetGrid = PresetGrid;
|
||||
PresetDialog.PresetOption = PresetOption;
|
||||
@@ -0,0 +1,2 @@
|
||||
export { InputDialog } from './InputDialog';
|
||||
export { PresetDialog } from './PresetDialog';
|
||||
Reference in new issue
Block a user