Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
formact
Advanced tools
Readme
A zero dependency and design agnostic Form library for React.
In your terminal, add it to the project by running:
npm install --save formact
As formact is design agnostic, you'll need to create your own fields. You can it by using the hook useField
in a function component.
import { useField, FieldProps } from 'formact'
export default function TextField(props: FieldProps & { label: string }) {
const field = useField<string>(props)
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-start',
margin: '5px 10px',
}}>
<div>
<label htmlFor={props.name}>{props.label}</label>
</div>
<input
// @ts-ignore
data-testid={`field-${props.name}`}
name={props.name}
id={props.name}
type={props.type}
onBlur={(e) => {
// call field.onBlur to make sure the field is dirty
field.onBlur(e)
}}
// use field value
value={field.fieldValue || ''}
// update the field value
onChange={(e) => field.update(e.target.value)}
onKeyDown={(e) => {
// Submit the form when pressing enter
if (e.key === 'Enter') {
field.submit()
}
}}
/>
{field.showError ? (
<div data-testid={`field-error-${props.name}`} style={{ color: 'red' }}>
{field.errorMessage}
</div>
) : null}
</div>
)
}
export default function SubmitButton() {
const form = useForm()
return (
<button
data-testid="submit-button"
type="submit"
disabled={!form.valid}
onClick={form.submit}>
{form.submitting ? 'Submitting...' : 'Submit'}
</button>
)
}
These fields become really powerful when wrapped in a Form component.
import Form from "formact"
<Form onChange=(console.log) onSubmit={console.log}>
<TextField name="country" />
<SubmitButton />
</Form>
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
FAQs
A zero dependency and design agnostic Form library for React.
The npm package formact receives a total of 22 weekly downloads. As such, formact popularity was classified as not popular.
We found that formact 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
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.