From a06f274938998db53c78214f1f0d0791adf4ee57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8F=B5=E4=B9=8B?= Date: Wed, 21 Feb 2024 17:54:42 +0800 Subject: [PATCH] chore: improve by codereview --- cypress/fixtures/example.json | 5 ----- cypress/support/commands.ts | 37 --------------------------------- cypress/support/component.ts | 3 ++- package-lock.json | 39 ++++++++--------------------------- package.json | 2 +- src/index.ts | 16 +++++++------- src/types.ts | 2 +- 7 files changed, 21 insertions(+), 83 deletions(-) delete mode 100644 cypress/fixtures/example.json delete mode 100644 cypress/support/commands.ts diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json deleted file mode 100644 index 519902d..0000000 --- a/cypress/fixtures/example.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "Using fixtures to represent data", - "email": "hello@cypress.io", - "body": "Fixtures are a great way to mock data for responses to routes" -} diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts deleted file mode 100644 index 95857ae..0000000 --- a/cypress/support/commands.ts +++ /dev/null @@ -1,37 +0,0 @@ -/// -// *********************************************** -// This example commands.ts shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add('login', (email, password) => { ... }) -// -// -// -- This is a child command -- -// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) -// -// -// -- This is a dual command -- -// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) -// -// -// -- This will overwrite an existing command -- -// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) -// -// declare global { -// namespace Cypress { -// interface Chainable { -// login(email: string, password: string): Chainable -// drag(subject: string, options?: Partial): Chainable -// dismiss(subject: string, options?: Partial): Chainable -// visit(originalFn: CommandOriginalFn, url: string, options: Partial): Chainable -// } -// } -// } diff --git a/cypress/support/component.ts b/cypress/support/component.ts index 5256b77..e43616c 100644 --- a/cypress/support/component.ts +++ b/cypress/support/component.ts @@ -1,5 +1,6 @@ +/// + import { mount } from 'cypress/react'; -import './commands'; declare global { // eslint-disable-next-line @typescript-eslint/no-namespace diff --git a/package-lock.json b/package-lock.json index 8c9c49b..d7dd117 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@alifd/field", - "version": "1.7.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@alifd/field", - "version": "1.7.0", + "version": "2.0.0", "license": "MIT", "dependencies": { - "@alifd/validate": "^2.0.1", + "@alifd/validate": "^2.0.2", "tslib": "^2.6.2" }, "devDependencies": { @@ -18,7 +18,6 @@ "@alifd/next": "^1.15.12", "@commitlint/cli": "^8.1.0", "@types/chai": "^4.3.11", - "@types/jest": "^26.0.24", "@types/react": "^16.14.56", "@types/react-dom": "^16.9.24", "@typescript-eslint/eslint-plugin": "^6.13.2", @@ -171,9 +170,9 @@ } }, "node_modules/@alifd/validate": { - "version": "2.0.1", - "resolved": "https://registry.npmmirror.com/@alifd/validate/-/validate-2.0.1.tgz", - "integrity": "sha512-culB7zSfwk7IwwxhjrxDVicJV/fHbrDwiTbcyQa+2MXcBua0uO3OAQoHsCxmO4yW5er4kYKfk/ddgY9DWGT73w==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@alifd/validate/-/validate-2.0.2.tgz", + "integrity": "sha512-KAVPpcryKJxyS5CsmnZoxvtkLgOCrUE8gD6sSBvmdPARSrgNiCk4x8fr40UJAce6LI8XvTqmZR7BuyL7xZOdOw==", "dependencies": { "tslib": "^2.6.2" } @@ -6170,16 +6169,6 @@ "@types/istanbul-lib-report": "*" } }, - "node_modules/@types/jest": { - "version": "26.0.24", - "resolved": "https://registry.npmmirror.com/@types/jest/-/jest-26.0.24.tgz", - "integrity": "sha512-E/X5Vib8BWqZNRlDxj9vYXhsDwPYbPINqKF9BsnSoon4RQ0D9moEuLD8txgyypFLH7J4+Lho9Nr/c8H0Fi+17w==", - "dev": true, - "dependencies": { - "jest-diff": "^26.0.0", - "pretty-format": "^26.0.0" - } - }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmmirror.com/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -40754,9 +40743,9 @@ } }, "@alifd/validate": { - "version": "2.0.1", - "resolved": "https://registry.npmmirror.com/@alifd/validate/-/validate-2.0.1.tgz", - "integrity": "sha512-culB7zSfwk7IwwxhjrxDVicJV/fHbrDwiTbcyQa+2MXcBua0uO3OAQoHsCxmO4yW5er4kYKfk/ddgY9DWGT73w==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@alifd/validate/-/validate-2.0.2.tgz", + "integrity": "sha512-KAVPpcryKJxyS5CsmnZoxvtkLgOCrUE8gD6sSBvmdPARSrgNiCk4x8fr40UJAce6LI8XvTqmZR7BuyL7xZOdOw==", "requires": { "tslib": "^2.6.2" } @@ -45232,16 +45221,6 @@ "@types/istanbul-lib-report": "*" } }, - "@types/jest": { - "version": "26.0.24", - "resolved": "https://registry.npmmirror.com/@types/jest/-/jest-26.0.24.tgz", - "integrity": "sha512-E/X5Vib8BWqZNRlDxj9vYXhsDwPYbPINqKF9BsnSoon4RQ0D9moEuLD8txgyypFLH7J4+Lho9Nr/c8H0Fi+17w==", - "dev": true, - "requires": { - "jest-diff": "^26.0.0", - "pretty-format": "^26.0.0" - } - }, "@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmmirror.com/@types/json-schema/-/json-schema-7.0.15.tgz", diff --git a/package.json b/package.json index 96b1764..f3f24a9 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "component" ], "dependencies": { - "@alifd/validate": "^2.0.1", + "@alifd/validate": "^2.0.2", "tslib": "^2.6.2" }, "devDependencies": { diff --git a/src/index.ts b/src/index.ts index 5d004c4..6e7d1e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ -import { Component, MutableRefObject } from 'react'; -import Validate from '@alifd/validate'; -import { NormalizedValidateError } from '@alifd/validate/lib/types'; +import type { Component, MutableRefObject } from 'react'; +import Validate, { type NormalizedValidateError } from '@alifd/validate'; import { getValueFromEvent, getErrorStrs, @@ -14,7 +13,7 @@ import { cloneToRuleArr, isOverwritten, } from './utils'; -import { +import type { FieldOption, ComponentInstance, GetUseFieldOption, @@ -59,7 +58,6 @@ class Field { }; } - private com: ComponentInstance; private fieldsMeta: Record; private cachedBind: Record>; private instance: Record; @@ -69,6 +67,7 @@ class Field { private values: FieldValues; private processErrorMessage?: FieldOption['processErrorMessage']; private afterValidateRerender?: FieldOption['afterValidateRerender']; + com: ComponentInstance; options: NormalizedFieldOption; constructor(com: ComponentInstance, options: FieldOption = {}) { @@ -259,8 +258,8 @@ class Field { id: id || name, ref: this._getCacheBind(name, `${name}__ref`, this._saveRef), [valueName]: setValueFormatter - ? setValueFormatter(field.value as any, field.inputValues) - : (field.value as any), + ? setValueFormatter(field.value as ValueType, field.inputValues) + : field.value, }; let rulesMap: Record[]> = {}; @@ -1095,7 +1094,8 @@ class Field { cache[action] = hander; } - private _getCache(name: string, action: string): R | undefined { + // FIXME 有一些地方用到了这些内部函数 (deep),先不声明为 private + _getCache(name: string, action: string): R | undefined { const cache = this.cachedBind[name] || {}; return cache[action] as R | undefined; } diff --git a/src/types.ts b/src/types.ts index a1b7b33..4cc2b3d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import type { MessagesConfig, PresetFormatter, Validator } from '@alifd/validate/lib/types'; +import type { MessagesConfig, PresetFormatter, Validator } from '@alifd/validate'; import type { Component, useState, useMemo, Dispatch, SetStateAction, Ref, RefCallback } from 'react'; export { Validator };