-
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.
Update Formulier, useFormField implementations
- Loading branch information
Showing
18 changed files
with
246 additions
and
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@formulier/docs": minor | ||
"@formulier/examples-react": minor | ||
"@formulier/core": minor | ||
"@formulier/react": minor | ||
--- | ||
|
||
Update Formulier, useFormField implementations |
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
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
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,126 +1,145 @@ | ||
import {FieldValidator, FormListener, FormulierState, Nullable, Primitives, Values} from './types' | ||
import {FieldValidator, FormulierState, Nullable, Primitives, Values} from './types' | ||
import {getPath, isEqual, removeKey, setKey, setPath} from './state-utils' | ||
|
||
export interface FormulierOptions<V extends Values, P extends Primitives> { | ||
initialValues: Nullable<V, P> | ||
} | ||
|
||
export class Formulier<V extends Values = Values, P extends Primitives = Primitives> { | ||
notifyEnabled: boolean | ||
listeners: Set<FormListener<V, P>> | ||
state: FormulierState<V, P> | ||
store: Store<FormulierState<V, P>> | ||
instances: Record<string, Set<string>> = {} | ||
|
||
constructor({initialValues}: FormulierOptions<V, P>) { | ||
this.notifyEnabled = true | ||
this.listeners = new Set() | ||
this.state = { | ||
this.store = new Store({ | ||
values: initialValues, | ||
validators: {}, | ||
errors: {}, | ||
touched: {}, | ||
submitCount: 0, | ||
} as FormulierState<V, P> | ||
}) | ||
} | ||
|
||
this.getState = this.getState.bind(this) | ||
this.subscribe = this.subscribe.bind(this) | ||
setFieldErrors = (fieldErrors: FormulierState<V, P>['errors']): void => { | ||
this.store.batch(() => { | ||
this.store.setState(state => ({...state, errors: {}})) | ||
for (const [name, error] of Object.entries(fieldErrors)) { | ||
this.store.setState(state => ({...state, errors: setKey(state.errors, name, error)})) | ||
} | ||
}) | ||
} | ||
|
||
getState(): FormulierState<V, P> { | ||
return this.state | ||
validateFields = (): boolean => { | ||
const {validators, values} = this.store.getState() | ||
const fieldErrors = Object.fromEntries( | ||
Object.entries(validators).map(([name, validate]) => { | ||
const value = getPath(values, name, null) | ||
const error = validate?.(value) || null | ||
return [name, error] | ||
}), | ||
) | ||
this.setFieldErrors(fieldErrors) | ||
const noErrors = Object.values(this.store.getState().errors).every(value => value == null) | ||
return noErrors | ||
} | ||
|
||
subscribe(listener: FormListener<V, P>): () => void { | ||
this.listeners.add(listener) | ||
validateField = (name: string): boolean => { | ||
const {validators, values} = this.store.getState() | ||
const validate = validators[name] | ||
const value = getPath(values, name, null) | ||
const error = validate?.(value) || null | ||
this.store.setState(state => ({...state, errors: setKey(state.errors, name, error)})) | ||
return !!validate && !error | ||
} | ||
|
||
registerField = (name: string, validate: FieldValidator | undefined): (() => void) => { | ||
this.store.setState(state => ({ | ||
...state, | ||
values: setPath(state.values, name, getPath(state.values, name, null)), | ||
validators: setKey(state.validators, name, validate || null), | ||
errors: state.errors[name] === undefined ? setKey(state.errors, name, null) : state.errors, | ||
touched: state.touched[name] === undefined ? setKey(state.touched, name, false) : state.touched, | ||
})) | ||
return () => { | ||
this.listeners.delete(listener) | ||
this.store.setState(state => ({ | ||
...state, | ||
values: setPath(state.values, name, undefined), | ||
validators: removeKey(state.validators, name), | ||
errors: removeKey(state.errors, name), | ||
touched: removeKey(state.touched, name), | ||
})) | ||
} | ||
} | ||
|
||
notify(): void { | ||
if (this.notifyEnabled === false) return | ||
const state = this.getState() | ||
this.listeners.forEach(listener => listener(state)) | ||
addInstance = (name: string, instanceId: string): (() => void) => { | ||
this.instances[name] ||= new Set() | ||
this.instances[name].add(instanceId) | ||
return () => { | ||
this.instances[name]?.delete(instanceId) | ||
} | ||
} | ||
|
||
validateFields(): boolean { | ||
this.state = {...this.state, errors: {}} | ||
Object.entries(this.state.validators).forEach(([name, validate]) => { | ||
const value = getPath(this.state.values, name, null) | ||
const error = validate?.(value) || null | ||
this.state = {...this.state, errors: setKey(this.state.errors, name, error)} | ||
}) | ||
this.notify() | ||
const noErrors = !Object.values(this.state.errors).some(value => value !== null) | ||
return noErrors | ||
hasInstance = (name: string): boolean => { | ||
return !!this.instances[name]?.size | ||
} | ||
|
||
validateField(name: string): boolean { | ||
const validate = this.state.validators[name] | ||
const value = getPath(this.state.values, name, null) | ||
const error = validate?.(value) || null | ||
this.state = {...this.state, errors: setKey(this.state.errors, name, error)} | ||
this.notify() | ||
return !!validate && !error | ||
setFieldValue = (name: string, value: unknown): void => { | ||
const {values} = this.store.getState() | ||
if (isEqual(getPath(values, name), value)) return | ||
this.store.setState(state => ({...state, values: setPath(state.values, name, value)})) | ||
} | ||
|
||
registerField(name: string, validate: FieldValidator | undefined): void { | ||
const value = getPath(this.state.values, name, null) | ||
const errors = this.state.errors[name] | ||
const touched = this.state.touched[name] | ||
this.state = { | ||
...this.state, | ||
values: setPath(this.state.values, name, value), | ||
validators: setKey(this.state.validators, name, validate || null), | ||
} | ||
if (errors === undefined) { | ||
this.state = {...this.state, errors: setKey(this.state.errors, name, null)} | ||
} | ||
if (touched === undefined) { | ||
this.state = {...this.state, touched: setKey(this.state.touched, name, false)} | ||
} | ||
this.notify() | ||
touchField = (name: string, value = true): void => { | ||
const {touched} = this.store.getState() | ||
if (touched[name] === value) return | ||
this.store.setState(state => ({...state, touched: setKey(state.touched, name, value)})) | ||
} | ||
|
||
unregisterField(name: string): void { | ||
this.state = { | ||
...this.state, | ||
values: setPath(this.state.values, name, undefined), | ||
validators: removeKey(this.state.validators, name), | ||
errors: removeKey(this.state.errors, name), | ||
touched: removeKey(this.state.touched, name), | ||
} | ||
this.notify() | ||
incrementSubmitCount = (): void => { | ||
this.store.setState(state => ({...state, submitCount: state.submitCount + 1})) | ||
} | ||
} | ||
|
||
setFieldValue(name: string, value: unknown): void { | ||
if (isEqual(getPath(this.state.values, name), value)) return | ||
this.state = { | ||
...this.state, | ||
values: setPath(this.state.values, name, value), | ||
} | ||
this.notify() | ||
class Store<S> { | ||
private batching = false | ||
private flushing = 0 | ||
private listeners = new Set<(state: S) => void>() | ||
private state: S | ||
|
||
constructor(state: S) { | ||
this.state = state | ||
} | ||
|
||
touchField(name: string, touched = true): void { | ||
if (this.state.touched[name] === touched) return | ||
this.state = { | ||
...this.state, | ||
touched: setKey(this.state.touched, name, touched), | ||
} | ||
this.notify() | ||
getState = (): S => { | ||
return this.state | ||
} | ||
|
||
incrementSubmitCount(): void { | ||
this.state = { | ||
...this.state, | ||
submitCount: this.state.submitCount + 1, | ||
setState = (updater: (previous: S) => S): void => { | ||
this.state = updater(this.state) | ||
this.flush() | ||
} | ||
|
||
subscribe = (listener: (state: S) => void): (() => void) => { | ||
this.listeners.add(listener) | ||
return () => { | ||
this.listeners.delete(listener) | ||
} | ||
this.notify() | ||
} | ||
|
||
withoutNotify(callback: CallableFunction): void { | ||
this.notifyEnabled = false | ||
batch = (callback: () => void): void => { | ||
if (this.batching === true) return void callback() | ||
this.batching = true | ||
callback() | ||
this.notifyEnabled = true | ||
this.batching = false | ||
this.flush() | ||
} | ||
|
||
private flush = (): void => { | ||
if (this.batching === true) return | ||
const state = this.getState() | ||
const flushId = ++this.flushing | ||
for (const listener of this.listeners) { | ||
if (this.flushing !== flushId) continue | ||
listener(state) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.