mayoiga 0.0.21 → 0.1.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/.eslintrc.js +111 -0
- package/.github/workflows/checker.yml +42 -0
- package/.gitignore +217 -0
- package/.prettierrc +5 -0
- package/README.md +21 -45
- package/component.d.ts +1 -0
- package/component.js +1 -0
- package/dist/component.d.ts +7 -0
- package/dist/component.js +11 -0
- package/dist/component.test.d.ts +1 -0
- package/dist/component.test.js +53 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +146 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +74 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +6 -0
- package/docs/.nojekyll +0 -0
- package/docs/assets/css/main.css +2328 -0
- package/docs/assets/images/icons.png +0 -0
- package/docs/assets/images/icons@2x.png +0 -0
- package/docs/assets/images/widgets.png +0 -0
- package/docs/assets/images/widgets@2x.png +0 -0
- package/docs/assets/js/main.js +1 -0
- package/docs/assets/js/search.js +3 -0
- package/docs/globals.html +185 -0
- package/docs/index.html +249 -0
- package/docs/modules/_actions_.html +398 -0
- package/docs/modules/_forms_.html +362 -0
- package/docs/modules/_index_.html +170 -0
- package/docs/modules/_inputprotocol_.html +319 -0
- package/docs/modules/_mayoiga_.html +783 -0
- package/docs/modules/_reducer_.html +417 -0
- package/example/src/demo-app.tsx +123 -0
- package/example/src/index.tsx +15 -0
- package/package.json +41 -44
- package/setup/vite-global-setup.js +1 -0
- package/setup/vite-set-global.js +0 -0
- package/src/component.test.tsx +91 -0
- package/src/component.tsx +19 -0
- package/src/index.test.tsx +120 -0
- package/src/index.ts +303 -0
- package/src/utils.ts +8 -0
- package/tsconfig.json +38 -0
- package/vite.config.js +10 -0
- package/yarn.lock +2378 -0
- package/lib/actions.d.ts +0 -28
- package/lib/actions.js +0 -29
- package/lib/actions.js.map +0 -1
- package/lib/forms.d.ts +0 -21
- package/lib/forms.js +0 -82
- package/lib/forms.js.map +0 -1
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -7
- package/lib/index.js.map +0 -1
- package/lib/inputProtocol.d.ts +0 -17
- package/lib/inputProtocol.js +0 -24
- package/lib/inputProtocol.js.map +0 -1
- package/lib/mayoiga.d.ts +0 -110
- package/lib/mayoiga.js +0 -131
- package/lib/mayoiga.js.map +0 -1
- package/lib/reducer.d.ts +0 -20
- package/lib/reducer.js +0 -53
- package/lib/reducer.js.map +0 -1
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
plugins: ['@typescript-eslint/eslint-plugin', "testing-library"],
|
|
4
|
+
extends: [
|
|
5
|
+
'eslint:recommended',
|
|
6
|
+
'plugin:@typescript-eslint/recommended',
|
|
7
|
+
// Lintの時間があまりに長いようなら無効化も考える
|
|
8
|
+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
|
|
9
|
+
|
|
10
|
+
'plugin:import/errors',
|
|
11
|
+
'plugin:import/typescript',
|
|
12
|
+
'plugin:testing-library/react',
|
|
13
|
+
],
|
|
14
|
+
parserOptions: {
|
|
15
|
+
project: './tsconfig.json',
|
|
16
|
+
ecmaVersion: 'next',
|
|
17
|
+
sourceType: 'module',
|
|
18
|
+
ecaFeatures: {
|
|
19
|
+
jsx: true,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
rules: {
|
|
23
|
+
curly: 'error',
|
|
24
|
+
eqeqeq: ['error', 'smart'],
|
|
25
|
+
|
|
26
|
+
'no-return-await': 'error',
|
|
27
|
+
'require-await': 'error',
|
|
28
|
+
'no-await-in-loop': 'error',
|
|
29
|
+
|
|
30
|
+
'@typescript-eslint/prefer-readonly': 'error',
|
|
31
|
+
// '@typescript-eslint/prefer-readonly-parameter-types': ["error", {ignoreInferredTypes: true}],
|
|
32
|
+
'@typescript-eslint/array-type': ['error', {default: 'generic', readonly: 'generic'}],
|
|
33
|
+
'@typescript-eslint/consistent-type-imports': [
|
|
34
|
+
'error',
|
|
35
|
+
{
|
|
36
|
+
prefer: 'type-imports',
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
|
|
40
|
+
'padding-line-between-statements': [
|
|
41
|
+
'error',
|
|
42
|
+
{blankLine: 'always', prev: 'multiline-const', next: 'multiline-const'},
|
|
43
|
+
{blankLine: 'always', prev: '*', next: 'function'},
|
|
44
|
+
{blankLine: 'always', prev: '*', next: 'class'},
|
|
45
|
+
{blankLine: 'always', prev: '*', next: 'export'},
|
|
46
|
+
],
|
|
47
|
+
|
|
48
|
+
'no-use-before-define': 'off',
|
|
49
|
+
'@typescript-eslint/no-use-before-define': ['error'],
|
|
50
|
+
|
|
51
|
+
'no-unused-vars': 'off',
|
|
52
|
+
'@typescript-eslint/no-unused-vars': [
|
|
53
|
+
'error',
|
|
54
|
+
{
|
|
55
|
+
vars: 'all',
|
|
56
|
+
args: 'none',
|
|
57
|
+
ignoreRestSiblings: true,
|
|
58
|
+
|
|
59
|
+
argsIgnorePattern: '^_',
|
|
60
|
+
varsIgnorePattern: '^_',
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
|
|
64
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
65
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
66
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
67
|
+
|
|
68
|
+
// 要検討
|
|
69
|
+
'@typescript-eslint/no-unsafe-assignment': 'off',
|
|
70
|
+
// voidとのunionで微妙な挙動をするので一旦停止
|
|
71
|
+
'@typescript-eslint/no-misused-promises': [
|
|
72
|
+
'error',
|
|
73
|
+
{
|
|
74
|
+
checksVoidReturn: false,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
|
|
78
|
+
'import/newline-after-import': 'error',
|
|
79
|
+
'import/named': 'off',
|
|
80
|
+
'import/no-named-default': 'error',
|
|
81
|
+
'import/no-default-export': 'off',
|
|
82
|
+
'import/default': 'off',
|
|
83
|
+
'import/no-useless-path-segments': 'error',
|
|
84
|
+
'import/order': [
|
|
85
|
+
'error',
|
|
86
|
+
{
|
|
87
|
+
groups: ['builtin', 'external', 'internal', 'parent', ['index', 'sibling'], 'type'],
|
|
88
|
+
'newlines-between': 'always-and-inside-groups',
|
|
89
|
+
alphabetize: {
|
|
90
|
+
order: 'asc' /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */,
|
|
91
|
+
caseInsensitive: true /* ignore case. Options: [true, false] */,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
|
|
96
|
+
},
|
|
97
|
+
settings: {
|
|
98
|
+
'import/parsers': {
|
|
99
|
+
'@typescript-eslint/parser': ['.js', '.jsx', '.ts', '.tsx'],
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
'import/ignore': ['node_modules'],
|
|
103
|
+
},
|
|
104
|
+
"overrides": [
|
|
105
|
+
{
|
|
106
|
+
// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching files!
|
|
107
|
+
"files": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
|
|
108
|
+
"extends": ["plugin:testing-library/react"]
|
|
109
|
+
},
|
|
110
|
+
]
|
|
111
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: Lint Check
|
|
2
|
+
|
|
3
|
+
on: [push]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
node-version: [16.x]
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v1
|
|
16
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
17
|
+
uses: actions/setup-node@v1
|
|
18
|
+
with:
|
|
19
|
+
node-version: ${{ matrix.node-version }}
|
|
20
|
+
|
|
21
|
+
- uses: actions/checkout@v1
|
|
22
|
+
- name: Cache node_modules
|
|
23
|
+
uses: actions/cache@preview
|
|
24
|
+
with:
|
|
25
|
+
path: ~/.cache/yarn
|
|
26
|
+
key: ${{ runner.os }}-projectname-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
|
|
27
|
+
restore-keys:
|
|
28
|
+
${{ runner.os }}-projectname-
|
|
29
|
+
|
|
30
|
+
- name: Install node_modules
|
|
31
|
+
if: steps.cache.outputs.cache-hit != 'true'
|
|
32
|
+
run: |
|
|
33
|
+
npm install -g yarn
|
|
34
|
+
yarn install
|
|
35
|
+
|
|
36
|
+
- name: Lint
|
|
37
|
+
run: |
|
|
38
|
+
yarn check-all
|
|
39
|
+
|
|
40
|
+
- name: Test
|
|
41
|
+
run: |
|
|
42
|
+
yarn test
|
package/.gitignore
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
|
|
2
|
+
# Created by https://www.toptal.com/developers/gitignore/api/macos,node,visualstudiocode,vim
|
|
3
|
+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,node,visualstudiocode,vim
|
|
4
|
+
|
|
5
|
+
### macOS ###
|
|
6
|
+
# General
|
|
7
|
+
.DS_Store
|
|
8
|
+
.AppleDouble
|
|
9
|
+
.LSOverride
|
|
10
|
+
|
|
11
|
+
# Icon must end with two \r
|
|
12
|
+
Icon
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Thumbnails
|
|
16
|
+
._*
|
|
17
|
+
|
|
18
|
+
# Files that might appear in the root of a volume
|
|
19
|
+
.DocumentRevisions-V100
|
|
20
|
+
.fseventsd
|
|
21
|
+
.Spotlight-V100
|
|
22
|
+
.TemporaryItems
|
|
23
|
+
.Trashes
|
|
24
|
+
.VolumeIcon.icns
|
|
25
|
+
.com.apple.timemachine.donotpresent
|
|
26
|
+
|
|
27
|
+
# Directories potentially created on remote AFP share
|
|
28
|
+
.AppleDB
|
|
29
|
+
.AppleDesktop
|
|
30
|
+
Network Trash Folder
|
|
31
|
+
Temporary Items
|
|
32
|
+
.apdisk
|
|
33
|
+
|
|
34
|
+
### Node ###
|
|
35
|
+
# Logs
|
|
36
|
+
logs
|
|
37
|
+
*.log
|
|
38
|
+
npm-debug.log*
|
|
39
|
+
yarn-debug.log*
|
|
40
|
+
yarn-error.log*
|
|
41
|
+
lerna-debug.log*
|
|
42
|
+
.pnpm-debug.log*
|
|
43
|
+
|
|
44
|
+
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
45
|
+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
46
|
+
|
|
47
|
+
# Runtime data
|
|
48
|
+
pids
|
|
49
|
+
*.pid
|
|
50
|
+
*.seed
|
|
51
|
+
*.pid.lock
|
|
52
|
+
|
|
53
|
+
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
54
|
+
lib-cov
|
|
55
|
+
|
|
56
|
+
# Coverage directory used by tools like istanbul
|
|
57
|
+
coverage
|
|
58
|
+
*.lcov
|
|
59
|
+
|
|
60
|
+
# nyc test coverage
|
|
61
|
+
.nyc_output
|
|
62
|
+
|
|
63
|
+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
64
|
+
.grunt
|
|
65
|
+
|
|
66
|
+
# Bower dependency directory (https://bower.io/)
|
|
67
|
+
bower_components
|
|
68
|
+
|
|
69
|
+
# node-waf configuration
|
|
70
|
+
.lock-wscript
|
|
71
|
+
|
|
72
|
+
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
73
|
+
build/Release
|
|
74
|
+
|
|
75
|
+
# Dependency directories
|
|
76
|
+
node_modules/
|
|
77
|
+
jspm_packages/
|
|
78
|
+
|
|
79
|
+
# Snowpack dependency directory (https://snowpack.dev/)
|
|
80
|
+
web_modules/
|
|
81
|
+
|
|
82
|
+
# TypeScript cache
|
|
83
|
+
*.tsbuildinfo
|
|
84
|
+
|
|
85
|
+
# Optional npm cache directory
|
|
86
|
+
.npm
|
|
87
|
+
|
|
88
|
+
# Optional eslint cache
|
|
89
|
+
.eslintcache
|
|
90
|
+
|
|
91
|
+
# Optional stylelint cache
|
|
92
|
+
.stylelintcache
|
|
93
|
+
|
|
94
|
+
# Microbundle cache
|
|
95
|
+
.rpt2_cache/
|
|
96
|
+
.rts2_cache_cjs/
|
|
97
|
+
.rts2_cache_es/
|
|
98
|
+
.rts2_cache_umd/
|
|
99
|
+
|
|
100
|
+
# Optional REPL history
|
|
101
|
+
.node_repl_history
|
|
102
|
+
|
|
103
|
+
# Output of 'npm pack'
|
|
104
|
+
*.tgz
|
|
105
|
+
|
|
106
|
+
# Yarn Integrity file
|
|
107
|
+
.yarn-integrity
|
|
108
|
+
|
|
109
|
+
# dotenv environment variable files
|
|
110
|
+
.env
|
|
111
|
+
.env.development.local
|
|
112
|
+
.env.test.local
|
|
113
|
+
.env.production.local
|
|
114
|
+
.env.local
|
|
115
|
+
|
|
116
|
+
# parcel-bundler cache (https://parceljs.org/)
|
|
117
|
+
.cache
|
|
118
|
+
.parcel-cache
|
|
119
|
+
|
|
120
|
+
# Next.js build output
|
|
121
|
+
.next
|
|
122
|
+
out
|
|
123
|
+
|
|
124
|
+
# Nuxt.js build / generate output
|
|
125
|
+
.nuxt
|
|
126
|
+
dist
|
|
127
|
+
|
|
128
|
+
# Gatsby files
|
|
129
|
+
.cache/
|
|
130
|
+
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
131
|
+
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
132
|
+
# public
|
|
133
|
+
|
|
134
|
+
# vuepress build output
|
|
135
|
+
.vuepress/dist
|
|
136
|
+
|
|
137
|
+
# vuepress v2.x temp and cache directory
|
|
138
|
+
.temp
|
|
139
|
+
|
|
140
|
+
# Docusaurus cache and generated files
|
|
141
|
+
.docusaurus
|
|
142
|
+
|
|
143
|
+
# Serverless directories
|
|
144
|
+
.serverless/
|
|
145
|
+
|
|
146
|
+
# FuseBox cache
|
|
147
|
+
.fusebox/
|
|
148
|
+
|
|
149
|
+
# DynamoDB Local files
|
|
150
|
+
.dynamodb/
|
|
151
|
+
|
|
152
|
+
# TernJS port file
|
|
153
|
+
.tern-port
|
|
154
|
+
|
|
155
|
+
# Stores VSCode versions used for testing VSCode extensions
|
|
156
|
+
.vscode-test
|
|
157
|
+
|
|
158
|
+
# yarn v2
|
|
159
|
+
.yarn/cache
|
|
160
|
+
.yarn/unplugged
|
|
161
|
+
.yarn/build-state.yml
|
|
162
|
+
.yarn/install-state.gz
|
|
163
|
+
.pnp.*
|
|
164
|
+
|
|
165
|
+
### Node Patch ###
|
|
166
|
+
# Serverless Webpack directories
|
|
167
|
+
.webpack/
|
|
168
|
+
|
|
169
|
+
# Optional stylelint cache
|
|
170
|
+
|
|
171
|
+
# SvelteKit build / generate output
|
|
172
|
+
.svelte-kit
|
|
173
|
+
|
|
174
|
+
### Vim ###
|
|
175
|
+
# Swap
|
|
176
|
+
[._]*.s[a-v][a-z]
|
|
177
|
+
!*.svg # comment out if you don't need vector files
|
|
178
|
+
[._]*.sw[a-p]
|
|
179
|
+
[._]s[a-rt-v][a-z]
|
|
180
|
+
[._]ss[a-gi-z]
|
|
181
|
+
[._]sw[a-p]
|
|
182
|
+
|
|
183
|
+
# Session
|
|
184
|
+
Session.vim
|
|
185
|
+
Sessionx.vim
|
|
186
|
+
|
|
187
|
+
# Temporary
|
|
188
|
+
.netrwhist
|
|
189
|
+
*~
|
|
190
|
+
# Auto-generated tag files
|
|
191
|
+
tags
|
|
192
|
+
# Persistent undo
|
|
193
|
+
[._]*.un~
|
|
194
|
+
|
|
195
|
+
### VisualStudioCode ###
|
|
196
|
+
.vscode/*
|
|
197
|
+
!.vscode/settings.json
|
|
198
|
+
!.vscode/tasks.json
|
|
199
|
+
!.vscode/launch.json
|
|
200
|
+
!.vscode/extensions.json
|
|
201
|
+
!.vscode/*.code-snippets
|
|
202
|
+
|
|
203
|
+
# Local History for Visual Studio Code
|
|
204
|
+
.history/
|
|
205
|
+
|
|
206
|
+
# Built Visual Studio Code Extensions
|
|
207
|
+
*.vsix
|
|
208
|
+
|
|
209
|
+
### VisualStudioCode Patch ###
|
|
210
|
+
# Ignore all local history of files
|
|
211
|
+
.history
|
|
212
|
+
.ionide
|
|
213
|
+
|
|
214
|
+
# Support for Project snippet scope
|
|
215
|
+
|
|
216
|
+
# End of https://www.toptal.com/developers/gitignore/api/macos,node,visualstudiocode,vim
|
|
217
|
+
|
package/.prettierrc
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
[](https://github.com/hachibeeDI/mayoiga/actions/workflows/checker.yml)
|
|
2
|
+
|
|
1
3
|
# Mayoiga
|
|
2
4
|
|
|
3
5
|
The React Hook based form library. Of course it is TypeScript friendly.
|
|
@@ -13,8 +15,6 @@ The React Hook based form library. Of course it is TypeScript friendly.
|
|
|
13
15
|
|
|
14
16
|
With less boilerplate code!!
|
|
15
17
|
|
|
16
|
-
You can see more working demo [under the example directory](example/src/demo-app.tsx) .
|
|
17
|
-
|
|
18
18
|
|
|
19
19
|
## How to use
|
|
20
20
|
|
|
@@ -23,62 +23,38 @@ You can see more working demo [under the example directory](example/src/demo-app
|
|
|
23
23
|
There are only two functions which you should know to use Mayoiga.
|
|
24
24
|
|
|
25
25
|
- [useForm](https://hachibeedi.github.io/mayoiga/modules/_mayoiga_.html#useform)
|
|
26
|
-
- [createFormScope](https://hachibeedi.github.io/mayoiga/modules/_mayoiga_.html#createformscope)
|
|
27
|
-
|
|
28
|
-
And if you would like to bring it to production, you need to know how to about [InputProtocol](https://hachibeedi.github.io/mayoiga/modules/_inputprotocol_.html#inputprotocol).
|
|
29
|
-
To know about that, `mayoiga/lib/forms` can be a good example.
|
|
30
26
|
|
|
31
|
-
|
|
27
|
+
- [createFormScope](https://hachibeedi.github.io/mayoiga/modules/_mayoiga_.html#createformscope)
|
|
32
28
|
|
|
33
29
|
|
|
34
30
|
### Minimum example:
|
|
35
31
|
|
|
36
32
|
```typeScript
|
|
37
33
|
|
|
38
|
-
|
|
39
|
-
import {FC} from 'react';
|
|
40
|
-
|
|
41
|
-
import {useForm, createFormScope} from 'mayoiga';
|
|
42
|
-
import {Input, NumberInput} from 'mayoiga/lib/forms';
|
|
34
|
+
const TestFormHook = createFormHook({name:'', description: '', age: ''} as FormStateBeforeValidation, testSchema);
|
|
43
35
|
|
|
44
|
-
const
|
|
36
|
+
const INPUT_AGE = '9';
|
|
37
|
+
const PARSED_RESULT = Number(INPUT_AGE);
|
|
45
38
|
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
|
|
39
|
+
const handleSubmitTester = TestFormHook.handleSubmit((e) => (result) => {
|
|
40
|
+
if (result.success) {
|
|
41
|
+
expect(result.data.age).toBe(PARSED_RESULT);
|
|
42
|
+
} else {
|
|
43
|
+
expect('should not called').toBe(result.err);
|
|
49
44
|
}
|
|
50
|
-
|
|
51
|
-
return `less than ${max}`;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const choice = function<T>(...candidates: Array<T>) {
|
|
56
|
-
return (target: T) => (candidates.includes(target) ? undefined : 'You should choose from the candidates.');
|
|
57
|
-
};
|
|
45
|
+
});
|
|
58
46
|
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
47
|
+
const Top = () => {
|
|
48
|
+
return (
|
|
49
|
+
<div>
|
|
50
|
+
<AgeInputComponent formHook={TestFormHook} />
|
|
51
|
+
<button data-testid="submit-button" onClick={handleSubmitTester} />
|
|
52
|
+
</div>
|
|
53
|
+
)
|
|
63
54
|
};
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const DemoForm = scope(props => {
|
|
68
|
-
const {Form, Field} = useForm(context);
|
|
69
|
-
return (
|
|
70
|
-
<Form onSubmit={value => console.log(value)}>
|
|
71
|
-
<Field name="name" component={Input} validations={[required]} />
|
|
72
|
-
<Field name="age" component={NumberInput} validations={[between(5, 20)]} />
|
|
73
|
-
<Field name="seed" component={Input} validations={[choice('fish', 'squid', 'octopus')]} />
|
|
74
|
-
|
|
75
|
-
<button disabled={!props.touched || Object.values(props.errors).some(e => !!e.length)}>submit</button>
|
|
76
|
-
</Form>
|
|
77
|
-
);
|
|
56
|
+
act(() => {
|
|
57
|
+
render(<Top />);
|
|
78
58
|
});
|
|
79
59
|
|
|
80
|
-
export default function DemoApp() {
|
|
81
|
-
return <DemoForm initialState={INITIAL_FORM_STATE} onSubmit={value => alert(`submit ${JSON.stringify(value)}`)} />;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
60
|
```
|
package/component.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {Slicer, Field} from "./dist/component";
|
package/component.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/component.js');
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SliceProps, FieldProps, StateRestriction, Controller } from './index';
|
|
2
|
+
declare type ControlledComponentProps<State, Props> = Props & {
|
|
3
|
+
controller: Controller<State>;
|
|
4
|
+
};
|
|
5
|
+
export declare function Slicer<State extends StateRestriction, R extends ReadonlyArray<unknown>>(props: ControlledComponentProps<State, SliceProps<State, R>>): JSX.Element;
|
|
6
|
+
export declare function Field<State extends StateRestriction, Name extends keyof State>(props: ControlledComponentProps<State, FieldProps<State, Name>>): JSX.Element;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export function Slicer(props) {
|
|
3
|
+
const { controller, ...restProps } = props;
|
|
4
|
+
const { Slicer: Delegate } = controller.components;
|
|
5
|
+
return React.createElement(Delegate, { ...restProps });
|
|
6
|
+
}
|
|
7
|
+
export function Field(props) {
|
|
8
|
+
const { controller, ...restProps } = props;
|
|
9
|
+
const { Field: Delegate } = controller.components;
|
|
10
|
+
return React.createElement(Delegate, { ...restProps });
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { test, expect } from 'vitest';
|
|
5
|
+
import * as zod from 'zod';
|
|
6
|
+
import { Field, Slicer } from './component';
|
|
7
|
+
import { createFormHook, } from './index';
|
|
8
|
+
const testSchema = zod.object({
|
|
9
|
+
name: zod.string(),
|
|
10
|
+
description: zod.string(),
|
|
11
|
+
age: zod.string().transform(val => Number(val)).refine(val => Number.isNaN(val) ? 0 : val),
|
|
12
|
+
marked: zod.boolean(),
|
|
13
|
+
});
|
|
14
|
+
const createTestHook = () => createFormHook({ name: '', description: '', age: '', marked: false }, testSchema);
|
|
15
|
+
// afterEach(cleanup);
|
|
16
|
+
test('Field component works fine in basic usage.', async () => {
|
|
17
|
+
const TestFormHook = createTestHook();
|
|
18
|
+
const Top = () => {
|
|
19
|
+
return (React.createElement("div", null,
|
|
20
|
+
React.createElement(Field, { controller: TestFormHook.controller, name: "age" }, (tool) => React.createElement("input", { "data-testid": "age-input", ...tool }))));
|
|
21
|
+
};
|
|
22
|
+
render(React.createElement(Top, null));
|
|
23
|
+
const INPUT_AGE = '42';
|
|
24
|
+
// calling `userEvent` in here will cause race condition...
|
|
25
|
+
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
26
|
+
await waitFor(() => {
|
|
27
|
+
const ageInput = screen.getByTestId('age-input');
|
|
28
|
+
expect(ageInput.value).toBe(INPUT_AGE);
|
|
29
|
+
TestFormHook.api.actions.peek((s) => {
|
|
30
|
+
expect(s.value.age).toBe(INPUT_AGE);
|
|
31
|
+
expect(s.errors.age).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
test('Slice component works fine in basic usage.', async () => {
|
|
36
|
+
const TestFormHook = createTestHook();
|
|
37
|
+
const Top = () => {
|
|
38
|
+
return (React.createElement("div", null,
|
|
39
|
+
React.createElement(Slicer, { controller: TestFormHook.controller, selector: s => [s.value.age] }, (tool, age) => React.createElement("input", { "data-testid": "age-input", name: "age", value: age, onChange: e => tool.handleChange('age', e.target.value) }))));
|
|
40
|
+
};
|
|
41
|
+
render(React.createElement(Top, null));
|
|
42
|
+
const INPUT_AGE = '42';
|
|
43
|
+
// calling `userEvent` in here will cause race condition...
|
|
44
|
+
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
45
|
+
await waitFor(() => {
|
|
46
|
+
const ageInput = screen.getByTestId('age-input');
|
|
47
|
+
expect(ageInput.value).toBe(INPUT_AGE);
|
|
48
|
+
TestFormHook.api.actions.peek((s) => {
|
|
49
|
+
expect(s.value.age).toBe(INPUT_AGE);
|
|
50
|
+
expect(s.errors.age).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { Subscriber } from 'nozuchi';
|
|
2
|
+
import type { BaseSyntheticEvent, ChangeEvent, ReactNode, ReactElement } from 'react';
|
|
3
|
+
import type { infer as zodInfer, ZodIssue, ZodType } from 'zod';
|
|
4
|
+
export declare type StateRestriction = Record<string, any>;
|
|
5
|
+
declare type FormStatus = {
|
|
6
|
+
isDirty: boolean;
|
|
7
|
+
isValid: boolean;
|
|
8
|
+
};
|
|
9
|
+
declare type FormErrors<State extends StateRestriction> = {
|
|
10
|
+
[k in keyof State]: string | null;
|
|
11
|
+
};
|
|
12
|
+
export declare type FullFormState<State extends StateRestriction> = FormStatus & {
|
|
13
|
+
value: State;
|
|
14
|
+
errors: FormErrors<State>;
|
|
15
|
+
};
|
|
16
|
+
export declare type HandleChangeAction<State extends StateRestriction, R = void> = <Name extends keyof State>(name: Name, value: State[Name]) => R;
|
|
17
|
+
export declare type FieldProps<State extends StateRestriction, Name extends keyof State> = {
|
|
18
|
+
name: Name;
|
|
19
|
+
children: (tool: {
|
|
20
|
+
name: Name;
|
|
21
|
+
value: State[Name];
|
|
22
|
+
onChange(this: void, name: Name, value: State[Name]): void;
|
|
23
|
+
onChange(this: void, e: ChangeEvent<HTMLElement>): void;
|
|
24
|
+
}, value: State[Name], errorMessage: string | null) => ReactNode;
|
|
25
|
+
};
|
|
26
|
+
export declare type SliceProps<State extends StateRestriction, Selected extends ReadonlyArray<unknown>> = {
|
|
27
|
+
selector: (s: FullFormState<State>) => Selected;
|
|
28
|
+
children: (tool: {
|
|
29
|
+
handleChange: HandleChangeAction<State>;
|
|
30
|
+
}, ...value: Readonly<Selected>) => ReactNode;
|
|
31
|
+
};
|
|
32
|
+
declare type FormControllerBehavior<StateBeforeValidation> = {
|
|
33
|
+
/** For testing */
|
|
34
|
+
peek: (eyeball: (prev: FullFormState<StateBeforeValidation>) => void) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
35
|
+
reset: () => () => FullFormState<StateBeforeValidation>;
|
|
36
|
+
/**
|
|
37
|
+
* Initialize form state without validation.
|
|
38
|
+
*/
|
|
39
|
+
initializeForm: (initialVal: Partial<StateBeforeValidation>) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
40
|
+
/**
|
|
41
|
+
* Handle issue that zod reported
|
|
42
|
+
*/
|
|
43
|
+
handleIssues: (issues: ReadonlyArray<ZodIssue>) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
44
|
+
/**
|
|
45
|
+
* You can handle server side or other external error via this action.
|
|
46
|
+
*/
|
|
47
|
+
handleError: (reducer: (prev: FormErrors<StateBeforeValidation>) => FormErrors<StateBeforeValidation>) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
48
|
+
/**
|
|
49
|
+
*/
|
|
50
|
+
handleChange: HandleChangeAction<StateBeforeValidation, (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>>;
|
|
51
|
+
reduceFormState: (reducer: (prevValue: StateBeforeValidation) => StateBeforeValidation) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
52
|
+
handleBulkChange: (setter: (prev: StateBeforeValidation) => Partial<StateBeforeValidation>) => (prev: FullFormState<StateBeforeValidation>) => FullFormState<StateBeforeValidation>;
|
|
53
|
+
};
|
|
54
|
+
declare type ActionsCanBePublic<State extends StateRestriction> = {
|
|
55
|
+
reset: VoidFunction;
|
|
56
|
+
initializeForm: (initial: Partial<State>) => void;
|
|
57
|
+
handleChange: HandleChangeAction<State>;
|
|
58
|
+
handleBulkChange: (setter: (prev: State) => Partial<State>) => void;
|
|
59
|
+
handleError: (reducer: (prev: FormErrors<State>) => FormErrors<State>) => void;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Restricted type interface of the form store
|
|
63
|
+
* TODO: more restriction
|
|
64
|
+
*/
|
|
65
|
+
export declare type Controller<State extends StateRestriction> = {
|
|
66
|
+
useInitialize: (initialValue: Partial<State>) => void;
|
|
67
|
+
useSelector: <R>(selector: (state: FullFormState<State>) => R, isEqual?: (prev: R, current: R) => boolean) => R;
|
|
68
|
+
actions: ActionsCanBePublic<State>;
|
|
69
|
+
components: {
|
|
70
|
+
Field: <Name extends keyof State>(props: FieldProps<State, Name>) => ReactElement<any, any> | null;
|
|
71
|
+
Slicer: <Selected extends ReadonlyArray<unknown>>(props: SliceProps<State, Selected>) => ReactElement<any, any> | null;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
declare type FormHook<State extends StateRestriction, Schema extends ZodType<any, any, any>> = {
|
|
75
|
+
controller: Controller<State>;
|
|
76
|
+
handleSubmit: (handler: (e: BaseSyntheticEvent) => (val: {
|
|
77
|
+
success: true;
|
|
78
|
+
data: zodInfer<Schema>;
|
|
79
|
+
error: undefined;
|
|
80
|
+
} | {
|
|
81
|
+
success: false;
|
|
82
|
+
error: ReadonlyArray<ZodIssue>;
|
|
83
|
+
}) => void | Promise<void>) => (e: BaseSyntheticEvent) => void;
|
|
84
|
+
api: Subscriber<FullFormState<State>, FormControllerBehavior<State>>;
|
|
85
|
+
} & Controller<State>;
|
|
86
|
+
/**
|
|
87
|
+
* @typeParam InitialState should be shallow
|
|
88
|
+
*/
|
|
89
|
+
export declare function createFormHook<StateBeforeValidation, Schema extends ZodType<any, any, any>>(initialState: StateBeforeValidation, schema: Schema): FormHook<StateBeforeValidation, Schema>;
|
|
90
|
+
export declare function useFormSlice<State extends StateRestriction, Sliced>(formController: Controller<State>, selector: (s: FullFormState<State>) => Sliced): readonly [Sliced, ActionsCanBePublic<State>];
|
|
91
|
+
export {};
|