Gathering detailed insights and metrics for react-signal-forms
Gathering detailed insights and metrics for react-signal-forms
Gathering detailed insights and metrics for react-signal-forms
Gathering detailed insights and metrics for react-signal-forms
solid-signal-form
Solid-js forms library with api similar to react-hook-form
@firanorg/mollitia-molestias-accusamus
[![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url]
@hishprorg/impedit-reprehenderit-quo
<div align="center"> <a href="https://@hishprorg/impedit-reprehenderit-quo.com" title="React Hook Form - Simple React forms validation"> <img src="https://raw.githubusercontent.com/@hishprorg/impedit-reprehenderit-quo/@hishprorg/impedi
@merger203/expert-invention
<h3 align="center"> @merger203/expert-invention </h3>
🆕 Create high performance forms with type-safe configurations, rules and plugins.
npm install react-signal-forms
Typescript
Module System
Node Version
NPM Version
68.3
Supply Chain
96.1
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
1,256
Last Day
7
Last Week
8
Last Month
13
Last Year
204
3 Stars
58 Commits
1 Watching
2 Branches
2 Contributors
Latest Version
0.6.0
Package Id
react-signal-forms@0.6.0
Unpacked Size
127.85 kB
Size
32.02 kB
File Count
127
NPM Version
10.2.3
Node Version
20.10.0
Publised On
03 Dec 2023
Cumulative downloads
Total Downloads
Last day
0%
7
Compared to previous day
Last week
700%
8
Compared to previous week
Last month
0%
13
Compared to previous month
Last year
-80.6%
204
Compared to previous year
1
1
⚠️ This library is still new, so everything is still subject to change. You can follow its development in the project and releases. The docs will be updated as development progresses.
A forms library which aims to provide a high performance modular experience by leveraging signals with @preact/signals-react.
required()
, requiredIf(...)
, applicableIf(...)
, computed(...)
, etc.npm i react-signal-forms
For a quick first look you can check out the demo of react-signal-forms with Material UI, or run it yourself by cloning the repository and running:
npm run ci
npm run demo
If you want to explore the demo code, a good place to start would be the basics example.
Start by initializing your form component and field hook, including the plugins you want to use:
1// Add plugins, built-in or your own.
2export const { SignalForm, useForm, useField } = createSignalForm(
3 ...defaultPlugins, // the defaults, includes validation rules and touched field signals.
4 plugins.applicabilityRules // adds applicability rules and field signals.
5)
ℹ️ A full list of the currently available plugins can be found in the plugins module.
Create field specifications for your forms:
1interface IYourData { 2 justText: string 3 aFieldWithRules: string 4 aSelectField: string 5} 6 7const fields = signalForm<IYourData>().withFields((field) => { 8 // ^ All specifications and rules will be strongly 9 // typed based on your data interface. 10 11 ...field("justText", "Just a text field"), 12 13 ...field("aFieldWithRules", "A field with some rules", { 14 defaultValue: "Demo", 15 16 // Add rules to your field. Some examples: 17 rules: [ 18 required(), 19 minLength(6), 20 requiredIf(({ form }) => form.fields.otherField.value === true), 21 applicableIf(({ form })) => form.field.otherField.value === true) 22 ] 23 }) 24 25 ...field("aSelectField", "Select field").as<SelectField>({ 26 // Plug in any field type you need, ^ 27 // built-in or your own. 28 options: [ 29 /* ...items */ 30 ] 31 }) 32 33})
Add the useField
hook to your inputs:
1interface TextInputProps { 2 field: TextField // only accepts string fields. 3} 4 5const TextInput = ({ field }: TextInputProps) => { 6 const { 7 value, 8 setValue, 9 isValid, 10 errors, 11 isApplicable, 12 ...otherSignals 13 // ^ With intellisense matching your selected plugins. 14 } = useField(field) 15 16 if (!isApplicable) { 17 return null 18 } 19 20 return ( 21 <input 22 value={value} 23 onChange={(e) => setValue(e.currentTarget.value)} 24 {...otherProps} 25 /> 26 ) 27}
You are now set to compose your form:
1const MyForm = () => { 2 return ( 3 <SignalForm 4 fields={fields} 5 initialValues={valuesFromStore} 6 onSubmit={handleSubmit} 7 > 8 <TextInput field={fields.justText} /> 9 <TextInput field={fields.aFieldWithRules} /> 10 <SelectInput field={fields.aSelectField} /> 11 </SignalForm> 12 ) 13}
All internal state management is done with signals. An advantage of this approach is that rules automatically subscribe to the state they need, and are only re-evaluated when state used in the rules are updated. The results of these rules are in turn also saved in (computed) signals.
A simple example to illustrate what this means for performance: if field A is only applicable if field B has a specific value, then:
true
to false
or vice versa.All form features other than the core - e.g. validation and applicability rules - are implemented as plugins. The goal behind this concept is to make the form implementation both scalable and extensible. In most simpler cases, the native plugins should be enough to get you going. If necessary though, plugins and rules can be added or replaced to fulfill on specialized requirements.
ℹ️ All native plugins use the methods described below, so you can use those as examples.
Custom rules can be added to existing plugins. If it fits your needs, than this is the easier option. In general, rules can be created with the createFieldRule()
helper function. This function can be used as is, or it can be wrapped for specific plugins. For example, the validation plugin has wrapped this function in createValidationRule()
.
Plugins can be replaced and you can create and plug in your own to better fit your needs. To do this you can use the createPlugin()
and createFieldRule()
methods. To get started you can have a look at the readonlyRules
plugin, which is one of the simpler plugins.
The implementation of forms with one or more arrays of items is supported by array fields. You can create the specifications for an array field with ...field("yourArrayField").asArray(...)
.
For example:
1type ExampleData = { 2 arrayField: Array<{ 3 booleanField: boolean 4 textField: string 5 }> 6} 7 8const fields = signalForm<ExampleData>().withFields((field) => ({ 9 ...field("arrayField").asArray({ 10 fields: (field) => ({ 11 ...field("booleanField", "Toggle field"), 12 ...field("textField", "Text field"), 13 }), 14 }), 15}))
The array field itself and all fields in an array field support the same features and plugins as other fields. Note that field rules in an array form also have access to the parent form.
For example:
1...field("textFieldInArray", "Text field in array", { 2 rules: [ 3 applicableIf( 4 ({ form }) => form.parent.fields.fieldInParent.value === "some value" 5 ) 6 ] 7})
Adding array fields to your form can then be done with the useArrayField()
hook and the <ArrayItem />
component. The hook provides a description of the items in the array, which can then be mapped to the ArrayItem
component.
For example:
1const YourForm = () => ( 2 <SignalForm fields={yourFields}> 3 {/* ... */} 4 <YourArrayField /> 5 {/* ... */} 6 </SignalForm> 7) 8 9const YourArrayField = () => { 10 const { items, itemFields, add } = useArrayField(yourFields.arrayField) 11 // ^ can also be accessed with `yourFields.arrayField.fields`. 12 13 return ( 14 <> 15 {items.map((item) => ( 16 <YourLayout key={item.id}> 17 {/* ^ make sure to set `key` to `item.id` */} 18 19 <ArrayItem item={item}> 20 <TextInput field={itemFields.textField}> 21 22 {/* Other layout and input components */} 23 24 <Button onClick={item.remove}>Remove item</Button> 25 </ArrayItem> 26 </YourLayout> 27 ))} 28 29 <Button onClick={add}>Add item</Button> 30 </> 31 ) 32}
The demo includes an example for array fields, and you can find the code in ArrayFieldsDemo.
ℹ️ For better performance when adding and removing items, wrap your array items in
React.memo()
. In the example above this could be done on the<YourLayout />
component, and you can also find it used in the demo.
Planned.
No vulnerabilities found.
No security vulnerabilities found.