Skip to content

Commit

Permalink
support functional components in collections and switchables
Browse files Browse the repository at this point in the history
  • Loading branch information
tpresley committed Apr 30, 2024
1 parent 933993c commit 42e6a39
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,30 @@ class Component {

const stateSource = new StateSource(combined$)
const stateField = props.from

if (typeof props.sygnalFactory !== 'function' && typeof props.sygnalOptions === 'object') {
props.sygnalFactory = component(props.sygnalOptions)
}

const collectionOf = props.of
let lense
let factory

const factory = typeof props.of === 'function' ? props.of : this.components[props.of]
if (typeof collectionOf === 'function') {
if (collectionOf.isSygnalComponent) {
factory = collectionOf
} else {
const name = (typeof collectionOf.name === 'string') ? collectionOf.name : 'FUNCTION_COMPONENT'
const view = collectionOf
const { model, intent, context, children, components, initialState, calculated, storeCalculatedInState, DOMSourceName, stateSourceName, debug } = collectionOf
const options = { name, view, model, intent, context, children, components, initialState, calculated, storeCalculatedInState, DOMSourceName, stateSourceName, debug }
factory = component(options)
}
} else if (this.components[collectionOf]) {
factory = this.components[collectionOf]
} else {
throw new Error(`[${this.name}] Invalid 'of' propery in collection: ${ collectionOf }`)
}

const sanitizeItems = item => {
if (typeof item === 'object') {
Expand Down Expand Up @@ -991,6 +1012,17 @@ class Component {
}

const switchableComponents = props.of
const keys = Object.keys(switchableComponents)
keys.forEach(key => {
const current = switchableComponents[key]
if (!current.isSygnalComponent) {
const name = (typeof current.name === 'string') ? current.name : 'FUNCTION_COMPONENT'
const view = current
const { model, intent, context, children, components, initialState, calculated, storeCalculatedInState, DOMSourceName, stateSourceName, debug } = current
const options = { name, view, model, intent, context, children, components, initialState, calculated, storeCalculatedInState, DOMSourceName, stateSourceName, debug }
switchableComponents[key] = component(options)
}
})
const sources = { ...this.sources, [this.stateSourceName]: stateSource, props$, children$, __parentContext$: this.context$ }

const sink$ = isolate(switchable(switchableComponents, props$.map(props => props.current)), { [this.stateSourceName]: lense })(sources)
Expand Down

0 comments on commit 42e6a39

Please sign in to comment.