Compare commits
32 Commits
v1.0.1
...
v3.1.0-bet
Author | SHA1 | Date | |
---|---|---|---|
37b4b9b990 | |||
7e3ef0f492 | |||
c0034d5af6 | |||
694d31e68d | |||
a2edafdc22 | |||
17656c149a | |||
c9bb631073 | |||
8cbe5c3bf2 | |||
686b5643b3 | |||
ec4929d7d8 | |||
079d3f9d50 | |||
d1f5430374 | |||
fd166db46c | |||
c90742b200 | |||
7ac46825ee | |||
e3c4c53f23 | |||
c133ba5a20 | |||
efc06a9639 | |||
f475f18b29 | |||
f720068fcc | |||
d4a3fc4573 | |||
7c878fd5f1 | |||
f8f655a6af | |||
75221c24f6 | |||
3dfcdaf81f | |||
a8ca83123e | |||
4058b48959 | |||
1601626fd9 | |||
e1026429e9 | |||
754c06ac66 | |||
72abb41251 | |||
0e12431622 |
1
.commitlintrc.json
Normal file
1
.commitlintrc.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": ["@commitlint/config-conventional"] }
|
@ -1,9 +1,11 @@
|
||||
# For more information see: https://editorconfig.org/
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
@ -1,5 +1,5 @@
|
||||
build/
|
||||
dist/
|
||||
node_modules/
|
||||
.snapshots/
|
||||
*.min.js
|
||||
node_modules
|
||||
build
|
||||
dist
|
||||
.parcel-cache
|
||||
example
|
||||
|
34
.eslintrc
34
.eslintrc
@ -1,34 +0,0 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"extends": [
|
||||
"standard",
|
||||
"standard-react",
|
||||
"plugin:prettier/recommended",
|
||||
"prettier/standard",
|
||||
"prettier/react",
|
||||
"plugin:@typescript-eslint/eslint-recommended"
|
||||
],
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2020,
|
||||
"ecmaFeatures": {
|
||||
"legacyDecorators": true,
|
||||
"jsx": true
|
||||
}
|
||||
},
|
||||
"settings": {
|
||||
"react": {
|
||||
"version": "16"
|
||||
}
|
||||
},
|
||||
"rules": {
|
||||
"space-before-function-paren": 0,
|
||||
"react/prop-types": 0,
|
||||
"react/jsx-handler-names": 0,
|
||||
"react/jsx-fragments": 0,
|
||||
"react/no-unused-prop-types": 0,
|
||||
"import/export": 0
|
||||
}
|
||||
}
|
14
.eslintrc.json
Normal file
14
.eslintrc.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"extends": ["conventions", "prettier"],
|
||||
"plugins": ["prettier", "import", "unicorn"],
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
},
|
||||
"env": {
|
||||
"jest": true,
|
||||
"browser": true
|
||||
},
|
||||
"rules": {
|
||||
"prettier/prettier": "error"
|
||||
}
|
||||
}
|
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
20
.github/ISSUE_TEMPLATE/BUG.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/BUG.md
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
name: '🐛 Bug Report'
|
||||
about: 'Report an unexpected problem or unintended behavior.'
|
||||
title: '[Bug]'
|
||||
labels: 'bug'
|
||||
---
|
||||
|
||||
<!--
|
||||
Please provide a clear and concise description of what the bug is. Include
|
||||
screenshots if needed. Please make sure your issue has not already been fixed.
|
||||
-->
|
||||
|
||||
## Steps To Reproduce
|
||||
|
||||
1. Step 1
|
||||
2. Step 2
|
||||
|
||||
## The current behavior
|
||||
|
||||
## The expected behavior
|
18
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
Normal file
18
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
---
|
||||
name: '📜 Documentation'
|
||||
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
|
||||
title: '[Documentation]'
|
||||
labels: 'documentation'
|
||||
---
|
||||
|
||||
<!-- Please make sure your issue has not already been fixed. -->
|
||||
|
||||
## Documentation
|
||||
|
||||
<!-- Please uncomment the type of documentation problem this issue address -->
|
||||
|
||||
<!-- Documentation is Missing -->
|
||||
<!-- Documentation is Confusing -->
|
||||
<!-- Documentation has Typo errors -->
|
||||
|
||||
## Proposal
|
20
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
name: '✨ Feature Request'
|
||||
about: 'Suggest a new feature idea.'
|
||||
title: '[Feature]'
|
||||
labels: 'feature request'
|
||||
---
|
||||
|
||||
<!-- Please make sure your issue has not already been fixed. -->
|
||||
|
||||
## Description
|
||||
|
||||
<!-- A clear and concise description of the problem or missing capability... -->
|
||||
|
||||
## Describe the solution you'd like
|
||||
|
||||
<!-- If you have a solution in mind, please describe it. -->
|
||||
|
||||
## Describe alternatives you've considered
|
||||
|
||||
<!-- Have you considered any alternative solutions or workarounds? -->
|
20
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
name: '🔧 Improvement'
|
||||
about: 'Improve structure/format/performance/refactor/tests of the code.'
|
||||
title: '[Improvement]'
|
||||
labels: 'improvement'
|
||||
---
|
||||
|
||||
<!-- Please make sure your issue has not already been fixed. -->
|
||||
|
||||
## Type of Improvement
|
||||
|
||||
<!-- Please uncomment the type of improvements this issue address -->
|
||||
|
||||
<!-- Files and Folders Structure -->
|
||||
<!-- Performance -->
|
||||
<!-- Refactoring code -->
|
||||
<!-- Tests -->
|
||||
<!-- Not Sure? -->
|
||||
|
||||
## Proposal
|
8
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
Normal file
8
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
---
|
||||
name: '🙋 Question'
|
||||
about: 'Further information is requested.'
|
||||
title: '[Question]'
|
||||
labels: 'question'
|
||||
---
|
||||
|
||||
### Question
|
7
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
7
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
<!-- Please first discuss the change you wish to make via issue before making a change. It might avoid a waste of your time. -->
|
||||
|
||||
## What changes this PR introduce?
|
||||
|
||||
## List any relevant issue numbers
|
||||
|
||||
## Is there anything you'd like reviewers to focus on?
|
28
.github/workflows/build.yml
vendored
Normal file
28
.github/workflows/build.yml
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
name: 'Build'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.1.0'
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
run: 'npm install'
|
||||
|
||||
- name: 'Build Package'
|
||||
run: 'npm run build'
|
||||
|
||||
- name: 'Build Example'
|
||||
run: 'cd example && npm install && npm run build'
|
28
.github/workflows/lint.yml
vendored
Normal file
28
.github/workflows/lint.yml
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
name: 'Lint'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.1.0'
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
run: 'npm install'
|
||||
|
||||
- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
|
||||
- run: 'npm run lint:editorconfig'
|
||||
- run: 'npm run lint:markdown'
|
||||
- run: 'npm run lint:typescript'
|
||||
- run: 'npm run lint:prettier'
|
25
.github/workflows/node.js.yml
vendored
25
.github/workflows/node.js.yml
vendored
@ -1,25 +0,0 @@
|
||||
name: Node.js CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x, 14.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm ci
|
||||
- run: npm run test:build
|
||||
- run: npm run test:lint
|
29
.github/workflows/release.yml
vendored
Normal file
29
.github/workflows/release.yml
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
name: 'Release'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.1.0'
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
run: 'npm install'
|
||||
|
||||
- name: 'Build Package'
|
||||
run: 'npm run build'
|
||||
|
||||
- name: 'Release'
|
||||
run: 'npm run release'
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
25
.github/workflows/test.yml
vendored
Normal file
25
.github/workflows/test.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
name: 'Test'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, develop]
|
||||
pull_request:
|
||||
branches: [master, develop]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: 'ubuntu-latest'
|
||||
steps:
|
||||
- uses: 'actions/checkout@v3.0.0'
|
||||
|
||||
- name: 'Use Node.js'
|
||||
uses: 'actions/setup-node@v3.1.0'
|
||||
with:
|
||||
node-version: 'lts/*'
|
||||
cache: 'npm'
|
||||
|
||||
- name: 'Install'
|
||||
run: 'npm install'
|
||||
|
||||
- name: 'Test'
|
||||
run: 'npm run test'
|
44
.gitignore
vendored
44
.gitignore
vendored
@ -1,21 +1,39 @@
|
||||
# See https://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
node_modules
|
||||
.npm
|
||||
|
||||
# builds
|
||||
# production
|
||||
build
|
||||
dist
|
||||
.rpt2_cache
|
||||
|
||||
# testing
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
# envs
|
||||
.env
|
||||
.env.production
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
.parcel-cache
|
||||
.cache
|
||||
|
11
.markdownlint-cli2.jsonc
Normal file
11
.markdownlint-cli2.jsonc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"config": {
|
||||
"default": true,
|
||||
"MD013": false,
|
||||
"MD024": false,
|
||||
"MD033": false,
|
||||
"MD041": false
|
||||
},
|
||||
"globs": ["**/*.{md,mdx}"],
|
||||
"ignores": ["**/node_modules"]
|
||||
}
|
10
.prettierrc
10
.prettierrc
@ -1,10 +0,0 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": true,
|
||||
"semi": false,
|
||||
"tabWidth": 2,
|
||||
"bracketSpacing": true,
|
||||
"jsxBracketSameLine": false,
|
||||
"arrowParens": "always",
|
||||
"trailingComma": "none"
|
||||
}
|
6
.prettierrc.json
Normal file
6
.prettierrc.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": true,
|
||||
"semi": false,
|
||||
"trailingComma": "none"
|
||||
}
|
22
.releaserc.json
Normal file
22
.releaserc.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"branches": [
|
||||
"master",
|
||||
{ "name": "develop", "prerelease": "beta", "channel": "beta" }
|
||||
],
|
||||
"plugins": [
|
||||
[
|
||||
"@semantic-release/commit-analyzer",
|
||||
{
|
||||
"preset": "conventionalcommits"
|
||||
}
|
||||
],
|
||||
[
|
||||
"@semantic-release/release-notes-generator",
|
||||
{
|
||||
"preset": "conventionalcommits"
|
||||
}
|
||||
],
|
||||
"@semantic-release/npm",
|
||||
"@semantic-release/github"
|
||||
]
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- 12
|
||||
- 10
|
132
CODE_OF_CONDUCT.md
Normal file
132
CODE_OF_CONDUCT.md
Normal file
@ -0,0 +1,132 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, religion, or sexual identity
|
||||
and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
- Demonstrating empathy and kindness toward other people
|
||||
- Being respectful of differing opinions, viewpoints, and experiences
|
||||
- Giving and gracefully accepting constructive feedback
|
||||
- Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
- Focusing on what is best not just for us as individuals, but for the
|
||||
overall community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
- The use of sexualized language or imagery, and sexual attention or
|
||||
advances of any kind
|
||||
- Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
- Public or private harassment
|
||||
- Publishing others' private information, such as a physical or email
|
||||
address, without their explicit permission
|
||||
- Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official e-mail address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
contact@divlo.fr.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series
|
||||
of actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or
|
||||
permanent ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within
|
||||
the community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.0, available at
|
||||
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
|
||||
|
||||
Community Impact Guidelines were inspired by
|
||||
[Mozilla's code of conduct enforcement ladder][mozilla coc].
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
[https://www.contributor-covenant.org/faq][faq]. Translations are available
|
||||
at [https://www.contributor-covenant.org/translations][translations].
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
|
||||
[mozilla coc]: https://github.com/mozilla/diversity
|
||||
[faq]: https://www.contributor-covenant.org/faq
|
||||
[translations]: https://www.contributor-covenant.org/translations
|
52
CONTRIBUTING.md
Normal file
52
CONTRIBUTING.md
Normal file
@ -0,0 +1,52 @@
|
||||
# 💡 Contributing
|
||||
|
||||
Thanks a lot for your interest in contributing to **react-component-form**! 🎉
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
**react-component-form** has adopted the [Contributor Covenant](https://www.contributor-covenant.org/) as its Code of Conduct, and we expect project participants to adhere to it. Please read [the full text](./CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.
|
||||
|
||||
## Types of contributions
|
||||
|
||||
- Reporting a bug.
|
||||
- Suggest a new feature idea.
|
||||
- Correct spelling errors, improvements or additions to documentation files.
|
||||
- Improve structure/format/performance/refactor/tests of the code.
|
||||
|
||||
## Pull Requests
|
||||
|
||||
- **Please first discuss** the change you wish to make via issues.
|
||||
|
||||
- Ensure your code respect linting.
|
||||
|
||||
- Make sure your **code passes the tests**.
|
||||
|
||||
If you're adding new features to **react-component-form**, please include tests.
|
||||
|
||||
## Commits
|
||||
|
||||
The commit message guidelines respect
|
||||
[@commitlint/config-conventional](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional)
|
||||
and [Semantic Versioning](https://semver.org/) for releases.
|
||||
|
||||
### Types
|
||||
|
||||
Types define which kind of changes you made to the project.
|
||||
|
||||
| Types | Description |
|
||||
| -------- | ------------------------------------------------------------------------------------------------------------ |
|
||||
| feat | A new feature. |
|
||||
| fix | A bug fix. |
|
||||
| docs | Documentation only changes. |
|
||||
| style | Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc). |
|
||||
| refactor | A code change that neither fixes a bug nor adds a feature. |
|
||||
| perf | A code change that improves performance. |
|
||||
| test | Adding missing tests or correcting existing tests. |
|
||||
| build | Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm). |
|
||||
| ci | Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs). |
|
||||
| chore | Other changes that don't modify src or test files. |
|
||||
| revert | Reverts a previous commit. |
|
||||
|
||||
### Scopes
|
||||
|
||||
Scopes define what part of the code changed.
|
23
LICENSE
23
LICENSE
@ -1,8 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
Copyright © 2020 Divlo
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
Copyright (c) Divlo
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
78
README.md
78
README.md
@ -5,32 +5,43 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://gitmoji.carloscuesta.me/"><img src="https://camo.githubusercontent.com/2a4924a23bd9ef18afe793f4999b1b9ec474e48f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746d6f6a692d253230f09f989c253230f09f988d2d4646444436372e7376673f7374796c653d666c61742d737175617265" alt="Gitmoji"/></a>
|
||||
<a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide"/></a>
|
||||
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
|
||||
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
|
||||
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
|
||||
<br />
|
||||
<a href="https://github.com/Divlo/react-component-form/actions/workflows/build.yml"><img src="https://github.com/Divlo/react-component-form/actions/workflows/build.yml/badge.svg?branch=master" /></a>
|
||||
<a href="https://github.com/Divlo/react-component-form/actions/workflows/lint.yml"><img src="https://github.com/Divlo/react-component-form/actions/workflows/lint.yml/badge.svg?branch=master" /></a>
|
||||
<a href="https://github.com/Divlo/react-component-form/actions/workflows/test.yml"><img src="https://github.com/Divlo/react-component-form/actions/workflows/test.yml/badge.svg?branch=master" /></a>
|
||||
<br />
|
||||
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
|
||||
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
|
||||
<a href="https://www.npmjs.com/package/react-component-form"><img src="https://img.shields.io/npm/v/react-component-form.svg" alt="npm version"></a>
|
||||
<img src="https://github.com/Divlo/react-component-form/workflows/Node.js%20CI/badge.svg" alt="Node.js CI" />
|
||||
</p>
|
||||
|
||||
## 📜 About
|
||||
|
||||
**react-component-form** is a lightweight form component for [React.js](https://reactjs.org/), it allows you to get the inputs values without state thanks to `onChange` or `onSubmit` props.
|
||||
|
||||
Demo : [https://divlo.github.io/react-component-form/](https://divlo.github.io/react-component-form/).
|
||||
There is also a [React Hooks](https://reactjs.org/docs/hooks-intro.html) to be used in combination with the `<Form />` component to validate the data with [Ajv JSON schema validator](https://ajv.js.org/), see [advanced usage](#%EF%B8%8F-advanced-usage).
|
||||
|
||||
Demo: [https://divlo.github.io/react-component-form/](https://divlo.github.io/react-component-form/).
|
||||
|
||||
## 💾 Install
|
||||
|
||||
```bash
|
||||
```sh
|
||||
npm install --save react-component-form
|
||||
```
|
||||
|
||||
## ⚙️ Usage
|
||||
|
||||
_Note : The examples use TypeScript, but obviously you can use JavaScript. Be aware that `HandleForm` is the type definition for the `onChange` and `onSubmit` props._
|
||||
|
||||
```tsx
|
||||
import React from 'react'
|
||||
import Form, { HandleForm } from 'react-component-form'
|
||||
import { Form } from 'react-component-form'
|
||||
import type { HandleForm } from 'react-component-form'
|
||||
|
||||
const Example = () => {
|
||||
export const Example = () => {
|
||||
const handleSubmit: HandleForm = (formData, formElement) => {
|
||||
console.log(formData) // { inputName: 'value of the input' }
|
||||
formElement.reset()
|
||||
@ -45,15 +56,62 @@ const Example = () => {
|
||||
}
|
||||
```
|
||||
|
||||
_Note : The example use TypeScript, but obviously you can use JavaScript. Be aware that `HandleForm` is the type definition for the `onChange` and `onSubmit` props._
|
||||
|
||||
Basically you have access to the same props of the HTML `form` tag in React, but the onSubmit and the onChange props are differents.
|
||||
|
||||
Instead to get the `event` param you get `formData` and `formElement` params :
|
||||
Instead to get the `event` param you get `formData` and `formElement` parameters:
|
||||
|
||||
- `formData`: It's an object where the keys are the name of your inputs and the current value. Behind the scene, it uses the [FormData](https://developer.mozilla.org/docs/Web/API/FormData) constructor.
|
||||
- `formElement`: It's the actual HTML form element in the DOM so for example you can access the `.reset()` method on a [HTMLFormElement](https://developer.mozilla.org/docs/Web/API/HTMLFormElement).
|
||||
|
||||
## ⚙️ Advanced Usage
|
||||
|
||||
This example shows how to use the `<Form />` component with `useForm` hook to validate the data with [Ajv JSON schema validator](https://ajv.js.org/).
|
||||
|
||||
You can see a more detailled example in the [./example](./example) folder.
|
||||
|
||||
```tsx
|
||||
import React from 'react'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
const schema = {
|
||||
inputName: {
|
||||
type: 'string',
|
||||
minLength: 3,
|
||||
maxLength: 20
|
||||
}
|
||||
}
|
||||
|
||||
export const Example = () => {
|
||||
const { errors, handleUseForm } = useForm(schema)
|
||||
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = (
|
||||
formData,
|
||||
formElement
|
||||
) => {
|
||||
console.log(formData) // { inputName: 'value of the input validated' }
|
||||
formElement.reset()
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<Form onSubmit={handleUseForm(onSubmit)}>
|
||||
<input type='text' name='inputName' />
|
||||
{errors.inputName != null && <p>{errors.inputName[0].message}</p>}
|
||||
|
||||
<button type='submit'>Submit</button>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## 💡 Contributing
|
||||
|
||||
Anyone can help to improve the project, submit a Feature Request, a bug report or
|
||||
even correct a simple spelling mistake.
|
||||
|
||||
The steps to contribute can be found in [CONTRIBUTING.md](./CONTRIBUTING.md).
|
||||
|
||||
## 📄 License
|
||||
|
||||
[MIT](./LICENSE)
|
||||
|
@ -1,5 +0,0 @@
|
||||
This example was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
It is linked to the react-component-form package in the parent directory for development purposes.
|
||||
|
||||
You can run `npm install` and then `npm start` to test your package.
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 83 KiB |
1
example/globals.d.ts
vendored
Normal file
1
example/globals.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
declare module '*.jpg'
|
13
example/index.html
Normal file
13
example/index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="shortcut icon" href="./github.jpg" type="image/jpg" />
|
||||
<title>react-component-form</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="./index.tsx"></script>
|
||||
</body>
|
||||
</html>
|
38
example/index.tsx
Normal file
38
example/index.tsx
Normal file
@ -0,0 +1,38 @@
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import React from 'react'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
const schema = {
|
||||
inputName: {
|
||||
type: 'string',
|
||||
minLength: 3,
|
||||
maxLength: 20
|
||||
}
|
||||
}
|
||||
|
||||
export const Example = () => {
|
||||
const { errors, handleUseForm } = useForm(schema)
|
||||
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = (
|
||||
formData,
|
||||
formElement
|
||||
) => {
|
||||
console.log(formData) // { inputName: 'value of the input validated' }
|
||||
formElement.reset()
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<Form onSubmit={handleUseForm(onSubmit)}>
|
||||
<input type='text' name='inputName' />
|
||||
{errors.inputName != null && <p>{errors.inputName[0].message}</p>}
|
||||
|
||||
<button type='submit'>Submit</button>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
|
||||
const container = document.getElementById('root') as HTMLElement
|
||||
const root = createRoot(container)
|
||||
root.render(<Example />)
|
34002
example/package-lock.json
generated
34002
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,44 +1,22 @@
|
||||
{
|
||||
"name": "react-component-form-example",
|
||||
"homepage": ".",
|
||||
"version": "0.0.0",
|
||||
"name": "example",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node ../node_modules/react-scripts/bin/react-scripts.js start",
|
||||
"build": "node ../node_modules/react-scripts/bin/react-scripts.js build",
|
||||
"test": "node ../node_modules/react-scripts/bin/react-scripts.js test",
|
||||
"eject": "node ../node_modules/react-scripts/bin/react-scripts.js eject"
|
||||
"start": "parcel index.html",
|
||||
"build": "parcel build index.html --public-url \"/react-component-form/\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@testing-library/jest-dom": "file:../node_modules/@testing-library/jest-dom",
|
||||
"@testing-library/react": "file:../node_modules/@testing-library/react",
|
||||
"@testing-library/user-event": "file:../node_modules/@testing-library/user-event",
|
||||
"@types/jest": "file:../node_modules/@types/jest",
|
||||
"@types/node": "file:../node_modules/@types/node",
|
||||
"@types/react": "file:../node_modules/@types/react",
|
||||
"@types/react-dom": "file:../node_modules/@types/react-dom",
|
||||
"react": "file:../node_modules/react",
|
||||
"react-component-form": "file:..",
|
||||
"react-dom": "file:../node_modules/react-dom",
|
||||
"react-scripts": "file:../node_modules/react-scripts",
|
||||
"typescript": "file:../node_modules/typescript"
|
||||
"react-dom": "file:../node_modules/react-dom"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/plugin-syntax-object-rest-spread": "^7.8.3"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "react-app"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
"@parcel/transformer-image": "2.7.0",
|
||||
"@types/react": "18.0.17",
|
||||
"@types/react-dom": "18.0.6",
|
||||
"parcel": "2.7.0",
|
||||
"process": "^0.11.10",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 3.8 KiB |
@ -1,48 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1, shrink-to-fit=no"
|
||||
/>
|
||||
<meta name="theme-color" content="#000000" />
|
||||
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is added to the
|
||||
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>react-component-form</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
|
||||
<div id="root"></div>
|
||||
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
</html>
|
@ -1,15 +0,0 @@
|
||||
{
|
||||
"short_name": "react-component-form",
|
||||
"name": "react-component-form",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
}
|
||||
],
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import Form, { HandleForm } from 'react-component-form'
|
||||
|
||||
import GithubLogo from './github.jpg'
|
||||
import './index.css'
|
||||
|
||||
const App = () => {
|
||||
const handleSubmit: HandleForm = (formData, formElement) => {
|
||||
console.clear()
|
||||
console.log('onSubmit: ', formData)
|
||||
formElement.reset()
|
||||
}
|
||||
|
||||
const handleChange: HandleForm = (formData) => {
|
||||
console.log('onChange: ', formData)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='container'>
|
||||
<h2>{'<Form />'}</h2>
|
||||
<h5 className='title-install'>npm i react-component-form</h5>
|
||||
|
||||
<Form onSubmit={handleSubmit} onChange={handleChange}>
|
||||
<div className='form-group'>
|
||||
<label htmlFor='name'>Name :</label>
|
||||
<input
|
||||
className='form-control'
|
||||
type='text'
|
||||
name='name'
|
||||
id='name'
|
||||
placeholder='name'
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type='submit' className='btn btn-primary'>
|
||||
Submit
|
||||
</button>
|
||||
</Form>
|
||||
|
||||
<div className='result-container'>
|
||||
<h4>
|
||||
Try the form and Inspect the console{' '}
|
||||
<span role='img' aria-label='smiley'>
|
||||
😃
|
||||
</span>
|
||||
</h4>
|
||||
</div>
|
||||
|
||||
<div className='github-logo'>
|
||||
<a
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
href='https://github.com/Divlo/react-component-form'
|
||||
>
|
||||
<img width='30px' alt='github' src={GithubLogo} />
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
1
example/src/react-app-env.d.ts
vendored
1
example/src/react-app-env.d.ts
vendored
@ -1 +0,0 @@
|
||||
/// <reference types="react-scripts" />
|
@ -1,30 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"module": "esnext",
|
||||
"lib": ["dom", "esnext", "DOM.Iterable"],
|
||||
"moduleResolution": "node",
|
||||
"module": "ESNext",
|
||||
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
||||
"target": "ESNext",
|
||||
"jsx": "react",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"esModuleInterop": true,
|
||||
"noImplicitReturns": true,
|
||||
"noImplicitThis": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"suppressImplicitAnyIndexErrors": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "es5",
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "build"]
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
|
7
jest.config.json
Normal file
7
jest.config.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"testEnvironment": "jsdom",
|
||||
"rootDir": "./src",
|
||||
"transform": {
|
||||
"^.+\\.tsx?$": "esbuild-jest"
|
||||
}
|
||||
}
|
40231
package-lock.json
generated
40231
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
103
package.json
103
package.json
@ -1,21 +1,14 @@
|
||||
{
|
||||
"name": "react-component-form",
|
||||
"version": "1.0.1",
|
||||
"version": "0.0.0-development",
|
||||
"public": true,
|
||||
"type": "module",
|
||||
"description": "Manage React Forms with ease.",
|
||||
"author": "Divlo <contact@divlo.fr>",
|
||||
"license": "MIT",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.modern.js",
|
||||
"source": "src/index.tsx",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Divlo/react-component-form.git"
|
||||
"url": "https://github.com/Divlo/react-component-form.git"
|
||||
},
|
||||
"keywords": [
|
||||
"react-form",
|
||||
@ -24,51 +17,57 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/Divlo/react-component-form/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Divlo/react-component-form#readme",
|
||||
"homepage": "https://github.com/Divlo/react-component-form",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "microbundle-crl --no-compress --format modern,cjs",
|
||||
"start": "microbundle-crl watch --no-compress --format modern,cjs",
|
||||
"prepare": "run-s build",
|
||||
"test": "run-s test:unit test:lint test:build",
|
||||
"test:build": "run-s build",
|
||||
"test:lint": "eslint ./**/*.{js,jsx,ts,tsx}",
|
||||
"test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
|
||||
"test:watch": "react-scripts test --env=jsdom",
|
||||
"predeploy": "cd example && npm install && npm run build",
|
||||
"deploy": "gh-pages -d example/build"
|
||||
"build": "tsup",
|
||||
"test": "jest",
|
||||
"lint:commit": "commitlint",
|
||||
"lint:editorconfig": "editorconfig-checker",
|
||||
"lint:markdown": "markdownlint-cli2",
|
||||
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\"",
|
||||
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
||||
"release": "semantic-release"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^16.0.0"
|
||||
"react": ">=16"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sinclair/typebox": "0.24.28",
|
||||
"ajv": "8.11.0",
|
||||
"ajv-formats": "2.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@testing-library/jest-dom": "^4.2.4",
|
||||
"@testing-library/react": "^9.5.0",
|
||||
"@testing-library/user-event": "^7.2.1",
|
||||
"@types/jest": "^25.1.4",
|
||||
"@types/node": "^12.12.38",
|
||||
"@types/react": "^16.9.27",
|
||||
"@types/react-dom": "^16.9.7",
|
||||
"@typescript-eslint/eslint-plugin": "^2.26.0",
|
||||
"@typescript-eslint/parser": "^2.26.0",
|
||||
"microbundle-crl": "^0.13.10",
|
||||
"babel-eslint": "^10.0.3",
|
||||
"cross-env": "^7.0.2",
|
||||
"eslint": "^6.8.0",
|
||||
"eslint-config-prettier": "^6.7.0",
|
||||
"eslint-config-standard": "^14.1.0",
|
||||
"eslint-config-standard-react": "^9.2.0",
|
||||
"eslint-plugin-import": "^2.18.2",
|
||||
"eslint-plugin-node": "^11.0.0",
|
||||
"eslint-plugin-prettier": "^3.1.1",
|
||||
"eslint-plugin-promise": "^4.2.1",
|
||||
"eslint-plugin-react": "^7.17.0",
|
||||
"eslint-plugin-standard": "^4.0.1",
|
||||
"gh-pages": "^2.2.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.4",
|
||||
"react": "^16.13.1",
|
||||
"react-dom": "^16.13.1",
|
||||
"react-scripts": "^3.4.1",
|
||||
"typescript": "^3.7.5"
|
||||
"@commitlint/cli": "17.0.3",
|
||||
"@commitlint/config-conventional": "17.0.3",
|
||||
"@testing-library/react": "13.3.0",
|
||||
"@types/jest": "28.1.8",
|
||||
"@types/react": "18.0.17",
|
||||
"@types/react-dom": "18.0.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.35.1",
|
||||
"@typescript-eslint/parser": "5.35.1",
|
||||
"editorconfig-checker": "4.0.2",
|
||||
"esbuild": "0.15.5",
|
||||
"esbuild-jest": "0.5.0",
|
||||
"eslint": "8.22.0",
|
||||
"eslint-config-conventions": "3.0.0",
|
||||
"eslint-config-prettier": "8.5.0",
|
||||
"eslint-plugin-import": "2.26.0",
|
||||
"eslint-plugin-prettier": "4.2.1",
|
||||
"eslint-plugin-promise": "6.0.1",
|
||||
"eslint-plugin-unicorn": "43.0.2",
|
||||
"jest": "29.0.0",
|
||||
"jest-environment-jsdom": "29.0.0",
|
||||
"markdownlint-cli2": "0.5.1",
|
||||
"prettier": "2.7.1",
|
||||
"react": "18.2.0",
|
||||
"react-dom": "18.2.0",
|
||||
"semantic-release": "19.0.5",
|
||||
"tsup": "6.2.2",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
}
|
||||
|
41
src/__test__/index.test.tsx
Normal file
41
src/__test__/index.test.tsx
Normal file
@ -0,0 +1,41 @@
|
||||
import React from 'react'
|
||||
import { render, cleanup, fireEvent } from '@testing-library/react'
|
||||
|
||||
import { Form, HandleForm } from '..'
|
||||
|
||||
afterEach(cleanup)
|
||||
|
||||
describe('<Form />', () => {
|
||||
it('should get the formData and formElement onSubmit and onChange', () => {
|
||||
let formData: { [k: string]: any } = {}
|
||||
let formElement: any = null
|
||||
const handleSubmitChange: HandleForm = (data, element) => {
|
||||
formData = data
|
||||
formElement = element
|
||||
}
|
||||
const formComponent = render(
|
||||
<Form onSubmit={handleSubmitChange} onChange={handleSubmitChange}>
|
||||
<input data-testid='input-form' type='text' name='inputName' />
|
||||
<button data-testid='button-submit' type='submit'>
|
||||
Submit
|
||||
</button>
|
||||
</Form>
|
||||
)
|
||||
const inputForm = formComponent.getByTestId(
|
||||
'input-form'
|
||||
) as HTMLInputElement
|
||||
const buttonSubmit = formComponent.getByTestId('button-submit')
|
||||
const text = 'some random text'
|
||||
|
||||
fireEvent.change(inputForm, { target: { value: text } })
|
||||
expect(formData.inputName).toEqual(text)
|
||||
expect(formElement instanceof HTMLFormElement).toBeTruthy()
|
||||
formData = {}
|
||||
formElement = null
|
||||
|
||||
fireEvent.click(buttonSubmit)
|
||||
expect(Object.keys(formData).length).toEqual(1)
|
||||
expect(formData.inputName).toEqual(text)
|
||||
expect(formElement instanceof HTMLFormElement).toBeTruthy()
|
||||
})
|
||||
})
|
@ -1,5 +1,4 @@
|
||||
import React, { useRef } from 'react'
|
||||
import { fromEntries } from './fromEntries'
|
||||
|
||||
export interface FormDataObject {
|
||||
[key: string]: FormDataEntryValue
|
||||
@ -8,38 +7,38 @@ export interface FormDataObject {
|
||||
export type HandleForm = (
|
||||
formData: FormDataObject,
|
||||
formElement: HTMLFormElement
|
||||
) => void
|
||||
|
||||
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
|
||||
) => void | Promise<void>
|
||||
|
||||
interface ReactFormProps
|
||||
extends Omit<React.HTMLProps<HTMLFormElement>, 'onSubmit' | 'onChange'> {}
|
||||
|
||||
interface FormProps extends ReactFormProps {
|
||||
export interface FormProps extends ReactFormProps {
|
||||
onSubmit?: HandleForm
|
||||
onChange?: HandleForm
|
||||
}
|
||||
|
||||
const getFormDataObject = (formElement: HTMLFormElement): FormDataObject => {
|
||||
return fromEntries<FormDataEntryValue>(new FormData(formElement))
|
||||
export const getFormDataObject = (
|
||||
formElement: HTMLFormElement
|
||||
): FormDataObject => {
|
||||
return Object.fromEntries<FormDataEntryValue>(new FormData(formElement))
|
||||
}
|
||||
|
||||
const Form = (props: FormProps): JSX.Element => {
|
||||
export const Form: React.FC<FormProps> = (props) => {
|
||||
const { onSubmit, onChange, children, ...rest } = props
|
||||
const formRef = useRef<HTMLFormElement>(null)
|
||||
|
||||
const handleSubmit = (event: React.FormEvent): void => {
|
||||
const handleSubmit = async (event: React.FormEvent): Promise<void> => {
|
||||
event.preventDefault()
|
||||
if (onSubmit != null) {
|
||||
const formData = getFormDataObject(formRef.current as HTMLFormElement)
|
||||
onSubmit(formData, formRef.current as HTMLFormElement)
|
||||
await onSubmit(formData, formRef.current as HTMLFormElement)
|
||||
}
|
||||
}
|
||||
|
||||
const handleChange = (): void => {
|
||||
const handleChange = async (): Promise<void> => {
|
||||
if (onChange != null) {
|
||||
const formData = getFormDataObject(formRef.current as HTMLFormElement)
|
||||
onChange(formData, formRef.current as HTMLFormElement)
|
||||
await onChange(formData, formRef.current as HTMLFormElement)
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,5 +53,3 @@ const Form = (props: FormProps): JSX.Element => {
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
export default Form
|
@ -1,9 +0,0 @@
|
||||
/*! fromentries. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
||||
export function fromEntries<T = any> (
|
||||
iterable: Iterable<readonly [PropertyKey, T]>
|
||||
) {
|
||||
return [...iterable].reduce((object, [key, value]) => {
|
||||
object[key] = value
|
||||
return object
|
||||
}, {})
|
||||
}
|
15
src/hooks/useFetchState.ts
Normal file
15
src/hooks/useFetchState.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
export const fetchState = ['idle', 'loading', 'error', 'success'] as const
|
||||
|
||||
export type FetchState = typeof fetchState[number]
|
||||
|
||||
export const useFetchState = (
|
||||
initialFetchState: FetchState = 'idle'
|
||||
): [
|
||||
fetchState: FetchState,
|
||||
setFetchState: React.Dispatch<React.SetStateAction<FetchState>>
|
||||
] => {
|
||||
const [fetchState, setFetchState] = useState<FetchState>(initialFetchState)
|
||||
return [fetchState, setFetchState]
|
||||
}
|
147
src/hooks/useForm.ts
Normal file
147
src/hooks/useForm.ts
Normal file
@ -0,0 +1,147 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { SchemaOptions, Static, TObject, Type } from '@sinclair/typebox'
|
||||
import type { ErrorObject } from 'ajv'
|
||||
|
||||
import type { HandleForm } from '../components/Form'
|
||||
import { FetchState, useFetchState } from './useFetchState'
|
||||
import { ajv } from '../utils/ajv'
|
||||
import { handleCheckboxBoolean } from '../utils/handleCheckboxBoolean'
|
||||
import { handleOptionalEmptyStringToNull } from '../utils/handleOptionalEmptyStringToNull'
|
||||
|
||||
export type Schema = SchemaOptions
|
||||
|
||||
export type Error = ErrorObject
|
||||
|
||||
export type ErrorsObject<K extends Schema> = {
|
||||
[key in keyof Partial<K>]: Error[] | undefined
|
||||
}
|
||||
|
||||
export type HandleUseFormCallback<K extends Schema> = (
|
||||
formData: Static<TObject<K>>,
|
||||
formElement: HTMLFormElement
|
||||
) => Promise<Message<K> | null> | Message<K> | null
|
||||
|
||||
export type HandleUseForm<K extends Schema> = (
|
||||
callback?: HandleUseFormCallback<K>
|
||||
) => HandleForm
|
||||
|
||||
export interface GlobalMessage {
|
||||
type: 'error' | 'success'
|
||||
value?: string
|
||||
properties?: undefined
|
||||
}
|
||||
|
||||
export interface PropertiesMessage<K extends Schema> {
|
||||
type: 'error'
|
||||
value?: string
|
||||
properties: { [key in keyof Partial<K>]: string }
|
||||
}
|
||||
|
||||
export type Message<K extends Schema> = GlobalMessage | PropertiesMessage<K>
|
||||
|
||||
export interface UseFormResult<K extends Schema> {
|
||||
handleUseForm: HandleUseForm<K>
|
||||
|
||||
readonly fetchState: FetchState
|
||||
setFetchState: React.Dispatch<React.SetStateAction<FetchState>>
|
||||
|
||||
/**
|
||||
* Global message of the form (not specific to a property).
|
||||
*/
|
||||
readonly message: string | null
|
||||
setMessage: React.Dispatch<React.SetStateAction<string | null>>
|
||||
|
||||
/**
|
||||
* Errors for each property.
|
||||
*
|
||||
* The array will always have at least one element (never empty) in case of errors.
|
||||
*
|
||||
* `undefined` means no errors.
|
||||
*/
|
||||
readonly errors: ErrorsObject<K>
|
||||
}
|
||||
|
||||
export const useForm = <K extends Schema>(
|
||||
validationSchema: K
|
||||
): UseFormResult<typeof validationSchema> => {
|
||||
const validationSchemaObject = useMemo(() => {
|
||||
return Type.Object(validationSchema)
|
||||
}, [validationSchema])
|
||||
|
||||
const [fetchState, setFetchState] = useFetchState()
|
||||
const [message, setMessage] = useState<string | null>(null)
|
||||
const [errors, setErrors] = useState<ErrorsObject<typeof validationSchema>>(
|
||||
{} as any
|
||||
)
|
||||
|
||||
const validate = useMemo(() => {
|
||||
return ajv.compile(validationSchemaObject)
|
||||
}, [validationSchemaObject])
|
||||
|
||||
const handleUseForm: HandleUseForm<typeof validationSchema> = (callback) => {
|
||||
return async (formData, formElement) => {
|
||||
setErrors({} as any)
|
||||
setMessage(null)
|
||||
formData = handleOptionalEmptyStringToNull(
|
||||
formData,
|
||||
validationSchemaObject.required
|
||||
)
|
||||
formData = handleCheckboxBoolean(formData, validationSchemaObject)
|
||||
const isValid = validate(formData)
|
||||
if (!isValid) {
|
||||
setFetchState('error')
|
||||
const errors: ErrorsObject<typeof validationSchema> = {} as any
|
||||
for (const property in validationSchemaObject.properties) {
|
||||
const errorsForProperty = validate.errors?.filter((error) => {
|
||||
return error.instancePath === `/${property}`
|
||||
})
|
||||
errors[property as keyof typeof validationSchema] =
|
||||
errorsForProperty != null && errorsForProperty.length > 0
|
||||
? errorsForProperty
|
||||
: undefined
|
||||
}
|
||||
setErrors(errors)
|
||||
} else {
|
||||
setErrors({} as any)
|
||||
if (callback != null) {
|
||||
setFetchState('loading')
|
||||
const message = await callback(
|
||||
formData as Static<TObject<typeof validationSchema>>,
|
||||
formElement
|
||||
)
|
||||
if (message != null) {
|
||||
const { value = null, type, properties } = message
|
||||
setMessage(value)
|
||||
setFetchState(type)
|
||||
if (type === 'error') {
|
||||
const propertiesErrors: ErrorsObject<typeof validationSchema> =
|
||||
{} as any
|
||||
for (const property in properties) {
|
||||
propertiesErrors[property] = [
|
||||
{
|
||||
keyword: 'message',
|
||||
message: properties[property],
|
||||
instancePath: `/${property}`,
|
||||
schemaPath: `#/properties/${property}/message`,
|
||||
params: {},
|
||||
data: formData[property]
|
||||
}
|
||||
]
|
||||
}
|
||||
setErrors(propertiesErrors)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
handleUseForm,
|
||||
fetchState,
|
||||
setFetchState,
|
||||
message,
|
||||
setMessage,
|
||||
errors
|
||||
}
|
||||
}
|
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export * from './components/Form'
|
||||
export * from './hooks/useFetchState'
|
||||
export * from './hooks/useForm'
|
||||
export * from './utils/ajv'
|
18
src/typings.d.ts
vendored
18
src/typings.d.ts
vendored
@ -1,18 +0,0 @@
|
||||
/**
|
||||
* Default CSS definition for typescript,
|
||||
* will be overridden with file-specific definitions by rollup
|
||||
*/
|
||||
declare module '*.css' {
|
||||
const content: { [className: string]: string }
|
||||
export default content
|
||||
}
|
||||
|
||||
interface SvgrComponent
|
||||
extends React.StatelessComponent<React.SVGAttributes<SVGElement>> {}
|
||||
|
||||
declare module '*.svg' {
|
||||
const svgUrl: string
|
||||
const svgComponent: SvgrComponent
|
||||
export default svgUrl
|
||||
export { svgComponent as ReactComponent }
|
||||
}
|
25
src/utils/ajv.ts
Normal file
25
src/utils/ajv.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import addFormats from 'ajv-formats'
|
||||
import Ajv from 'ajv'
|
||||
|
||||
export const ajv = addFormats(
|
||||
new Ajv({
|
||||
allErrors: true,
|
||||
verbose: true
|
||||
}),
|
||||
[
|
||||
'date-time',
|
||||
'time',
|
||||
'date',
|
||||
'email',
|
||||
'hostname',
|
||||
'ipv4',
|
||||
'ipv6',
|
||||
'uri',
|
||||
'uri-reference',
|
||||
'uuid',
|
||||
'uri-template',
|
||||
'json-pointer',
|
||||
'relative-json-pointer',
|
||||
'regex'
|
||||
]
|
||||
)
|
25
src/utils/handleCheckboxBoolean.ts
Normal file
25
src/utils/handleCheckboxBoolean.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import type { TObject } from '@sinclair/typebox'
|
||||
|
||||
import type { ObjectAny } from './types'
|
||||
|
||||
export const handleCheckboxBoolean = (
|
||||
object: ObjectAny,
|
||||
validateSchemaObject: TObject<ObjectAny>
|
||||
): ObjectAny => {
|
||||
const booleanProperties: string[] = []
|
||||
for (const property in validateSchemaObject.properties) {
|
||||
const rule = validateSchemaObject.properties[property]
|
||||
if (rule.type === 'boolean') {
|
||||
booleanProperties.push(property)
|
||||
}
|
||||
}
|
||||
for (const booleanProperty of booleanProperties) {
|
||||
if (object[booleanProperty] == null) {
|
||||
object[booleanProperty] =
|
||||
validateSchemaObject.properties[booleanProperty].default
|
||||
} else {
|
||||
object[booleanProperty] = object[booleanProperty] === 'on'
|
||||
}
|
||||
}
|
||||
return object
|
||||
}
|
17
src/utils/handleOptionalEmptyStringToNull.ts
Normal file
17
src/utils/handleOptionalEmptyStringToNull.ts
Normal file
@ -0,0 +1,17 @@
|
||||
export const handleOptionalEmptyStringToNull = <K>(
|
||||
object: K,
|
||||
required: string[] = []
|
||||
): K => {
|
||||
return Object.fromEntries(
|
||||
Object.entries(object).map(([key, value]) => {
|
||||
if (
|
||||
typeof value === 'string' &&
|
||||
value.length === 0 &&
|
||||
!required.includes(key)
|
||||
) {
|
||||
return [key, null]
|
||||
}
|
||||
return [key, value]
|
||||
})
|
||||
) as K
|
||||
}
|
3
src/utils/types.ts
Normal file
3
src/utils/types.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export interface ObjectAny {
|
||||
[key: string]: any
|
||||
}
|
@ -1,24 +1,22 @@
|
||||
{
|
||||
"include": ["src", "types"],
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"module": "esnext",
|
||||
"target": "ES3",
|
||||
"lib": ["dom", "esnext", "DOM.Iterable"],
|
||||
"moduleResolution": "node",
|
||||
"jsx": "react",
|
||||
"sourceMap": true,
|
||||
"module": "ESNext",
|
||||
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
||||
"importHelpers": false,
|
||||
"declaration": true,
|
||||
"esModuleInterop": true,
|
||||
"sourceMap": true,
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"noImplicitReturns": true,
|
||||
"noImplicitThis": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"suppressImplicitAnyIndexErrors": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"downlevelIteration": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "dist", "example"]
|
||||
"moduleResolution": "node",
|
||||
"jsx": "react",
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noEmit": true
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs"
|
||||
}
|
||||
}
|
12
tsup.config.js
Normal file
12
tsup.config.js
Normal file
@ -0,0 +1,12 @@
|
||||
import { defineConfig } from 'tsup'
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
platform: 'browser',
|
||||
target: 'esnext',
|
||||
format: ['esm'],
|
||||
minify: true,
|
||||
dts: true
|
||||
})
|
Reference in New Issue
Block a user