MCPcopy Index your code
hub / github.com/formkit/formkit / FormKitConfigLoader

Function FormKitConfigLoader

packages/react/src/FormKitProvider.ts:133–182  ·  view source on GitHub ↗
(props: ConfigLoaderProps)

Source from the content-addressed store, hash-verified

131}
132
133function FormKitConfigLoader(props: ConfigLoaderProps) {
134 const {
135 defaultConfig: useDefaultConfig,
136 configFile,
137 children,
138 ...attrs
139 } = props
140 const [config, setConfig] = useState<FormKitOptions | null>(null)
141
142 useEffect(() => {
143 let mounted = true
144
145 ;(async () => {
146 let loadedConfig: Record<string, any> | ((...args: any[]) => FormKitOptions) =
147 {}
148
149 if (configFile) {
150 const configModule = await import(
151 /* @vite-ignore */
152 /* webpackIgnore: true */
153 configFile
154 )
155 loadedConfig =
156 'default' in configModule ? configModule.default : configModule
157 }
158
159 if (typeof loadedConfig === 'function') {
160 loadedConfig = loadedConfig()
161 }
162
163 const shouldUseDefaultConfig = useDefaultConfig ?? true
164 if (shouldUseDefaultConfig) {
165 const { defaultConfig } = await import('./defaultConfig')
166 loadedConfig = defaultConfig(loadedConfig as any)
167 }
168
169 if (mounted) {
170 setConfig(loadedConfig as FormKitOptions)
171 }
172 })()
173
174 return () => {
175 mounted = false
176 }
177 }, [configFile, useDefaultConfig])
178
179 if (!config) return null
180
181 return createElement(FormKitProvider, { ...attrs, config }, children)
182}
183
184/**
185 * Lazy provider: if config is already present, renders children directly.

Callers

nothing calls this directly

Calls 2

createElementFunction · 0.90
defaultConfigFunction · 0.70

Tested by

no test coverage detected