Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update stated-bean version #7

Merged
merged 3 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.cache
coverage
dist
lib
node_modules
output
CHANGELOG.md
12 changes: 5 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
const { overrides } = require('eslint-config-1stg/overrides');
const { overrides } = require('@1stg/eslint-config/overrides');

module.exports = {
extends: '1stg',
extends: '@1stg',
overrides: [
...overrides,
{
files: '*.{ts,tsx}',
rules: {
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/member-naming': 0,
'@typescript-eslint/interface-name-prefix': 0,
'@typescript-eslint/no-type-alias': 0,
'standard/no-callback-literal': 0,
'@typescript-eslint/no-floating-promises': 0,
'@typescript-eslint/restrict-plus-operands': 0,
},
},
],
settings: {
polyfills: ['Symbol'],
},
};
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
.history
coverage
dist
lib
node_modules
output
7 changes: 0 additions & 7 deletions .huskyrc

This file was deleted.

1 change: 1 addition & 0 deletions .huskyrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/husky-config')
5 changes: 0 additions & 5 deletions .lintstagedrc

This file was deleted.

1 change: 1 addition & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/lint-staged')
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require('@1stg/prettier-config'),
semi: true,
}
5 changes: 5 additions & 0 deletions .remarkrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"@1stg/remark-config"
]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ deploy:
github_token: $GH_TOKEN
keep_history: true
allow_empty_commit: false
local_dir: output
local_dir: dist
on:
branch: master

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![codechecks.io](https://raw.githubusercontent.com/codechecks/docs/master/images/badges/badge-default.svg?sanitize=true)](https://codechecks.io)

> React Form Binding via [StatedBean](https://github.com/mjolnirjs/stated-bean)

## Install

```sh
Expand Down
1 change: 1 addition & 0 deletions browserslist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends @1stg/browserslist-config
6 changes: 2 additions & 4 deletions codechecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ checks:
- name: build-size-watcher
options:
files:
- path: dist/**/*.*
- path: lib/**/*.*
- name: commit-deployment
options:
buildPath: ./output
- name: type-coverage-watcher
options:
ignoreCatch: true
ignoreFiles:
- dist/**/*
- lib/**/*
strict: true
4 changes: 2 additions & 2 deletions example/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { StatedBeanApplication, StatedBeanProvider } from 'stated-bean';
import { FormValidator } from 'stated-form-bean';

import { UserForm } from './src/UserForm';

import ReactDOM from 'react-dom';
import React from 'react';
import { FormValidateInterceptor } from 'stated-form-bean';

const app = new StatedBeanApplication();

app.setInterceptors(new FormValidateInterceptor());
app.use(FormValidator);

const App = () => {
return (
Expand Down
4 changes: 2 additions & 2 deletions example/src/UserForm.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useStatedBean } from 'stated-bean';
import { useBean } from 'stated-bean';

import { UserModel } from './UserModel';

import * as React from 'react';

export const UserForm = () => {
const model = useStatedBean(UserModel);
const model = useBean(UserModel);

console.log(model);
const { errors } = model.getFormField('user');
Expand Down
4 changes: 2 additions & 2 deletions example/src/UserModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StatedBean, Stated } from 'stated-bean';

import { Valid, FormModel } from 'stated-form-bean';

import * as yup from 'yup';

export interface User {
Expand All @@ -20,7 +20,7 @@ export class UserModel extends FormModel<UserModel> {
.max(99)
.required(),
}),
{ validOnChange: false },
// { validOnChange: false },
)
user: Partial<User> = { age: 15 };

Expand Down
89 changes: 46 additions & 43 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,87 +1,90 @@
{
"name": "stated-form-bean",
"version": "0.1.0-beta.7",
"description": "",
"main": "dist/cjs",
"module": "dist/es",
"unpkg": "dist/umd",
"types": "dist/types/index.d.ts",
"version": "0.2.0-beta",
"description": "React Form Binding via StatedBean",
"repository": "git@github.com/mjolnirjs/stated-form-bean.git",
"license": "MIT",
"main": "lib/cjs.js",
"module": "lib/es",
"unpkg": "lib/umd.min.js",
"es2015": "lib/es2015.js",
"fesm5": "lib/esm.js",
"types": "lib/types/index.d.ts",
"files": [
"dist"
"lib"
],
"scripts": {
"dev": "parcel ./example/index.html --https",
"start": "yarn dev",
"serve": "serve output",
"build:cjs": "tsc -p src/tsconfig.cjs.json",
"build:es": "tsc -p src/tsconfig.es.json",
"build:umd": "cross-env NODE_ENV=development parcel build src/index.ts -d dist/umd --global statedFormBean --no-minify",
"build:umd:min": "parcel build src/index.ts -d dist/umd --global statedFormBean -o index.min.js",
"build:example": "parcel build ./example/index.html -d output --log-level 1",
"build": "rimraf dist output && run-p build:**",
"lint": "cross-env EFF_NO_LINK_RULES=true eslint . --ext .js,.ts,.tsx -f friendly",
"serve": "serve dist",
"build:ts": "tsc -p src",
"build:umd": "rollup -c node_modules/@1stg/rollup-config/module.js",
"build:umd:min": "cross-env NODE_ENV=production yarn build:umd",
"build:example": "parcel build ./example/index.html -d dist --log-level 1",
"build": "rimraf dist lib && run-p build:**",
"lint": "cross-env EFF_NO_LINK_RULES=true eslint . --ext js,md,ts,tsx -f friendly",
"test": "jest --env=jsdom --coverage",
"type-coverage": "type-coverage --ignore-catch --ignore-files 'dist/**/*' --strict"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mjolnirjs/stated-form-bean.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/mjolnirjs/stated-form-bean/issues"
"type-coverage": "type-coverage --detail --ignore-catch --ignore-files 'lib/**/*' --strict"
},
"homepage": "https://github.com/mjolnirjs/stated-form-bean#readme",
"peerDependencies": {
"stated-bean": "^0.1.3-beta",
"stated-bean": "^0.3.0-alpha",
"yup": "^0.27.0"
},
"dependencies": {
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2"
},
"devDependencies": {
"@1stg/babel-preset": "^0.6.0",
"@1stg/browserslist-config": "^0.2.1",
"@1stg/eslint-config": "^0.9.1",
"@1stg/husky-config": "^0.3.0",
"@1stg/lint-staged": "^0.6.0",
"@1stg/prettier-config": "^0.2.0",
"@1stg/remark-config": "^0.2.1",
"@1stg/rollup-config": "^0.7.0",
"@1stg/tsconfig": "^0.5.0",
"@babel/core": "^7.6.0",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@types/enzyme": "^3.10.3",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.17",
"@types/lodash.get": "^4.4.6",
"@types/lodash.set": "^4.3.6",
"@types/node": "^12.7.2",
"@types/node": "^12.7.4",
"@types/react": "^16.9.1",
"@types/react-dom": "^16.8.5",
"@types/react-test-renderer": "^16.9.0",
"@types/yup": "^0.26.23",
"commitlint": "^8.1.0",
"cross-env": "^5.2.0",
"browserslist": "^4.7.0",
"cross-env": "^5.2.1",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"eslint": "^6.1.0",
"eslint-config-1stg": "^5.8.2-beta.1",
"eslint": "^6.3.0",
"eslint-formatter-friendly": "^7.0.0",
"husky": "^3.0.3",
"husky": "^3.0.5",
"jest": "^24.8.0",
"lint-staged": "^9.2.1",
"lint-staged": "^9.2.5",
"npm-run-all": "^4.1.5",
"parcel-bundler": "^1.12.3",
"prettier": "^1.18.2",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-test-renderer": "^16.9.0",
"rimraf": "^3.0.0",
"rollup": "^1.20.3",
"serve": "^11.1.0",
"stated-bean": "^0.3.0-alpha",
"stated-form-bean": "link:src",
"ts-jest": "^24.0.2",
"ts-node": "^8.3.0",
"tslib": "^1.10.0",
"type-coverage": "^2.2.0",
"typescript": "^3.5.3"
},
"dependencies": {
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"stated-bean": "^0.1.3-beta",
"typescript": "^3.6.2",
"yup": "^0.27.0"
},
"alias": {
"stated-form-bean": "./src"
},
"typeCoverage": {
"atLeast": 97
"atLeast": 98
}
}
4 changes: 3 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": ["config:base", ":preserveSemverRanges", "schedule:weekly"]
"extends": [
"@1stg"
]
}
52 changes: 39 additions & 13 deletions src/core/FormModel.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
import { ForceUpdate } from 'stated-bean';

import { getMetadataStorage } from '../metadata';

import { FormField } from './FormField';

import * as yup from 'yup';

export const fields = Symbol('stated_form_bean_fields');

export const _fields = Symbol('stated_form_bean_fields');
export const _clearErrors = Symbol('stated_form_bean_clearErrors');
export const _addError = Symbol('stated_form_bean_addError');

export class FormModel<Values> {
private readonly [fields]: {
private readonly [_fields]: {
[K in keyof Values]: FormField<Values[K]>;
} = {} as { [K in keyof Values]: FormField<Values[K]> };

setFormField<T extends keyof Values>(
field: T,
schema: yup.Schema<Values[T]>,
): void {
const formField = this[fields][field];
const formField = this[_fields][field];
if (formField !== undefined) {
formField.setSchema(schema);
} else {
this[fields][field] = new FormField(field as (string | symbol), schema);
this[_fields][field] = new FormField(field as (string | symbol), schema);
}
}

getFormField<T extends keyof Values>(field: T): FormField<Values[T]> {
if (this[fields][field] === undefined) {
this[fields][field] = new FormField(field as (string | symbol));
if (this[_fields][field] === undefined) {
const validFieldMeta = getMetadataStorage().getField<Values>(
Object.getPrototypeOf(this).constructor,
field,
);
if (validFieldMeta !== undefined) {
this[_fields][field] = new FormField(
field as (string | symbol),
validFieldMeta.schema as yup.Schema<Values[T]>,
);
}
}
return this[fields][field];
return this[_fields][field];
}

validate<T extends keyof Values>(
this: FormModel<Values> & Values,
field: T,
schema?: yup.Schema<Values[T]>,
): Promise<boolean> {
if (this[fields][field] === undefined) {
this[fields][field] = new FormField(field as (string | symbol), schema);
console.log(this[_fields]);
if (this[_fields][field] === undefined) {
let validSchema = schema;
if (schema === undefined) {
console.log(Object.getPrototypeOf(this).constructor, field);
const validFieldMeta = getMetadataStorage().getField<Values>(
Object.getPrototypeOf(this).constructor,
field,
);
validSchema =
validFieldMeta !== undefined
? (validFieldMeta.schema as yup.Schema<Values[T]>)
: undefined;
}
this[_fields][field] = new FormField(
field as (string | symbol),
validSchema,
);
}
const formField = this[fields][field];
const formField = this[_fields][field];

const self = this as FormModel<Values> &
Values & { [ForceUpdate]: (field: T) => void };
Expand All @@ -54,8 +80,8 @@ export class FormModel<Values> {
}

[_clearErrors](field: keyof Values) {
if (this[fields] !== undefined && this[fields][field] !== undefined) {
this[fields][field]._clearErrors();
if (this[_fields] !== undefined && this[_fields][field] !== undefined) {
this[_fields][field]._clearErrors();
}
}

Expand Down