Files
metabuilder/fakemui/react/components/inputs/FormControl.tsx
johndoe6345789 54a819ed71 chore(fakemui): reorganize folder structure by implementation type
ORGANIZED INTO 4 MAIN CATEGORIES:
- react/              React TypeScript components (145 components + Python bindings)
- qml/               QML desktop components (104+ QML components)
- python/            Python package implementations
- legacy/            Utilities, contexts, and migration-in-progress code

SUPPORTING FOLDERS (kept as-is):
- icons/             421 SVG icons
- theming/           Material Design 3 theme system
- styles/            SCSS modules and utilities
- scss/              SCSS preprocessor files
- docs/              Documentation files

STRUCTURE IMPROVEMENTS:
 All code preserved (nothing deleted)
 Clear separation by implementation type
 Better navigation and discoverability
 Easy to find what you need
 Professional organization

DOCUMENTATION:
- Added STRUCTURE.md explaining the new layout
- Updated folder organization with clear purpose
- Maintained all original functionality

All files reorganized while keeping full functionality intact.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-01-23 17:09:48 +00:00

115 lines
2.9 KiB
TypeScript

'use client'
import React, { forwardRef, createContext, useContext, useMemo, useId } from 'react'
/**
* FormControl context for sharing state with child components
*/
interface FormControlContextValue {
id?: string
required?: boolean
disabled?: boolean
error?: boolean
filled?: boolean
focused?: boolean
}
const FormControlContext = createContext<FormControlContextValue>({})
/**
* Hook to access FormControl context from child components
*/
export const useFormControl = () => useContext(FormControlContext)
/**
* Props for FormControl component
*/
export interface FormControlProps extends React.HTMLAttributes<HTMLDivElement> {
children?: React.ReactNode
/** Whether the field is required */
required?: boolean
/** Whether the field is disabled */
disabled?: boolean
/** Whether the field has an error */
error?: boolean
/** Full width form control */
fullWidth?: boolean
/** Margin size */
margin?: 'none' | 'dense' | 'normal'
/** Size of the form control */
size?: 'small' | 'medium'
/** Visual variant */
variant?: 'standard' | 'outlined' | 'filled'
/** Whether the input has value (filled state) */
filled?: boolean
/** Whether the input is focused */
focused?: boolean
/** MUI sx prop for styling compatibility */
sx?: Record<string, unknown>
}
/**
* FormControl - Provides context to form input components for consistent state
*
* @example
* ```tsx
* <FormControl required error={hasError}>
* <FormLabel>Email</FormLabel>
* <Input placeholder="Enter email" />
* <FormHelperText>Required field</FormHelperText>
* </FormControl>
* ```
*/
export const FormControl = forwardRef<HTMLDivElement, FormControlProps>(
(
{
children,
required = false,
disabled = false,
error = false,
fullWidth = false,
margin = 'none',
size = 'medium',
variant = 'outlined',
filled = false,
focused = false,
className = '',
sx,
...props
},
ref
) => {
const id = useId()
const contextValue = useMemo(
() => ({ id, required, disabled, error, filled, focused }),
[id, required, disabled, error, filled, focused]
)
return (
<FormControlContext.Provider value={contextValue}>
<div
ref={ref}
className={`
form-control
form-control--${variant}
form-control--${size}
form-control--margin-${margin}
${fullWidth ? 'form-control--full-width' : ''}
${required ? 'form-control--required' : ''}
${disabled ? 'form-control--disabled' : ''}
${error ? 'form-control--error' : ''}
${focused ? 'form-control--focused' : ''}
${className}
`.trim().replace(/\s+/g, ' ')}
{...props}
>
{children}
</div>
</FormControlContext.Provider>
)
}
)
FormControl.displayName = 'FormControl'