Skip to content

withControl typing bug if controlFactory props optional #18

@kyeotic

Description

@kyeotic

I have a control defined using withControl and the expected props for the control and the component are reversed.

Screenshots

  • Bad control typing
    Image

  • Bad component typing
    Image

Code

import { For } from 'solid-js'
import {
  createFormGroup,
  createFormControl,
  withControl,
  createFormArray,
  bindOwner,
} from 'solid-forms'
import { ItemWithNote } from '../../../server/users/types'
import { H2, Button } from '../../components'
import { TextInput } from '../../components/Forms'

const newItem = (item?: ItemWithNote) =>
  createFormGroup({
    item: createFormControl(item?.item ?? ''),
    note: createFormControl(item?.note ?? ''),
  })

const controlFactory = (props: { initialValue: ItemWithNote[] }) => {
  return createFormArray<ReturnType<typeof newItem>[]>(
    props.initialValue.map(newItem),
  )
}

export const ItemWithNoteSet = withControl<
  { title: string },
  typeof controlFactory
>({
  controlFactory,
  component: (props) => {
    const array = () => props.control

    const onAdd = bindOwner(() => array().push(newItem()))

    const onDelete = (index: number) => {
      array().removeControl(index)
    }

    return (
      <div>
        <H2 class="mb-2">{props.title}</H2>
        <Button onclick={onAdd}>Add</Button>
        <For each={array().controls}>
          {(control, index) => (
            <div class="p-4 border rounded mt-2 space-y-2">
              <div class="flex justify-between items-start">
                <TextInput label="Item" control={control.controls.item} />
                <Button danger onclick={() => onDelete(index())}>
                  Delete
                </Button>
              </div>
              <TextInput label="Note" control={control.controls.note} />
            </div>
          )}
        </For>
      </div>
    )
  },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions