Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-form-stateful
Advanced tools
full featured, extensible form component for react using react hooks
react-form-stateful a full featured, extensible form component for react using react hooks
NOTE: this project requires the use of an alpha version of react to use.
npm install --save react-form-stateful
import { FC } from 'react';
import { StatefulForm, SFInput, SFSelect, SFTextArea } from 'react-form-stateful';
import * as yup from 'yup';
const ValidationSchemeForm: FC = () => {
return (
<StatefulForm
validationSchema={yup.object().shape({
email: yup
.string()
.required('Email required')
.email('Invalid email address'),
desc: yup.string().max(256, 'Please keep your description short!'),
complaint: yup
.string()
.required('Complaint required')
.max(10000, 'Max complaint size: 10,000 characters.'),
})}
>
<div>Feedback form:</div>
<label>
Email:
<SFInput name="email" />
</label>
<label>
Short Description:
<SFInput name="desc" />
</label>
<label>
Reason for complaint:
<SFSelect
name="reason"
defaultEntry={'Please select a reason'}
values={['Bug', 'Typo', 'Feature Request', 'Other']}
/>
</label>
<label>
Complaint:
<SFTextArea name="complaint" />
</label>
</StatefulForm>
);
};
Other examples can be seen in the examples folder. ValidationScheme.tsx
is a simple form and the other two are more complex.
Context is used to expose the state. This allows for helper hooks to be written. Several already exist, but more could easily be written. (Feel free to make a PR).
One of the components from components:
type SFControlProps<T = string> = {
name: string;
className?: string;
errorClassName?: string;
initialValue?: T;
defaultValue?: T;
};
export const SFInput: FC<SFControlProps> = props => {
const { touch, value, setValue, error } = useSFControl(props.name, props.initialValue, props.defaultValue);
return (
<Fragment>
<input className={props.className} onBlur={touch} value={value || ''} onChange={e => setValue(e.target.value)} />
<div className={props.errorClassName}>{error}</div>
</Fragment>
);
};
Example Usage
import { FC, createElement } from 'react';
import { StatefulForm, SFInput } from 'react-form-stateful';
const Form: FC = () => {
return (
<StatefulForm>
<SFInput name="item" />
</StatefulForm>
);
};
As you can see component that matches your application's look and feel, but basic components do exist for your convenience.
While the internal reducer is not exposed, the dispatch and actions are exposed, which allows for extension through side effects. An example of this can be seen in examples/pages/Pages.tsx.
There are two special constants that help with extending the functionality of react-form-stateful.
NO_DEFAULT
prevents resets from affecting this value. Useful for hidden from values that are used to control validation.
This us used in the advanced example examples/pages/Pages:
const valueState = useSFValue<number[]>(
'@@pages',
[0],
NO_DEFAULT, // Don't get reset
value => (props.pages.length !== value.length ? 'more pages exist' : null)
);
ASYNC_VALIDATION
Is used to to defer the validation to some external process. This is useful when you want to defer the validation to a separate process. This could also be done with Promises, but there may be cases where ASYNC_VALIDATION
is more convenient.
const { error, setValue, value, touch, touched } = useSFControl<string>(props.name, '', '', () => ASYNC_VALIDATION);
Here when a validation is triggered, the error state is set to { async:true }
. The form is not submitable until this is resolved. One way to resolve this is to use the useSFError
hook and set the error state for the component.
const [, setError] = useSFError(props.name);
setError('Invalid username');
FAQs
full featured, extensible form component for react using react hooks
We found that react-form-stateful 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.