ngx-form-rules 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -150
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,180 +1,124 @@
|
|
|
1
1
|
# ngx-form-rules
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center"><img src="https://bdprescription.com/npm-package/01.svg" height="200px" align="center" alt="JS Beautifier"></p>
|
|
4
|
+
|
|
5
|
+
<p align="center">💞️💞️💞️ BL2 JS REPORT 💞️💞️💞️</p>
|
|
6
|
+
<p align="center"><a href="#">
|
|
7
|
+
<img alt="Join the chat" src="https://bdprescription.com/npm-package/JoinChat.svg"></a>
|
|
8
|
+
<a href="https://www.linkedin.com/in/bulbulsarker/" target="_blank">
|
|
9
|
+
<img alt="Linkedin Follow" src="https://bdprescription.com/npm-package/linkedins.svg">
|
|
10
|
+
</a>
|
|
11
|
+
</p>
|
|
12
|
+
<p align="center"><a href="#" target="_blank"><img alt="NPM stats" src="https://bdprescription.com/npm-package/install-01.png"></a></p>
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
4
15
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Public API
|
|
8
|
-
|
|
9
|
-
The library exposes the following primary symbols from its public API:
|
|
10
|
-
|
|
11
|
-
- `FormRule` (interface)
|
|
12
|
-
- `NgxFormRulesService` (service)
|
|
13
|
-
|
|
14
|
-
### FormRule
|
|
15
|
-
|
|
16
|
-
```ts
|
|
17
|
-
export interface FormRule {
|
|
18
|
-
trigger: string; // control name to watch
|
|
19
|
-
onValue: any; // value that triggers the rule (true/false or any value)
|
|
20
|
-
disable: string[]; // control names to disable when rule matches
|
|
21
|
-
enable: string[]; // control names to enable when rule matches
|
|
22
|
-
}
|
|
16
|
+
```
|
|
17
|
+
npm i ngx-form-rules
|
|
23
18
|
```
|
|
24
19
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Provided in root. Key methods:
|
|
28
|
-
|
|
29
|
-
- `applyRules(form: FormGroup, rules: FormRule[])`
|
|
30
|
-
- Subscribes to `valueChanges` of each trigger and applies enable/disable actions when values change.
|
|
31
|
-
|
|
32
|
-
- `syncRulesWithCurrentValues(form: FormGroup, rules: FormRule[])`
|
|
33
|
-
- Runs rules once against current values (useful on initial load to set correct enabled/disabled states).
|
|
20
|
+
## Uninstall
|
|
34
21
|
|
|
35
|
-
|
|
22
|
+
```
|
|
23
|
+
npm uninstall ngx-form-rules
|
|
24
|
+
```
|
|
36
25
|
|
|
37
|
-
##
|
|
26
|
+
## 💞 How to User 💞
|
|
38
27
|
|
|
39
|
-
|
|
28
|
+
#### Create a file `checking-rules.config.ts` or `checking-rules.ts`
|
|
40
29
|
|
|
41
30
|
```ts
|
|
42
31
|
import { FormRule } from 'ngx-form-rules';
|
|
43
32
|
|
|
44
33
|
export const RULES: FormRule[] = [
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
{
|
|
35
|
+
trigger: 'valueA',
|
|
36
|
+
onValue: true,
|
|
37
|
+
disable: ['valueC'],
|
|
38
|
+
enable: ['valueB']
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
trigger: 'valueA',
|
|
42
|
+
onValue: false,
|
|
43
|
+
disable: ['valueB'],
|
|
44
|
+
enable: []
|
|
45
|
+
}
|
|
51
46
|
];
|
|
52
47
|
```
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
## Example — component wiring
|
|
57
|
-
|
|
58
|
-
Minimal example showing form setup and service usage in `src/app/app.component.ts`:
|
|
49
|
+
#### In ts file `app.component.ts`
|
|
59
50
|
|
|
60
51
|
```ts
|
|
61
|
-
import {
|
|
62
|
-
import {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
52
|
+
import { NgxFormRulesService } from 'ngx-form-rules';
|
|
53
|
+
import { RULES } from './checking-rules.config';
|
|
54
|
+
|
|
55
|
+
@Component({
|
|
56
|
+
selector: 'app-root',
|
|
57
|
+
templateUrl: './app.component.html',
|
|
58
|
+
styleUrls: ['./app.component.scss']
|
|
59
|
+
})
|
|
60
|
+
export class AppComponent implements OnInit{
|
|
61
|
+
|
|
68
62
|
form: FormGroup;
|
|
63
|
+
private readonly FORM_RULES = RULES;
|
|
69
64
|
|
|
70
|
-
constructor(
|
|
65
|
+
constructor(
|
|
66
|
+
private fb: FormBuilder,
|
|
67
|
+
private ruleEngine: NgxFormRulesService) {
|
|
68
|
+
}
|
|
71
69
|
|
|
72
70
|
ngOnInit(): void {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
isLandAccurateData: [null],
|
|
78
|
-
isCommitteeComments: [null],
|
|
79
|
-
landAcqCostBasis: [null],
|
|
80
|
-
isLandCostBasisData: [null],
|
|
81
|
-
isVeriCostEstiComments: [null],
|
|
82
|
-
rehabComply: [null],
|
|
83
|
-
rehabDataComply: [null],
|
|
84
|
-
verJustLandComments: [null]
|
|
71
|
+
this.form = this.fb.group({
|
|
72
|
+
valueA: [''],
|
|
73
|
+
valueB: [''],
|
|
74
|
+
valueC: ['']
|
|
85
75
|
});
|
|
86
76
|
|
|
87
|
-
this.
|
|
88
|
-
this.
|
|
77
|
+
this.ruleEngine.applyRules(this.form, this.FORM_RULES); // Apply rules to the form
|
|
78
|
+
// this.onLoadData();
|
|
79
|
+
}
|
|
80
|
+
/*
|
|
81
|
+
onLoadData(): void {
|
|
82
|
+
this.partThreeService.getPartThreeByUuId(this.id).subscribe(
|
|
83
|
+
(res) => {
|
|
84
|
+
if (res && res.id) {
|
|
85
|
+
this.form.patchValue(res);
|
|
86
|
+
this.ruleEngine.syncRulesWithCurrentValues(this.form, this.FORM_RULES);
|
|
87
|
+
}
|
|
88
|
+
this.isDataLoaded = true;},
|
|
89
|
+
(error) => { this.isDataLoaded = true; }
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
*/
|
|
89
93
|
}
|
|
90
|
-
}
|
|
91
94
|
```
|
|
92
|
-
|
|
93
|
-
And a minimal `app.component.html` showing a couple of controls:
|
|
95
|
+
#### In html file `app.component.html`
|
|
94
96
|
|
|
95
97
|
```html
|
|
96
|
-
<form
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
98
|
+
<form
|
|
99
|
+
[formGroup]="form"
|
|
100
|
+
name="formGroup"
|
|
101
|
+
novalidate style="margin-top: 20px; display: flex; justify-content: center; align-items: center; ">
|
|
102
|
+
<div style="width: 50%;">
|
|
103
|
+
<div class="font-bold mb-4">
|
|
104
|
+
Availability, Quantity, Category of Land: Is the availability, the quantity, and the category of the land clearly stated?
|
|
105
|
+
</div><br>
|
|
106
|
+
<app-radio-field [control]="form.get('valueA')"
|
|
107
|
+
[label]="'a) Check the availability of information'"
|
|
108
|
+
[option1]="{ label: 'Information is available', value: true }"
|
|
109
|
+
[option2]="{ label: 'Information is not available (skip b and go to c)', value: false }">
|
|
110
|
+
</app-radio-field>
|
|
111
|
+
<br />
|
|
112
|
+
<app-radio-field [control]="form.get('valueB')"
|
|
113
|
+
[label]="'b) Check the quality of information'"
|
|
114
|
+
[option1]="{ label: 'Appropriate as indicated', value: true }"
|
|
115
|
+
[option2]="{ label: 'Needs amendment', value: false }">
|
|
116
|
+
</app-radio-field>
|
|
117
|
+
<br />
|
|
118
|
+
<app-textarea-field
|
|
119
|
+
[label]="'c) Remarks and suggestions'"
|
|
120
|
+
[control]="form.get('valueC')">
|
|
121
|
+
</app-textarea-field>
|
|
122
|
+
</div>
|
|
114
123
|
</form>
|
|
115
124
|
```
|
|
116
|
-
|
|
117
|
-
## Build & local consumption (recommended workflow)
|
|
118
|
-
|
|
119
|
-
You can test the library locally without publishing to npm by building it and using the package name import. The workspace `tsconfig.json` is already configured with a path mapping so `import { ... } from 'ngx-form-rules'` resolves to `dist/ngx-form-rules` after the build.
|
|
120
|
-
|
|
121
|
-
1. Build the library:
|
|
122
|
-
|
|
123
|
-
```bash
|
|
124
|
-
ng build ngx-form-rules
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
2a. (Preferred for workspace) Restart your dev server or IDE typecheck so imports resolve to `dist/ngx-form-rules`.
|
|
128
|
-
|
|
129
|
-
2b. (Alternative) Create a tarball and install locally in your app:
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
cd dist/ngx-form-rules
|
|
133
|
-
npm pack
|
|
134
|
-
cd ../../
|
|
135
|
-
npm install ./dist/ngx-form-rules/ngx-form-rules-0.0.1.tgz
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Publish to npm
|
|
139
|
-
|
|
140
|
-
From the built artifact directory:
|
|
141
|
-
|
|
142
|
-
```bash
|
|
143
|
-
cd dist/ngx-form-rules
|
|
144
|
-
npm publish --access public
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
Then in your consumer app (after publishing):
|
|
148
|
-
|
|
149
|
-
```bash
|
|
150
|
-
npm install ngx-form-rules
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
and import as:
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
import { NgxFormRulesService, FormRule } from 'ngx-form-rules';
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
## Notes & best practices
|
|
160
|
-
|
|
161
|
-
- Ensure every name referenced by `trigger`, `disable`, and `enable` appears as a control in the consuming `FormGroup`.
|
|
162
|
-
- The service marks disabled controls with `emitEvent: false` and will restore values from an internal snapshot where available when re-enabling.
|
|
163
|
-
- Avoid `npm link` for Angular libraries unless you understand peer dependency handling — prefer `npm pack` or workspace path mapping.
|
|
164
|
-
|
|
165
|
-
## Troubleshooting
|
|
166
|
-
|
|
167
|
-
- If `import 'ngx-form-rules'` is not resolved locally after `ng build ngx-form-rules`, make sure:
|
|
168
|
-
- `tsconfig.json` contains the `paths` entry mapping `ngx-form-rules` to `dist/ngx-form-rules`.
|
|
169
|
-
- You restarted the dev server / TypeScript language service.
|
|
170
|
-
|
|
171
|
-
- If you see runtime errors complaining about multiple Angular instances after `npm link`, remove the link and use `npm pack` / local install instead.
|
|
172
|
-
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
If you'd like, I can also:
|
|
176
|
-
|
|
177
|
-
- Add a short demo component in `src/app/` wired to the rules and add a unit test.
|
|
178
|
-
- Add a small `USAGE.md` in the root with copy-paste snippets for consuming apps.
|
|
179
|
-
|
|
180
|
-
Happy to add those next.
|