-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release 0.2: improve performance and bundle size
- Loading branch information
Showing
27 changed files
with
638 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { FormRef } from '../internal/formRef'; | ||
import { getMapValue, KeySelector } from '../internal/path'; | ||
|
||
export const readField = <T, V>( | ||
formRef: FormRef<T>, | ||
key: KeySelector<T, V> | ||
): V | undefined => { | ||
try { | ||
return getMapValue(key, formRef.values).getValue(); | ||
} catch (ex) { | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { FormRef } from '../internal/formRef'; | ||
import { buildObject } from '../internal/path'; | ||
|
||
export const readForm = <T>(formRef: FormRef<T>): T => { | ||
try { | ||
return formRef.values$.getValue(); | ||
} catch (ex) { | ||
return {} as T; | ||
} | ||
}; | ||
export const readForm = <T>(formRef: FormRef<T>): T => | ||
buildObject( | ||
Object.fromEntries( | ||
Array.from(formRef.values.entries()).map(([key, value]) => [ | ||
key, | ||
value.hasValue() ? value.getValue() : undefined, | ||
]) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
import { FormRef, getControlState } from '../internal/formRef'; | ||
import { getKeys, KeysSelector, navigateDeepSubject } from '../internal/path'; | ||
import { getKeys, KeysSelector } from '../internal/path'; | ||
|
||
export const resetForm = <T>( | ||
formRef: FormRef<T>, | ||
keysSelector?: KeysSelector<T> | ||
): void => { | ||
const { values$, initialValues$, registeredKeys } = formRef; | ||
if (!keysSelector) { | ||
values$.next(initialValues$.getValue()); | ||
registeredKeys.getValue().forEach(key => { | ||
getControlState(formRef, key) | ||
.getChild('touched') | ||
.next(false); | ||
}); | ||
return; | ||
} | ||
const { values, initialValues } = formRef; | ||
|
||
const keys = getKeys(keysSelector); | ||
const keys = keysSelector ? getKeys(keysSelector) : Array.from(values.keys()); | ||
keys.forEach(key => { | ||
const value$ = navigateDeepSubject(key, values$); | ||
const initialValue$ = navigateDeepSubject(key, initialValues$); | ||
value$.next(initialValue$.getValue()); | ||
|
||
getControlState(formRef, key) | ||
.getChild('touched') | ||
.next(false); | ||
if (!values.has(key) || !initialValues.has(key)) { | ||
return; | ||
} | ||
values.get(key)!.setValue(initialValues.get(key)!.getValue()); | ||
const control = getControlState(formRef, key); | ||
const controlValue = control.getValue(); | ||
if (controlValue.touched) { | ||
control.setValue({ | ||
...controlValue, | ||
touched: false, | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,19 @@ | ||
import { FormRef } from '../internal/formRef'; | ||
import { KeySelector, navigateDeepSubject } from '../internal/path'; | ||
import { getKeyValues, getMapValue, KeySelector } from '../internal/path'; | ||
|
||
export const setFieldValue = <TValues, T>( | ||
formRef: FormRef<TValues>, | ||
keySelector: KeySelector<TValues, T>, | ||
value: T | ||
) => { | ||
const value$ = navigateDeepSubject(keySelector, formRef.values$); | ||
|
||
if ( | ||
value$ === formRef.values$ && | ||
typeof value === 'object' && | ||
value !== null | ||
) { | ||
const keys = value$.getKeys(); | ||
const autofill = {} as any; | ||
keys.forEach(key => { | ||
if (value$.getChild(key).hasValue()) | ||
autofill[key] = value$.getChild(key).getValue(); | ||
}); | ||
value$.next({ | ||
...autofill, | ||
...value, | ||
}); | ||
return; | ||
} | ||
getMapValue(keySelector, formRef.values).setValue(value); | ||
}; | ||
|
||
value$.next(value); | ||
export const setFormValue = <TValues, T>( | ||
formRef: FormRef<TValues>, | ||
value: T | ||
) => { | ||
Object.entries(getKeyValues(value)).forEach(([key, value]) => { | ||
getMapValue(key, formRef.values).setValue(value); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import { FormRef } from '../internal/formRef'; | ||
import { KeySelector, navigateDeepSubject } from '../internal/path'; | ||
import { getKeyValues, getMapValue, KeySelector } from '../internal/path'; | ||
|
||
export const setInitialValue = <TValues, T>( | ||
formRef: FormRef<TValues>, | ||
keySelector: KeySelector<TValues, T>, | ||
value: T | ||
) => navigateDeepSubject(keySelector, formRef.initialValues$).next(value); | ||
) => getMapValue(keySelector, formRef.initialValues).setValue(value); | ||
|
||
export const setFormInitialValue = <TValues, T>( | ||
formRef: FormRef<TValues>, | ||
value: T | ||
) => { | ||
Object.entries(getKeyValues(value)).forEach(([key, value]) => { | ||
getMapValue(key, formRef.initialValues).setValue(value); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
import { useEffect } from 'react'; | ||
import { FormRef, getControlState, ControlOptions } from '../internal/formRef'; | ||
import { getKey, navigateDeepSubject } from '../internal/path'; | ||
import { getKey, getMapValue } from '../internal/path'; | ||
|
||
export const useControlSubscription = <TValues, T>( | ||
formRef: FormRef<TValues>, | ||
options: ControlOptions<TValues, T> | ||
) => { | ||
const key = getKey(options.key); | ||
useEffect(() => { | ||
formRef.registerControl(options); | ||
}, [formRef, options]); | ||
formRef.registerControl(options); | ||
|
||
return { | ||
setValue: (value: T) => | ||
navigateDeepSubject(key, formRef.values$).next(value), | ||
subscribe: (cb: (value: T) => void) => { | ||
const sub = navigateDeepSubject(key, formRef.values$).subscribe(cb); | ||
return () => { | ||
sub.unsubscribe(); | ||
}; | ||
getValue: () => getMapValue(key, formRef.values).getValue(), | ||
setValue: (value: T) => getMapValue(key, formRef.values).setValue(value), | ||
subscribe: (cb: (value: T) => void) => | ||
getMapValue(key, formRef.values).subscribe(cb), | ||
touch: () => { | ||
const state$ = getControlState(formRef, key); | ||
state$.value.then( | ||
value => { | ||
if (value.touched) return; | ||
state$.setValue({ | ||
...value, | ||
touched: true, | ||
}); | ||
}, | ||
() => {} | ||
); | ||
}, | ||
touch: () => | ||
getControlState(formRef, key) | ||
.getChild('touched') | ||
.next(true), | ||
}; | ||
}; |
Oops, something went wrong.