@smartsoft001/claude-plugins 2.193.0 → 2.195.0
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/.claude-plugin/marketplace.json +14 -3
- package/README.md +19 -4
- package/package.json +3 -3
- package/plugins/smart-angular/.claude-plugin/plugin.json +6 -0
- package/plugins/{smart → smart-angular}/skills/angular-components-button/SKILL.md +1 -1
- package/plugins/{smart → smart-angular}/skills/angular-components-container/SKILL.md +8 -8
- package/plugins/{smart → smart-angular}/skills/angular-components-drawer/SKILL.md +5 -9
- package/plugins/{smart → smart-angular}/skills/angular-components-list-container/SKILL.md +2 -5
- package/plugins/{smart → smart-angular}/skills/angular-components-media-object/SKILL.md +1 -1
- package/plugins/{smart → smart-angular}/skills/angular-components-modal/SKILL.md +1 -1
- package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/SKILL.md +1 -1
- package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/references/base-claude-config.md +13 -9
- package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/references/patch-delta.md +1 -1
- package/plugins/{smart → smart-core}/.claude-plugin/README.md +31 -16
- package/plugins/{smart → smart-core}/.claude-plugin/merge-permissions.js +7 -6
- package/plugins/smart-core/.claude-plugin/plugin.json +5 -0
- package/plugins/{smart → smart-core}/.claude-plugin/settings.template.json +2 -2
- package/plugins/{smart → smart-core}/hooks/README.md +1 -1
- package/plugins/{smart → smart-core}/skills/audit-log/SKILL.md +2 -2
- package/plugins/{smart → smart-core}/skills/format-code/SKILL.md +1 -1
- package/plugins/smart-react/.claude-plugin/plugin.json +6 -0
- package/plugins/smart-react/agents/react-components.md +260 -0
- package/plugins/smart-react/skills/react-components-accordion/SKILL.md +204 -0
- package/plugins/smart-react/skills/react-components-action-panel/SKILL.md +191 -0
- package/plugins/smart-react/skills/react-components-alert/SKILL.md +244 -0
- package/plugins/smart-react/skills/react-components-app/SKILL.md +236 -0
- package/plugins/smart-react/skills/react-components-avatar/SKILL.md +176 -0
- package/plugins/smart-react/skills/react-components-badge/SKILL.md +165 -0
- package/plugins/smart-react/skills/react-components-breadcrumbs/SKILL.md +183 -0
- package/plugins/smart-react/skills/react-components-button/SKILL.md +239 -0
- package/plugins/smart-react/skills/react-components-button-group/SKILL.md +187 -0
- package/plugins/smart-react/skills/react-components-calendar/SKILL.md +270 -0
- package/plugins/smart-react/skills/react-components-card/SKILL.md +201 -0
- package/plugins/smart-react/skills/react-components-card-heading/SKILL.md +133 -0
- package/plugins/smart-react/skills/react-components-command-palette/SKILL.md +228 -0
- package/plugins/smart-react/skills/react-components-container/SKILL.md +134 -0
- package/plugins/smart-react/skills/react-components-date-edit/SKILL.md +186 -0
- package/plugins/smart-react/skills/react-components-date-range/SKILL.md +259 -0
- package/plugins/smart-react/skills/react-components-description-list/SKILL.md +144 -0
- package/plugins/smart-react/skills/react-components-detail/SKILL.md +272 -0
- package/plugins/smart-react/skills/react-components-details/SKILL.md +250 -0
- package/plugins/smart-react/skills/react-components-divider/SKILL.md +128 -0
- package/plugins/smart-react/skills/react-components-drawer/SKILL.md +165 -0
- package/plugins/smart-react/skills/react-components-dropdown/SKILL.md +194 -0
- package/plugins/smart-react/skills/react-components-empty-state/SKILL.md +202 -0
- package/plugins/smart-react/skills/react-components-export/SKILL.md +120 -0
- package/plugins/smart-react/skills/react-components-feed/SKILL.md +171 -0
- package/plugins/smart-react/skills/react-components-form/SKILL.md +319 -0
- package/plugins/smart-react/skills/react-components-grid-list/SKILL.md +172 -0
- package/plugins/smart-react/skills/react-components-icon/SKILL.md +117 -0
- package/plugins/smart-react/skills/react-components-import/SKILL.md +113 -0
- package/plugins/smart-react/skills/react-components-info/SKILL.md +147 -0
- package/plugins/smart-react/skills/react-components-input/SKILL.md +406 -0
- package/plugins/smart-react/skills/react-components-list/SKILL.md +384 -0
- package/plugins/smart-react/skills/react-components-list-container/SKILL.md +105 -0
- package/plugins/smart-react/skills/react-components-loader/SKILL.md +126 -0
- package/plugins/smart-react/skills/react-components-media-object/SKILL.md +130 -0
- package/plugins/smart-react/skills/react-components-modal/SKILL.md +266 -0
- package/plugins/smart-react/skills/react-components-multi-column-layout/SKILL.md +155 -0
- package/plugins/smart-react/skills/react-components-navbar/SKILL.md +211 -0
- package/plugins/smart-react/skills/react-components-notification/SKILL.md +223 -0
- package/plugins/smart-react/skills/react-components-overlays/SKILL.md +182 -0
- package/plugins/smart-react/skills/react-components-page/SKILL.md +224 -0
- package/plugins/smart-react/skills/react-components-page-heading/SKILL.md +155 -0
- package/plugins/smart-react/skills/react-components-paging/SKILL.md +183 -0
- package/plugins/smart-react/skills/react-components-password-strength/SKILL.md +149 -0
- package/plugins/smart-react/skills/react-components-progress-bars/SKILL.md +195 -0
- package/plugins/smart-react/skills/react-components-searchbar/SKILL.md +150 -0
- package/plugins/smart-react/skills/react-components-section-heading/SKILL.md +138 -0
- package/plugins/smart-react/skills/react-components-select-menu/SKILL.md +168 -0
- package/plugins/smart-react/skills/react-components-sidebar-layout/SKILL.md +155 -0
- package/plugins/smart-react/skills/react-components-sidebar-navigation/SKILL.md +277 -0
- package/plugins/smart-react/skills/react-components-sign-in-form/SKILL.md +228 -0
- package/plugins/smart-react/skills/react-components-stacked-layout/SKILL.md +146 -0
- package/plugins/smart-react/skills/react-components-stacked-list/SKILL.md +156 -0
- package/plugins/smart-react/skills/react-components-stats/SKILL.md +149 -0
- package/plugins/smart-react/skills/react-components-table/SKILL.md +222 -0
- package/plugins/smart-react/skills/react-components-tabs/SKILL.md +189 -0
- package/plugins/smart-react/skills/react-components-textarea/SKILL.md +202 -0
- package/plugins/smart-react/skills/react-components-toggle/SKILL.md +157 -0
- package/plugins/smart-react/skills/react-components-vertical-navigation/SKILL.md +212 -0
- package/plugins/smart-react/skills/react-forms/SKILL.md +272 -0
- package/plugins/smart-react/skills/react-provider/SKILL.md +368 -0
- package/plugins/smart-react/skills/smart-crud-react/SKILL.md +410 -0
- package/plugins/smart/.claude-plugin/plugin.json +0 -5
- /package/plugins/{smart/agents/angular-components/AGENT.md → smart-angular/agents/angular-components.md} +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-accordion/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-action-panel/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-alert/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-avatar/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-badge/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-breadcrumbs/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-button-group/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-calendar/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-card/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-card-heading/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-command-palette/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-date-edit/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-date-range/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-description-list/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-detail/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-details/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-divider/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-dropdown/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-empty-state/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-feed/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-form/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-grid-list/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-icon/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-info/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-input/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-list/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-multi-column-layout/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-navbar/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-notification/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-page/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-page-heading/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-paging/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-password-strength/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-progress-bars/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-searchbar/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-section-heading/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-select-menu/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-sidebar-layout/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-sidebar-navigation/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-sign-in-form/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-stacked-layout/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-stacked-list/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-stats/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-table/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-tabs/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-textarea/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-toggle/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/angular-components-vertical-navigation/SKILL.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/references/base-app.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/references/base-shared-lib.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/references/version-resolution.md +0 -0
- /package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/scripts/derive-prefix.sh +0 -0
- /package/plugins/{smart → smart-angular}/skills/scaffold-nx-workspace/scripts/verify-scaffold.sh +0 -0
- /package/plugins/{smart → smart-angular}/skills/smart-crud/SKILL.md +0 -0
- /package/plugins/{smart → smart-core}/hooks/CONFIG.md +0 -0
- /package/plugins/{smart → smart-core}/hooks/audit_logger.py +0 -0
- /package/plugins/{smart → smart-core}/hooks/auto_format.sh +0 -0
- /package/plugins/{smart → smart-core}/hooks/hooks.json +0 -0
- /package/plugins/{smart → smart-core}/hooks/safety_validator.py +0 -0
- /package/plugins/{smart → smart-core}/hooks/sensitive_file_blocker.py +0 -0
- /package/plugins/{smart → smart-core}/hooks/skill_validator.py +0 -0
- /package/plugins/{smart → smart-core}/skills/project-conventions/SKILL.md +0 -0
- /package/plugins/{smart → smart-core}/skills/safety-check/SKILL.md +0 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-forms
|
|
3
|
+
description: The form engine of @smartsoft001/react without a form library — SmartFormControl / SmartFormGroup / SmartFormArray, SmartValidators and custom validators (error keys, customMessage), useControlState / useControlBinding for binding inputs, FormFactory and useModelForm that build a form from @Model/@Field metadata (modes, required, confirm, unique, enabled specifications, permissions, min/max/length possibilities) and IModelValidatorsProvider.
|
|
4
|
+
user-invocable: false
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Forms (`@smartsoft001/react`)
|
|
8
|
+
|
|
9
|
+
The forms of `@smartsoft001/react` do not depend on a form library. A form is a tree of controls (`SmartFormGroup` of `SmartFormControl`s and `SmartFormArray`s) that hold the value, run validators (sync and async), track `touched` / `dirty` and propagate their status to their parents. React reads them through hooks built on `useSyncExternalStore`. `FormFactory` builds such a tree from a model's `@Field` metadata, and `SmartForm` / `SmartInput` render it (see `react-components-form` and `react-components-input`).
|
|
10
|
+
|
|
11
|
+
## When to Use This Skill
|
|
12
|
+
|
|
13
|
+
- Building a form from a model (`useModelForm`, `FormFactory`) and reading its value and validity
|
|
14
|
+
- Describing fields with `@Field` so the factory creates the right controls and validators (modes, `required`, `confirm`, `unique`, `enabled`, `permissions`, `possibilities`)
|
|
15
|
+
- Writing a form or a field by hand with `SmartFormControl` and binding inputs (`useControlBinding`)
|
|
16
|
+
- Adding validators: `SmartValidators`, a custom function, an async check, or an `IModelValidatorsProvider`
|
|
17
|
+
- Understanding which error keys produce which input messages
|
|
18
|
+
|
|
19
|
+
## Controls
|
|
20
|
+
|
|
21
|
+
| Class | Constructor | Value |
|
|
22
|
+
| --------------------- | ------------------------------------------------------------------- | ---------------------------------------------- |
|
|
23
|
+
| `SmartFormControl<T>` | `new SmartFormControl(value = null, validators?, asyncValidators?)` | Its own value. `reset()` falls back to `null`. |
|
|
24
|
+
| `SmartFormGroup<T>` | `new SmartFormGroup(controls = {}, validators?, asyncValidators?)` | An object of its enabled children's values. |
|
|
25
|
+
| `SmartFormArray<T>` | `new SmartFormArray(controls = [], validators?, asyncValidators?)` | An array of its enabled children's values. |
|
|
26
|
+
|
|
27
|
+
Members shared by every control (`SmartAbstractControl`):
|
|
28
|
+
|
|
29
|
+
- State: `value`, `status` (`'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'`), `errors`, `valid`, `invalid`, `pending`, `disabled`, `enabled`, `touched` / `untouched`, `dirty` / `pristine`, `parent`, `root`.
|
|
30
|
+
- Changes: `setValue(value, { onlySelf?, emitEvent? })`, `patchValue`, `reset`, `getRawValue()` (includes disabled children), `updateValueAndValidity()`.
|
|
31
|
+
- Flags: `markAsTouched()`, `markAllAsTouched()`, `markAsUntouched()`, `markAsDirty()`, `markAsPristine()`, `disable()`, `enable()`.
|
|
32
|
+
- Validators: `setValidators`, `addValidators`, `removeValidators`, `hasValidator`, `clearValidators`, the same for async ones; `setErrors`, `getError(code, path?)`, `hasError(code, path?)`.
|
|
33
|
+
- Children: `get(path)` (`'address.city'` or `['items', 0]`); groups also `controls`, `addControl`, `removeControl`, `setControl`, `contains`; arrays `at`, `push`, `insert`, `removeAt`, `move`, `clear`, `length`.
|
|
34
|
+
- Events: `valueChanges`, `statusChanges` and `changes` are `SmartEmitter`s: `subscribe(listener)` returns `{ unsubscribe() }`.
|
|
35
|
+
|
|
36
|
+
Rules: a change recomputes the control's value and status, then its parent's, up to the root, so a group is `INVALID` while any child is and `PENDING` while any child waits for an async validator. Async validators run only once the sync ones pass; a newer run discards an older result, and a rejected validator marks the control invalid (`{ asyncValidator: true }`). A disabled control has no errors, does not count towards its parent's status and is left out of its parent's `value`.
|
|
37
|
+
|
|
38
|
+
## Validators
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
type SmartValidatorFn = (
|
|
42
|
+
control: SmartAbstractControl,
|
|
43
|
+
) => SmartValidationErrors | null | undefined | void;
|
|
44
|
+
type SmartAsyncValidatorFn = (
|
|
45
|
+
control: SmartAbstractControl,
|
|
46
|
+
) => Promise<SmartValidationErrors | null | undefined | void>;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`SmartValidators` has `required`, `requiredTrue`, `email`, `min(n)`, `max(n)`, `minLength(n)`, `maxLength(n)`, `pattern(re)` and `nullValidator`. They report the error keys the input messages read:
|
|
50
|
+
|
|
51
|
+
| Error key | Reported by | Message (translation key) |
|
|
52
|
+
| ------------------------------------ | ---------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
|
|
53
|
+
| `required` | `SmartValidators.required` / `requiredTrue`, `required: true` fields | `INPUT.ERRORS.required` |
|
|
54
|
+
| `confirm` | the `<key>Confirm` control of a `confirm` field | `INPUT.ERRORS.confirm` |
|
|
55
|
+
| `email` | `SmartValidators.email`, `email` fields | `INPUT.ERRORS.invalidEmailFormat` |
|
|
56
|
+
| `min` / `max` | `SmartValidators.min` / `max`, `possibilities.min` / `max` | `INPUT.ERRORS.invalidMin` / `invalidMax` + the limit |
|
|
57
|
+
| `minlength` / `maxlength` | `SmartValidators.minLength` / `maxLength`, `possibilities.minLength` / `maxLength` | `INPUT.ERRORS.invalidMinLength` / `invalidMaxLength` + the length |
|
|
58
|
+
| `phoneNumber`, `pesel`, `invalidNip` | the field types `phoneNumber`, `pesel`, `nip` | `INPUT.ERRORS.invalid…Format` / `invalidNip` |
|
|
59
|
+
| `invalidUnique` | a `unique` field whose `uniqueProvider` resolved `false` | `INPUT.ERRORS.invalidUnique` |
|
|
60
|
+
| `customMessage` | your validator: `{ customMessage: 'text' }` | the text itself |
|
|
61
|
+
|
|
62
|
+
`pattern` reports `{ pattern: … }`, which has no message of its own: return `customMessage` from your own validator to show one.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import {
|
|
66
|
+
SmartFormControl,
|
|
67
|
+
SmartFormGroup,
|
|
68
|
+
SmartValidatorFn,
|
|
69
|
+
SmartValidators,
|
|
70
|
+
} from '@smartsoft001/react';
|
|
71
|
+
|
|
72
|
+
const noSpaces: SmartValidatorFn = (control) =>
|
|
73
|
+
/\s/.test(String(control.value ?? ''))
|
|
74
|
+
? { customMessage: 'No spaces, please.' }
|
|
75
|
+
: null;
|
|
76
|
+
|
|
77
|
+
export const signup = new SmartFormGroup({
|
|
78
|
+
login: new SmartFormControl(
|
|
79
|
+
'',
|
|
80
|
+
[SmartValidators.required, SmartValidators.minLength(3), noSpaces],
|
|
81
|
+
async (control) => {
|
|
82
|
+
const taken = await fetch(
|
|
83
|
+
`/api/logins/${encodeURIComponent(String(control.value))}`,
|
|
84
|
+
).then((r) => r.ok);
|
|
85
|
+
return taken ? { customMessage: 'This login is taken.' } : null;
|
|
86
|
+
},
|
|
87
|
+
),
|
|
88
|
+
age: new SmartFormControl<number | null>(null, [SmartValidators.min(18)]),
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Binding inputs
|
|
93
|
+
|
|
94
|
+
- `useControlState(control)` returns the live `SmartControlState` (`value`, `status`, `errors`, `valid`, `invalid`, `pending`, `disabled`, `touched`, `dirty`, `required`) and re-renders on every change. `required` is `true` when the control's validators report `required` for an empty value (`isControlRequired`), which is how the inputs decide to show an asterisk.
|
|
95
|
+
- `useControlBinding(control)` adds `onChange(value)` (marks dirty, then sets the value, as a user edit) and `onBlur()` (marks touched).
|
|
96
|
+
- `useControlVersion(control)` only re-renders and returns a version number.
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { SmartAbstractControl, useControlBinding } from '@smartsoft001/react';
|
|
100
|
+
|
|
101
|
+
export function BoundTextInput({
|
|
102
|
+
control,
|
|
103
|
+
label,
|
|
104
|
+
}: {
|
|
105
|
+
control: SmartAbstractControl<string | null>;
|
|
106
|
+
label: string;
|
|
107
|
+
}) {
|
|
108
|
+
const { value, onChange, onBlur, errors, touched, required, disabled } =
|
|
109
|
+
useControlBinding(control);
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<label>
|
|
113
|
+
{label}
|
|
114
|
+
{required && ' *'}
|
|
115
|
+
<input
|
|
116
|
+
value={value ?? ''}
|
|
117
|
+
disabled={disabled}
|
|
118
|
+
onChange={(event) => onChange(event.target.value)}
|
|
119
|
+
onBlur={onBlur}
|
|
120
|
+
/>
|
|
121
|
+
{touched && errors?.['required'] && <small>Required</small>}
|
|
122
|
+
</label>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Building a form from a model
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import { Field, FieldType, Model } from '@smartsoft001/models';
|
|
131
|
+
import { SmartButton, SmartForm, useModelForm } from '@smartsoft001/react';
|
|
132
|
+
|
|
133
|
+
@Model({})
|
|
134
|
+
export class Signup {
|
|
135
|
+
@Field({
|
|
136
|
+
type: FieldType.text,
|
|
137
|
+
create: { required: true },
|
|
138
|
+
possibilities: { minLength: 3, maxLength: 30 },
|
|
139
|
+
})
|
|
140
|
+
name!: string;
|
|
141
|
+
|
|
142
|
+
@Field({ type: FieldType.email, create: { required: true }, unique: true })
|
|
143
|
+
email!: string;
|
|
144
|
+
|
|
145
|
+
@Field({
|
|
146
|
+
type: FieldType.password,
|
|
147
|
+
create: { required: true, confirm: true },
|
|
148
|
+
})
|
|
149
|
+
password!: string;
|
|
150
|
+
|
|
151
|
+
@Field({ type: FieldType.flag, create: true })
|
|
152
|
+
company!: boolean;
|
|
153
|
+
|
|
154
|
+
// Only in the form while `company` is true.
|
|
155
|
+
@Field({
|
|
156
|
+
type: FieldType.nip,
|
|
157
|
+
create: { required: true, enabled: { criteria: { company: true } } },
|
|
158
|
+
})
|
|
159
|
+
nip!: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const model = new Signup();
|
|
163
|
+
|
|
164
|
+
export function SignupForm({
|
|
165
|
+
isEmailFree,
|
|
166
|
+
onSignup,
|
|
167
|
+
}: {
|
|
168
|
+
isEmailFree: (email: string) => Promise<boolean>;
|
|
169
|
+
onSignup: (value: Signup) => void;
|
|
170
|
+
}) {
|
|
171
|
+
const form = useModelForm(model, {
|
|
172
|
+
mode: 'create',
|
|
173
|
+
uniqueProvider: async (values) =>
|
|
174
|
+
isEmailFree(String(values['email']).replace(/'/g, '')),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
if (!form) return null; // the factory builds asynchronously
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<>
|
|
181
|
+
<SmartForm
|
|
182
|
+
options={{ model, mode: 'create', show: true, control: form }}
|
|
183
|
+
/>
|
|
184
|
+
<SmartButton
|
|
185
|
+
options={{
|
|
186
|
+
click: () => {
|
|
187
|
+
form.markAllAsTouched();
|
|
188
|
+
if (form.valid) onSignup(form.value as Signup);
|
|
189
|
+
},
|
|
190
|
+
}}
|
|
191
|
+
>
|
|
192
|
+
Sign up
|
|
193
|
+
</SmartButton>
|
|
194
|
+
</>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`useModelForm(model, { mode?, uniqueProvider?, control? })` asks the provider's `FormFactory` (`useFormFactory()`) for the form, keeps it in state and rebuilds it when the model or the mode changes; it is `null` until the first build settles. `FormFactory` also works outside React: `new FormFactory({ authService: { expectPermissions: () => true } }).create(model, { mode: 'create' })` resolves with the `SmartFormGroup`.
|
|
200
|
+
|
|
201
|
+
### What the factory reads
|
|
202
|
+
|
|
203
|
+
| `@Field` option | Effect on the form |
|
|
204
|
+
| ---------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
205
|
+
| `create` / `update` (`true` or an object) | The field is in the `create` / `update` form; an object is merged over the field's options for that mode. |
|
|
206
|
+
| `update: { multi: true }` | The field is in the `multiUpdate` form. |
|
|
207
|
+
| `customs: [{ mode, ... }]` | The field is in a custom mode's form. |
|
|
208
|
+
| `required` | `SmartValidators.required` (an `address` group gets it on city, street, building number and zip code). |
|
|
209
|
+
| `type` | `email`, `phoneNumber`, `pesel` add their checks; `object` becomes a nested group of the `classType` model, `array` an array of groups, `address` a group of its five parts (zip code checked). The `nip` and `phoneNumberPl` inputs add their checks when rendered. |
|
|
210
|
+
| `possibilities: { minLength, maxLength, min, max }` | The matching `SmartValidators`. |
|
|
211
|
+
| `confirm` | A `<key>Confirm` control that must match (`confirm` error), re-checked when the original changes. |
|
|
212
|
+
| `unique` (`true` or `{ withFields }`) | An async check through `uniqueProvider(values)`: `values` holds the field (quoted with `'` unless it is an `int`) and the `withFields`. |
|
|
213
|
+
| `permissions` | The field is left out when the user has none of them (`AuthService.expectPermissions`). |
|
|
214
|
+
| `enabled` (a specification `{ criteria }`) | The control is added and removed as the form value changes; `$root.<path>` in the criteria refers to the root form of a nested one. A removed control is marked `smartDisabled`. |
|
|
215
|
+
| `defaltValue` (spelled so in `@smartsoft001/models`) | A function giving the initial value when the model has none. |
|
|
216
|
+
| `focused` | The input autofocuses. |
|
|
217
|
+
|
|
218
|
+
### Replacing validators per field
|
|
219
|
+
|
|
220
|
+
An `IModelValidatorsProvider` on `SmartProvider` (`modelValidatorsProvider`) is asked for every field, and what it returns **replaces** the derived validators, so return `options.base` to keep them:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import {
|
|
224
|
+
IModelValidators,
|
|
225
|
+
IModelValidatorsOptions,
|
|
226
|
+
IModelValidatorsProvider,
|
|
227
|
+
SmartValidatorFn,
|
|
228
|
+
SmartValidators,
|
|
229
|
+
} from '@smartsoft001/react';
|
|
230
|
+
|
|
231
|
+
const toArray = (
|
|
232
|
+
v: SmartValidatorFn | SmartValidatorFn[] | null | undefined,
|
|
233
|
+
): SmartValidatorFn[] => (Array.isArray(v) ? v : v ? [v] : []);
|
|
234
|
+
|
|
235
|
+
export class AppValidatorsProvider extends IModelValidatorsProvider {
|
|
236
|
+
async get(options: IModelValidatorsOptions): Promise<IModelValidators> {
|
|
237
|
+
// `options.type` is the model class (e.g. compare it with `Signup`), `options.instance` the model instance.
|
|
238
|
+
if (options.type?.name === 'Signup' && options.key === 'name') {
|
|
239
|
+
return {
|
|
240
|
+
...options.base,
|
|
241
|
+
validators: [
|
|
242
|
+
...toArray(options.base?.validators),
|
|
243
|
+
SmartValidators.pattern(/^[A-Za-z ]+$/),
|
|
244
|
+
],
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return options.base ?? {};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Exports
|
|
253
|
+
|
|
254
|
+
| Export | What it is |
|
|
255
|
+
| ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
|
|
256
|
+
| `SmartAbstractControl`, `SmartFormControl`, `SmartFormGroup`, `SmartFormArray` | The controls. |
|
|
257
|
+
| `SmartValidators`, `isControlRequired` | Built-in validators; whether a control is required. |
|
|
258
|
+
| `SmartValidatorFn`, `SmartAsyncValidatorFn`, `SmartValidationErrors`, `SmartValidatorResult`, `SmartControlStatus`, `SmartControlUpdateOptions` | Validator and control types. |
|
|
259
|
+
| `SmartEmitter`, `SmartSubscription` | The event source of the controls. |
|
|
260
|
+
| `useControlState`, `useControlBinding`, `useControlVersion`, `SmartControlState`, `SmartControlBinding` | React bindings. |
|
|
261
|
+
| `FormFactory`, `IFormFactoryOptions`, `IFormFactoryDependencies`, `SmartUniqueProvider` | The model form builder. |
|
|
262
|
+
| `useModelForm`, `IUseModelFormOptions`, `useFormFactory` | Building a form in a component; the provider's factory. |
|
|
263
|
+
| `IModelValidatorsProvider`, `IModelValidatorsOptions`, `IModelValidators` | Validators of model fields from code. |
|
|
264
|
+
|
|
265
|
+
## File Locations
|
|
266
|
+
|
|
267
|
+
Source: `packages/shared/react/src/lib/` in the smartsoft001 repository.
|
|
268
|
+
|
|
269
|
+
- `forms/`: `abstract-control.ts`, `form-control.ts`, `form-group.ts`, `form-array.ts`, `validators.ts`, `hooks.ts`, `emitter.ts`
|
|
270
|
+
- `factories/form/`: `form.factory.ts`, `use-model-form.ts`
|
|
271
|
+
- `providers/model-validators.provider.ts`
|
|
272
|
+
- `components/input/error/input-error-messages.ts`: the order and texts of the messages
|