All files / frontend/src/components/SukohForm FieldRegistry.ts

47.76% Statements 32/67
0% Branches 0/2
2.77% Functions 1/36
84.21% Lines 32/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                      2x 2x         2x 2x 2x 2x 2x     2x 2x     2x     2x     2x 2x 2x 2x 2x 2x     2x 2x 2x     2x 2x 2x 2x 2x     2x 2x 2x     2x     2x       2x                                                                                                               2x                    
import type { ComponentType } from 'react';
 
export interface FieldComponentProps {
  compositeKey: string;
}
 
export type FieldImporter = () => Promise<{
  default: ComponentType<FieldComponentProps>;
}>;
 
// Module-level state
const components: Map<string, FieldImporter> = new Map();
let legacyMode = true;
 
// Initialize default field type registrations
function registerDefaults(): void {
  // Text fields
  components.set('string', () => import('./fields/StringField'));
  components.set('markdown', () => import('./fields/MarkdownField'));
  components.set('easymde', () => import('./fields/EasyMdeField'));
  components.set('readonly', () => import('./fields/ReadonlyField'));
  components.set('uniq', () => import('./fields/UniqField'));
 
  // Number fields
  components.set('number', () => import('./fields/NumberField'));
  components.set('slider', () => import('./fields/SliderField'));
 
  // Boolean fields
  components.set('boolean', () => import('./fields/ToggleField'));
 
  // Date fields
  components.set('date', () => import('./fields/DateField'));
 
  // Selection fields
  components.set('select', () => import('./fields/SelectField'));
  components.set('select-from-query', () => import('./fields/SelectFromQueryField'));
  components.set('chips', () => import('./fields/ChipsField'));
  components.set('color', () => import('./fields/ColorField'));
  components.set('fonticon-picker', () => import('./fields/FontIconPickerField'));
  components.set('font-picker', () => import('./fields/FontPickerField'));
 
  // Image/File fields
  components.set('image-select', () => import('./fields/ImageSelectField'));
  components.set('bundle-manager', () => import('./fields/BundleManagerField'));
  components.set('bundle-image-thumbnail', () => import('./fields/BundleImgThumbField'));
 
  // Container fields
  components.set('accordion', () => import('./fields/AccordionField'));
  components.set('section', () => import('./fields/SectionField'));
  components.set('nest', () => import('./fields/NestField'));
  components.set('pull', () => import('./fields/PullField'));
  components.set('leaf-array', () => import('./fields/LeafArrayField'));
 
  // Utility fields
  components.set('hidden', () => import('./fields/HiddenField'));
  components.set('empty-line', () => import('./fields/EmptyLineField'));
  components.set('info', () => import('./fields/InfoField'));
 
  // Special fields
  components.set('eisenhouwer', () => import('./fields/EisenhouwerField'));
 
  // Not found fallback
  components.set('not-found', () => import('./fields/NotFoundField'));
}
 
// Call initialization at module load
registerDefaults();
 
/**
 * Register a custom field type with its lazy-loaded component
 */
export function register(type: string, importer: FieldImporter): void {
  components.set(type, importer);
}
 
/**
 * Get the lazy importer for a field type
 * Returns 'not-found' field if type is not registered
 */
export function getFieldComponent(type: string): FieldImporter {
  return components.get(type) || components.get('not-found')!;
}
 
/**
 * Check if a field type is registered
 */
export function hasFieldComponent(type: string): boolean {
  return components.has(type);
}
 
/**
 * Get all registered field types (excluding 'not-found')
 */
export function getRegisteredTypes(): string[] {
  return Array.from(components.keys()).filter((t) => t !== 'not-found');
}
 
/**
 * Enable or disable legacy mode
 */
export function setLegacyMode(enabled: boolean): void {
  legacyMode = enabled;
}
 
/**
 * Check if legacy mode is enabled
 */
export function isLegacyMode(): boolean {
  return legacyMode;
}
 
// Interface for type inference
export interface FieldRegistry {
  register: typeof register;
  getFieldComponent: typeof getFieldComponent;
  hasFieldComponent: typeof hasFieldComponent;
  getRegisteredTypes: typeof getRegisteredTypes;
  setLegacyMode: typeof setLegacyMode;
  isLegacyMode: typeof isLegacyMode;
}
 
// Backward compatibility: namespace object
export const fieldRegistry: FieldRegistry = {
  register,
  getFieldComponent,
  hasFieldComponent,
  getRegisteredTypes,
  setLegacyMode,
  isLegacyMode,
};
 
export default fieldRegistry;