Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
starh-comp-common
Advanced tools
[![build and tests](https://github.com/amiroff157/reactjs-use-form/actions/workflows/ubuntu_node.yml/badge.svg)](https://github.com/amiroff157/reactjs-use-form/actions/workflows/ubuntu_node.yml) [![code style: prettier](https://img.shields.io/badge/code_s
Create a form model, flag input fields as required or add a value validation function with custom error messages. useForm will validate the inputs as the user types, when there are no errors the form gets enabled for submission. On form submission, it executes a callback function the user provides.
npm install reactjs-use-form
import { FormModelType } from 'reactjs-use-form';
export const formModel: FormModelType = {
currentPassphrase: {
value: '',
required: true,
},
newPassphrase: {
value: '',
required: true,
validator: (newPassphrase, values) => {
if (newPassphrase === values?.currentPassphrase) {
return 'New password must be different from current password';
} else if (newPassphrase.length <= 5) {
return 'Password must be at least 6 characters long';
} else if (newPassphrase !== values?.verifyPassphrase) {
return 'Passwords do not match';
} else return '';
},
},
verifyPassphrase: {
value: '',
required: true,
validator: (verifyPassphrase, values) => {
return verifyPassphrase !== values?.newPassphrase ? 'Passwords do not match' : '';
},
},
};
prepare a submit callback function, for example: function handleSubmit() {...}
.
use the form model with the callback function in useForm hook in a functional react component:
import React from 'react';
import { useForm, ValuesType } from 'reactjs-use-form';
import { formModel } from './formModel';
const ChangePassphraseComponent = () => {
const {
values,
errors,
handleOnChange,
handleOnSubmit,
isDisabled,
isSubmitted
} = useForm(formModel, handleSubmit);
const { currentPassphrase, newPassphrase, verifyPassphrase }: ValuesType = values;
function handleSubmit() {
// formSubmitCallback();
}
return (
<form onSubmit={handleOnSubmit}>
<div>
<label>Current Passphrase</label>
<input
type="password"
name="currentPassphrase"
value={currentPassphrase}
onChange={handleOnChange}
/>
<span>{errors.currentPassphrase.message}</span>
</div>
<div>
<label>New Passphrase</label>
<input
type="password"
name="newPassphrase"
value={newPassphrase}
onChange={handleOnChange}
/>
<span>{errors.newPassphrase.message}</span>
</div>
<div>
<label>Verify Passphrase</label>
<input
type="password"
name="verifyPassphrase"
value={verifyPassphrase}
onChange={handleOnChange}
/>
<span>{errors.verifyPassphrase.message}</span>
</div>
<span>{isSubmitted ? 'Passphrase has been changed!' : null}</span>
<button type="submit" disabled={isDisabled}>
<span>Submit</span>
</button>
</form>
);
};
import React from 'react';
import { Button, FormControl, FormGroup, FormHelperText, FormLabel, TextField } from '@material-ui/core';
import { useForm, ValuesType } from 'reactjs-use-form';
import { formModel } from './formModel';
const ChangePassphraseComponent = () => {
const {
values,
errors,
handleOnChange,
handleOnSubmit,
isDisabled,
isSubmitted
} = useForm(formModel, handleSubmit);
const { currentPassphrase, newPassphrase, verifyPassphrase }: ValuesType = values;
function handleSubmit() {
// formSubmitCallback();
}
return (
<form onSubmit={handleOnSubmit}>
<FormGroup>
<FormControl>
<TextField
required={true}
label='Current Passphrase'
type='password'
name='currentPassphrase'
error={errors.currentPassphrase.hasError}
value={currentPassphrase}
onChange={handleOnChange} />
<FormHelperText error={errors.currentPassphrase.hasError}>
{errors.currentPassphrase.message}
</FormHelperText>
</FormControl>
</FormGroup>
<FormGroup>
<FormControl>
<TextField
required={true}
label='New Passphrase'
type='password'
name='newPassphrase'
error={errors.newPassphrase.hasError}
value={newPassphrase}
onChange={handleOnChange} />
<FormHelperText error={errors.newPassphrase.hasError}>
{errors.newPassphrase.message}
</FormHelperText>
</FormControl>
</FormGroup>
<FormGroup>
<FormControl>
<TextField
required={true}
label='Verify Passphrase'
type='password'
name='verifyPassphrase'
error={errors.verifyPassphrase.hasError}
value={verifyPassphrase}
onChange={handleOnChange} />
<FormHelperText error={errors.verifyPassphrase.hasError}>
{errors.verifyPassphrase.message}
</FormHelperText>
</FormControl>
</FormGroup>
{isSubmitted ? <Alert variant='standard' severity='success' action='Passphrase has been changed!' /> : null}
<Button type='submit' disabled={isDisabled}>
Submit
</Button>
</form>
);
};
useForm takes two params: formModel
and formSubmitCallback
and returns the rest.
const {
values,
errors,
handleOnChange,
handleOnSubmit,
isDisabled,
isSubmitted
} = useForm(formModel, formSubmitCallback);
Param | Type | Description |
---|---|---|
values | ValuesType | returns form values state object |
errors | ErrorsType | returns form errors state object |
handleOnChange | HandleOnChangeType | binds to a HTMLInputElement: change event |
handleOnSubmit | HandleOnSubmitType | binds to a HTMLFormElement: submit event |
isDisabled | boolean | returns true / false when the form is valid / invalid |
isSubmitted | boolean | returns true when the form was submitted without errors |
formModel | FormModelType | initial form model with optional validation function |
formSubmitCallback | () => void | function to run after form validation and submission |
FAQs
Libreria di componenti React specifica per la piattaforma StarH.
The npm package starh-comp-common receives a total of 9 weekly downloads. As such, starh-comp-common popularity was classified as not popular.
We found that starh-comp-common demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.