-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improved inline component experience (#893)
* feat: improved inline component experience * updated name
- Loading branch information
1 parent
79d3b31
commit 5b6976b
Showing
3 changed files
with
103 additions
and
75 deletions.
There are no files selected for viewing
93 changes: 19 additions & 74 deletions
93
packages/kit-headless/src/components/accordion/accordion-inline.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { JSXChildren, JSXNode } from '@builder.io/qwik'; | ||
|
||
/** | ||
* | ||
* This function allows us to process the children of an inline component. We can look into the children and get the proper index, pass data, or make certain API decisions. | ||
* | ||
* See accordion-inline.tsx for a usage example. | ||
* | ||
* @param children | ||
* | ||
*/ | ||
export function processChildren(children: JSXChildren) { | ||
const childrenToProcess = ( | ||
Array.isArray(children) ? [...children] : children ? [children] : [] | ||
) as Array<JSXNode>; | ||
|
||
while (childrenToProcess.length) { | ||
const child = childrenToProcess.shift(); | ||
|
||
if (!child) continue; | ||
|
||
if (Array.isArray(child)) { | ||
childrenToProcess.unshift(...child); | ||
continue; | ||
} | ||
|
||
const processor = componentRegistry.get(child.type); | ||
|
||
if (processor) { | ||
processor(child.props); | ||
} else { | ||
const anyChildren = Array.isArray(child.children) | ||
? [...child.children] | ||
: [child.children]; | ||
childrenToProcess.unshift(...(anyChildren as JSXNode[])); | ||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const componentRegistry = new Map<any, ComponentProcessor>(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function findComponent(component: any, processor: ComponentProcessor) { | ||
componentRegistry.set(component, processor); | ||
} | ||
|
||
type ComponentProcessor = (props: Record<string, unknown>) => void; |