Skip to content

Commit

Permalink
feat(useFormKitSchema): allow also boolean value for render
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxcode committed Nov 1, 2024
1 parent 55fde26 commit eb11fde
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/composables/useFormKitSchema.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
export function useFormKitSchema() {
const addComponent = (component: string = 'Button', props: object = {}, render: string = 'true', formKitAttrs: object = {}): object => {
const addComponent = (component: string = 'Button', props: object = {}, render: string | boolean = true, formKitAttrs: object = {}): object => {
return {
$cmp: component,
if: render,
if: render.toString(),
props,
...formKitAttrs,
}
}

const addElement = (element: string = 'div', children: any[] | string = [], attrs: object = {}, render: string = 'true', formKitAttrs: object = {}) => {
const addElement = (element: string = 'div', children: any[] | string = [], attrs: object = {}, render: string | boolean = true, formKitAttrs: object = {}) => {
return {
$el: element,
if: render,
if: render.toString(),
attrs,
children,
...formKitAttrs,
}
}

const addGroup = (name: string, children: object[] = [], render: string = 'true', formKitAttrs: object = {}) => {
const addGroup = (name: string, children: object[] = [], render: string | boolean = true, formKitAttrs: object = {}) => {
return {
$formkit: 'group',
if: render,
if: render.toString(),
name,
children,
...formKitAttrs,
}
}

const addList = (name: string, children: object[], dynamic: boolean = true, render: string = 'true', formKitAttrs: object = {}) => {
const addList = (name: string, children: object[] = [], dynamic: boolean = true, render: string | boolean = true, formKitAttrs: object = {}) => {
return {
$formkit: 'list',
if: render,
if: render.toString(),
name,
dynamic,
children,
...formKitAttrs,
}
}

const addListGroup = (children: object[] = [], render: string = 'true', formKitAttrs: object = {}) => {
const addListGroup = (children: object[] = [], render: string | boolean = true, formKitAttrs: object = {}) => {
return {
$formkit: 'group',
if: render,
if: render.toString(),
for: ['item', 'index', '$items'], // 👈 $items is in the slot’s scope
key: '$item',
index: '$index',
Expand All @@ -51,7 +51,7 @@ export function useFormKitSchema() {
}
}

const addElementsInOuterDiv = (children: object[] = [], innerClass: string = '', outerClass: string = '', label: string = '', help: string = '', render: string = 'true') => {
const addElementsInOuterDiv = (children: object[] = [], innerClass: string = '', outerClass: string = '', label: string = '', help: string = '', render: string | boolean = true) => {
const inner = addElement('div', children, { class: `formkit-inner ${innerClass}`, style: 'position: relative;' })
const labelDiv = addElement('label', [label], { class: 'formkit-label' })
const wrapperDiv = addElement('div', [labelDiv, inner], { class: 'formkit-wrapper' })
Expand Down
2 changes: 2 additions & 0 deletions test/useFormKitSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ it('add element', () => {
expect(e2.$el).toBe('span')
expect(renderToBoolean(e2)).toBe(false)
expect(e2.children?.length).toBe(1)
// @ts-expect-error: name is not in the type`
expect(e2.attrs?.name).toBe('test')
})

it('add component', () => {
const { addComponent } = useFormKitSchema()

const component = addComponent()
// @ts-expect-error: $cmp is not in the type
expect(component.$cmp).toBe('Button')
expect(renderToBoolean(component)).toBe(true)
})
Expand Down

0 comments on commit eb11fde

Please sign in to comment.