import type { ISpaceProps } from '@/types'
import { getGapStyle } from './index'
export const api = ['state', 'orderedChildren']
function isVNodeFn(node: any): boolean {
return !!(node && (node.__v_isVNode || node.componentOptions))
}
export const renderless = (props: ISpaceProps, hooks, { slots }) => {
const { reactive, computed } = hooks
const state = reactive({
gapStyle: computed(() => getGapStyle(props))
})
const orderedChildren = computed(() => {
const children = slots.default?.() || []
const validChildren = children.filter((v) => {
if (!isVNodeFn(v)) return false
const type = (v as any).type
return type !== 'Comment' && type !== Symbol.for('v-comment')
})
if (!props.order?.length) return validChildren
const map: Record<string, any> = {}
validChildren.forEach((child) => {
const key = child.key ?? (Array.isArray(child.props?.class) ? child.props.class.join(' ') : child.props?.class)
if (key) map[String(key)] = child
})
const sorted = props.order.map((k) => map[k]).filter(Boolean)
const rest = validChildren.filter((v) => !props.order.includes(String(v.key)))
return [...sorted, ...rest]
})
return { state, orderedChildren }
}