@pyreon/form 0.3.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/context.ts CHANGED
@@ -1,11 +1,5 @@
1
- import {
2
- createContext,
3
- pushContext,
4
- popContext,
5
- onUnmount,
6
- useContext,
7
- } from '@pyreon/core'
8
- import type { VNodeChild, VNode, Props } from '@pyreon/core'
1
+ import type { Props, VNode, VNodeChild } from '@pyreon/core'
2
+ import { createContext, provide, useContext } from '@pyreon/core'
9
3
  import type { FormState } from './types'
10
4
 
11
5
  const FormContext = createContext<FormState<Record<string, unknown>> | null>(
@@ -33,10 +27,7 @@ export interface FormProviderProps<TValues extends Record<string, unknown>>
33
27
  export function FormProvider<TValues extends Record<string, unknown>>(
34
28
  props: FormProviderProps<TValues>,
35
29
  ): VNode {
36
- const frame = new Map([[FormContext.id, props.form]])
37
- pushContext(frame)
38
-
39
- onUnmount(() => popContext())
30
+ provide(FormContext, props.form as FormState<Record<string, unknown>>)
40
31
 
41
32
  const ch = props.children
42
33
  return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode
package/src/index.ts CHANGED
@@ -1,26 +1,22 @@
1
- export { useForm } from './use-form'
2
- export { useFieldArray } from './use-field-array'
3
- export { useField } from './use-field'
4
- export { useWatch } from './use-watch'
5
- export { useFormState } from './use-form-state'
6
1
  export { FormProvider, useFormContext } from './context'
7
-
8
2
  export type {
9
3
  Accessor,
10
- FieldState,
11
4
  FieldRegisterProps,
5
+ FieldState,
12
6
  FormState,
7
+ SchemaValidateFn,
13
8
  UseFormOptions,
14
- ValidationError,
15
9
  ValidateFn,
16
- SchemaValidateFn,
10
+ ValidationError,
17
11
  } from './types'
18
-
12
+ export type { UseFieldResult } from './use-field'
13
+ export { useField } from './use-field'
19
14
  export type {
20
15
  FieldArrayItem,
21
16
  UseFieldArrayResult,
22
17
  } from './use-field-array'
23
-
24
- export type { UseFieldResult } from './use-field'
25
-
18
+ export { useFieldArray } from './use-field-array'
19
+ export { useForm } from './use-form'
26
20
  export type { FormStateSummary } from './use-form-state'
21
+ export { useFormState } from './use-form-state'
22
+ export { useWatch } from './use-watch'
@@ -1,12 +1,12 @@
1
- import { signal, computed } from '@pyreon/reactivity'
1
+ import { computed, signal } from '@pyreon/reactivity'
2
2
  import {
3
- registerForm,
4
- unregisterForm,
3
+ _resetDevtools,
5
4
  getActiveForms,
6
5
  getFormInstance,
7
6
  getFormSnapshot,
8
7
  onFormChange,
9
- _resetDevtools,
8
+ registerForm,
9
+ unregisterForm,
10
10
  } from '../devtools'
11
11
 
12
12
  // Minimal form-like object for testing (avoids needing the full useForm + DOM)
@@ -1,27 +1,32 @@
1
- import { h } from '@pyreon/core'
2
1
  import { mount } from '@pyreon/runtime-dom'
2
+ import type { FormState } from '../index'
3
3
  import {
4
- useForm,
5
- useFieldArray,
6
- useField,
7
- useWatch,
8
- useFormState,
9
4
  FormProvider,
5
+ useField,
6
+ useFieldArray,
7
+ useForm,
10
8
  useFormContext,
9
+ useFormState,
10
+ useWatch,
11
11
  } from '../index'
12
- import type { FormState } from '../index'
13
12
 
14
13
  // ─── Helpers ──────────────────────────────────────────────────────────────────
15
14
 
15
+ function Capture<T>({ fn }: { fn: () => T }) {
16
+ fn()
17
+ return null
18
+ }
19
+
16
20
  function mountWith<T>(fn: () => T): { result: T; unmount: () => void } {
17
21
  let result: T | undefined
18
22
  const el = document.createElement('div')
19
23
  document.body.appendChild(el)
20
24
  const unmount = mount(
21
- h(() => {
22
- result = fn()
23
- return null
24
- }, null),
25
+ <Capture
26
+ fn={() => {
27
+ result = fn()
28
+ }}
29
+ />,
25
30
  el,
26
31
  )
27
32
  return {
@@ -1810,23 +1815,26 @@ describe('FormProvider / useFormContext', () => {
1810
1815
  const el = document.createElement('div')
1811
1816
  document.body.appendChild(el)
1812
1817
 
1813
- const unmount = mount(
1814
- h(() => {
1815
- const form = useForm({
1816
- initialValues: { email: 'context@test.com' },
1817
- onSubmit: () => {
1818
- /* noop */
1819
- },
1820
- })
1821
- return h(FormProvider as any, { form }, () =>
1822
- h(() => {
1823
- contextForm = useFormContext<{ email: string }>()
1824
- return null
1825
- }, null),
1826
- )
1827
- }, null),
1828
- el,
1829
- )
1818
+ function ContextConsumer() {
1819
+ contextForm = useFormContext() as FormState<{ email: string }>
1820
+ return null
1821
+ }
1822
+
1823
+ function ContextTest() {
1824
+ const form = useForm({
1825
+ initialValues: { email: 'context@test.com' },
1826
+ onSubmit: () => {
1827
+ /* noop */
1828
+ },
1829
+ })
1830
+ return (
1831
+ <FormProvider form={form as any}>
1832
+ {() => <ContextConsumer />}
1833
+ </FormProvider>
1834
+ )
1835
+ }
1836
+
1837
+ const unmount = mount(<ContextTest />, el)
1830
1838
 
1831
1839
  expect(contextForm).toBeDefined()
1832
1840
  expect(contextForm!.fields.email.value()).toBe('context@test.com')
@@ -1840,14 +1848,15 @@ describe('FormProvider / useFormContext', () => {
1840
1848
 
1841
1849
  let error: Error | undefined
1842
1850
  const unmount = mount(
1843
- h(() => {
1844
- try {
1845
- useFormContext()
1846
- } catch (e) {
1847
- error = e as Error
1848
- }
1849
- return null
1850
- }, null),
1851
+ <Capture
1852
+ fn={() => {
1853
+ try {
1854
+ useFormContext()
1855
+ } catch (e) {
1856
+ error = e as Error
1857
+ }
1858
+ }}
1859
+ />,
1851
1860
  el,
1852
1861
  )
1853
1862
 
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Signal, Computed } from '@pyreon/reactivity'
1
+ import type { Computed, Signal } from '@pyreon/reactivity'
2
2
 
3
3
  export type ValidationError = string | undefined
4
4
 
@@ -1,5 +1,5 @@
1
- import { signal, computed } from '@pyreon/reactivity'
2
- import type { Signal, Computed } from '@pyreon/reactivity'
1
+ import type { Computed, Signal } from '@pyreon/reactivity'
2
+ import { computed, signal } from '@pyreon/reactivity'
3
3
 
4
4
  export interface FieldArrayItem<T> {
5
5
  /** Stable key for keyed rendering. */
package/src/use-field.ts CHANGED
@@ -1,10 +1,10 @@
1
+ import type { Computed, Signal } from '@pyreon/reactivity'
1
2
  import { computed } from '@pyreon/reactivity'
2
- import type { Signal, Computed } from '@pyreon/reactivity'
3
3
  import type {
4
+ FieldRegisterProps,
4
5
  FieldState,
5
6
  FormState,
6
7
  ValidationError,
7
- FieldRegisterProps,
8
8
  } from './types'
9
9
 
10
10
  export interface UseFieldResult<T> {
@@ -1,5 +1,5 @@
1
- import { computed } from '@pyreon/reactivity'
2
1
  import type { Computed } from '@pyreon/reactivity'
2
+ import { computed } from '@pyreon/reactivity'
3
3
  import type { FormState, ValidationError } from './types'
4
4
 
5
5
  export interface FormStateSummary<TValues extends Record<string, unknown>> {
package/src/use-form.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { onUnmount } from '@pyreon/core'
2
- import { signal, computed, effect } from '@pyreon/reactivity'
3
2
  import type { Signal } from '@pyreon/reactivity'
3
+ import { computed, effect, signal } from '@pyreon/reactivity'
4
4
  import type {
5
5
  FieldRegisterProps,
6
6
  FieldState,
package/src/use-watch.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import type { Computed, Signal } from '@pyreon/reactivity'
1
2
  import { computed } from '@pyreon/reactivity'
2
- import type { Signal, Computed } from '@pyreon/reactivity'
3
3
  import type { FormState } from './types'
4
4
 
5
5
  /**