proje-react-panel 1.10.0 → 1.11.0-beta.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/CHANGELOG.md +38 -0
- package/README.md +23 -0
- package/dist/__tests__/components/form/NumberField.test.d.ts +1 -0
- package/dist/components/form/registerOptions.d.ts +12 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/components/form/NumberField.test.tsx +193 -0
- package/src/components/form/FormField.tsx +11 -2
- package/src/components/form/InnerForm.tsx +6 -0
- package/src/components/form/registerOptions.ts +50 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
This file starts at 1.11.0; earlier releases are only in the git history.
|
|
4
|
+
|
|
5
|
+
## 1.11.0-beta.0
|
|
6
|
+
|
|
7
|
+
### Changed — please read before upgrading
|
|
8
|
+
|
|
9
|
+
- **`inputType: 'number'` fields now reach `onSubmit` as numbers.** `FormField` registers them
|
|
10
|
+
with a `setValueAs` conversion, the same way `type: 'checkbox'` has always been registered as a
|
|
11
|
+
boolean. `@Type(() => Number)` on the model is no longer needed — and keeps working where it is
|
|
12
|
+
already written, since a value that is already a number passes through untouched.
|
|
13
|
+
- **An empty number field is now `undefined`, where it used to be `''`.** This is a deliberate
|
|
14
|
+
behaviour change, and the reason to try this release on a branch first:
|
|
15
|
+
- Before: `''` reached the body, and `@Type(() => Number)` turned it into `0`. A user who left
|
|
16
|
+
an optional number field empty silently saved a `0`.
|
|
17
|
+
- Now: the key is `undefined`, so `JSON.stringify` drops it from the request. `@IsOptional()`
|
|
18
|
+
fields stay unset, and a required `@IsInt()` fails client-side with the message shown under
|
|
19
|
+
the field.
|
|
20
|
+
- Why not react-hook-form's `valueAsNumber`: it produces `NaN` for an empty input. `NaN` is
|
|
21
|
+
neither `null` nor `undefined`, so `@IsOptional()` does not skip it and every untouched
|
|
22
|
+
optional number field would start failing validation — and `NaN` becomes `null` in JSON.
|
|
23
|
+
- If your backend distinguishes "field absent" from "field null", check those endpoints.
|
|
24
|
+
- **`type: 'hidden'` fields go through the same conversion.** A hidden id declared with
|
|
25
|
+
`inputType: 'number'` reaches the body as a number instead of a string.
|
|
26
|
+
- **`inputType: 'date'` is deliberately left as a string** — the native input's `'YYYY-MM-DD'`,
|
|
27
|
+
and `''` when empty. Existing consumers validate it with `@IsString()` / `@IsISO8601()` and post
|
|
28
|
+
it as-is, so it is never converted to a `Date`. Documented in the README rather than left to be
|
|
29
|
+
discovered.
|
|
30
|
+
- Multipart forms (`@Form({ type: 'formData' })`) no longer append `undefined`/`null` values on
|
|
31
|
+
top of what the DOM already put into the `FormData`; an empty number field used to be able to
|
|
32
|
+
post the literal string `undefined`.
|
|
33
|
+
|
|
34
|
+
### Added
|
|
35
|
+
|
|
36
|
+
- `src/__tests__/components/form/NumberField.test.tsx` pins all of the above: filled, empty,
|
|
37
|
+
hidden, `defaultValue`, programmatic `setValue`, text and date fields, plus a model that still
|
|
38
|
+
declares `@Type(() => Number)`.
|
package/README.md
CHANGED
|
@@ -99,6 +99,29 @@ The render function receives:
|
|
|
99
99
|
Because the value is whatever you pass to `onChange`, a custom field can hold an id, an
|
|
100
100
|
object or a file reference — validation and submit treat it like any other form value.
|
|
101
101
|
|
|
102
|
+
## What a field puts into the submitted body
|
|
103
|
+
|
|
104
|
+
The DOM hands every input value over as a string. The library converts it before it reaches
|
|
105
|
+
`onSubmit`, so a model does not need `@Type(() => Number)` to get a number:
|
|
106
|
+
|
|
107
|
+
| Field | Value in the submitted body |
|
|
108
|
+
| --------------------------------- | --------------------------------------------------------------------- |
|
|
109
|
+
| `@Input({ inputType: 'number' })` | `number` — and `undefined` when the field is empty |
|
|
110
|
+
| `@Input({ inputType: 'date' })` | `string` — the input's own `'YYYY-MM-DD'`, `''` when empty, no `Date` |
|
|
111
|
+
| `@Input({ type: 'checkbox' })` | `boolean` |
|
|
112
|
+
| everything else | `string` |
|
|
113
|
+
|
|
114
|
+
Notes:
|
|
115
|
+
|
|
116
|
+
- **Empty number fields are `undefined`, not `0`.** `JSON.stringify` drops the key, so
|
|
117
|
+
`@IsOptional()` fields simply stay unset, and a required `@IsInt()` fails with the message
|
|
118
|
+
under the field instead of quietly posting `0`.
|
|
119
|
+
- **Date fields stay strings.** They are meant to be validated with `@IsString()` /
|
|
120
|
+
`@IsISO8601()` and posted as-is; the library never turns them into `Date` objects.
|
|
121
|
+
- **`type: 'hidden'` follows the same rules** — declare `inputType: 'number'` on a hidden id
|
|
122
|
+
and it reaches the body as a number rather than a string.
|
|
123
|
+
- `@Type(() => Number)` on a model keeps working; it just is not needed anymore.
|
|
124
|
+
|
|
102
125
|
## Guides
|
|
103
126
|
|
|
104
127
|
- **[Dashboard Guide](./guides/DASHBOARD_GUIDE.md)** - Complete guide for using Dashboard components
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UseFormRegister } from 'react-hook-form';
|
|
2
|
+
import { InputConfiguration } from '../../decorators/form/Input';
|
|
3
|
+
type FieldRegisterOptions = Parameters<UseFormRegister<any>>[1];
|
|
4
|
+
/**
|
|
5
|
+
* The DOM hands every <input> value over as a string, number inputs included. Deciding what
|
|
6
|
+
* lands in the form body is this module's job — not the model author's: a forgotten
|
|
7
|
+
* `@Type(() => Number)` used to turn into a client-side `@IsInt()` error under the field, or a
|
|
8
|
+
* backend 400, with no visible cause. Checkbox already closed the same trap through
|
|
9
|
+
* `setValueAs`, see Checkbox.tsx.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getFieldRegisterOptions(inputType: InputConfiguration['inputType']): FieldRegisterOptions;
|
|
12
|
+
export {};
|