/** * FormControlContext - 对应 MUI FormControlContext.js * 为 FormControl 内的子组件提供上下文 */ // 简化版 Context(Lynx 环境) export interface FormControlState { adornedStart?: boolean setAdornedStart?: (value: boolean) => void color?: 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' disabled?: boolean error?: boolean filled?: boolean focused?: boolean fullWidth?: boolean hiddenLabel?: boolean size?: 'small' | 'medium' onBlur?: () => void onFocus?: () => void onEmpty?: () => void onFilled?: () => void required?: boolean variant?: 'standard' | 'outlined' | 'filled' } // 全局 FormControl 状态存储(简化实现) let currentFormControlState: FormControlState | null = null export function setFormControlContext(state: FormControlState | null) { currentFormControlState = state } export function getFormControlContext(): FormControlState | null { return currentFormControlState } /** * 从 FormControl 上下文获取表单状态 */ export function useFormControl(): FormControlState | undefined { return currentFormControlState || undefined } /** * 根据 props 和 FormControl 状态计算最终状态 */ export function formControlState(options: { props: Record muiFormControl?: FormControlState states: string[] }): Record { const { props, muiFormControl, states } = options return states.reduce((acc, state) => { acc[state] = props[state] if (muiFormControl) { if (typeof props[state] === 'undefined') { acc[state] = (muiFormControl as any)[state] } } return acc }, {} as Record) } export default { useFormControl, formControlState, setFormControlContext, getFormControlContext, }