Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.