Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Easy way to work with forms in React. Using React Hooks 😏
npm install formular
import { useForm } from 'formular'
import { Input } from 'formular/lib/tags'
const App = () => {
const form = useForm({
fields: {
email: [ required ],
password: [ required ],
},
})
const handleSubmit = useCallback(() => {
form.submit()
.then((values) => {
// { email: "noreply@gmail.com", password: "strongpass" }
}, (errors) => {
// { email: "Required", password: "Required" }
})
}, [])
return (
<Fragment>
<Input field={form.fields.email} />
<button onClick={handleSubmit}>Submit</button>
</Fragment>
)
}
Or if you need only one field you can just do
import { useField } from 'formular'
import { Input } from 'formular/lib/tags'
const App = () => {
const field = useField({
validate: [ required ],
})
const handleSubmit = useCallback(() => {
form.validate()
.then((error) => {
// { email: "noreply@gmail.com", password: "strongpass" }
})
}, [])
return (
<Fragment>
<Input field={field} />
<button onClick={handleSubmit}>Submit</button>
</Fragment>
)
}
// validator should return "undefined" if value is valid
const required = (value) => !value && value !== 0 ? 'Required' : undefined
type FormOpts = {
name?: string
fields: {
[key: string]: FieldOpts
}
initialValues?: object
}
type FieldOpts = {
name?: string
value?: string // initial value
validate?: Array<Function> // list of validators
readOnly?: boolean
validationDelay?: number // adds debounce to validation
}
type FormEntity = {
name?: string
opts: FormOpts
fields: {
[key: string]: Field
}
state: {
isValid: boolean
isChanged: boolean
isValidating: boolean
isSubmitting: boolean
isSubmitted: boolean
}
setState(values: Partial<State>): void
setValues(values: object): void
getValues(): object
unsetValues(): void
getErrors(): object
async validate(): Promise<boolean>
async submit(): Promise<object>
on(eventName: string, handler: Function): void
off(eventName: string, handler: Function): void
}
const form: FormEntity = useForm(opts)
type FieldEntity = {
form?: Form
name?: string
opts: FieldOpts
node?: HTMLElement
validators: Array<Function>
readOnly: boolean
debounceValidate: Function // method to call validation with debounce
state: {
value: any
error: any
isChanged: boolean
isValidating: boolean
isValidated: boolean
isValid: boolean
}
setState(values: Partial<State>): void
setRef(node: HTMLElement): void
unsetRef(): void
set(value: any): void
unset(): void
validate = (): CancelablePromise
on(eventName: string, handler: Function): void
off(eventName: string, handler: Function): void
}
const field: FieldEntity = useField(opts)
form.on('state change', (state) => {
// triggers on a form's state change
})
form.on('change', (field) => {
// triggers on a field change
})
form.on('blur', (field) => {
// triggers on a field blurring
})
form.on('state change', (state) => {
// triggers on a field's state change
})
field.on('set', (value) => {
// triggers on a value set
})
field.on('change', (value) => {
// simlink to "set"
})
field.on('unset', () => {
// triggers on a value unset
})
field.on('start validate', () => {
// triggers on a validation start
})
field.on('validate', (error) => {
// triggers on a validation finish
})
field.on('blur', () => {
// triggers on a field blurring
})
FAQs
Build forms in React. Easy-Peasy!
The npm package formular receives a total of 0 weekly downloads. As such, formular popularity was classified as not popular.
We found that formular demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.