Skip to content

Commit

Permalink
refactor(project): remove react-shared-components
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jan 31, 2021
1 parent 0c5947a commit 6f6dbed
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 140 deletions.
1 change: 0 additions & 1 deletion packages/antd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@formily/json-schema": "^2.0.0-beta.3",
"@formily/react": "^2.0.0-beta.3",
"@formily/react-schema-field": "^2.0.0-beta.3",
"@formily/react-shared-components": "^2.0.0-beta.3",
"@formily/shared": "^2.0.0-beta.3",
"classnames": "^2.2.6",
"react-sortable-hoc": "^1.11.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import React, { Fragment } from 'react'
import { IPasswordStrengthProps } from './types'
import { isFn } from '@formily/shared'

const isNum = function(c) {
type ReactRenderPropsChildren<T = any> =
| React.ReactNode
| ((props: T) => React.ReactElement)

interface IPasswordStrengthProps {
value?: React.ReactText
children?: ReactRenderPropsChildren<number>
}

const isNum = function (c) {
return c >= 48 && c <= 57
}
const isLower = function(c) {
const isLower = function (c) {
return c >= 97 && c <= 122
}
const isUpper = function(c) {
const isUpper = function (c) {
return c >= 65 && c <= 90
}
const isSymbol = function(c) {
const isSymbol = function (c) {
return !(isLower(c) || isUpper(c) || isNum(c))
}
const isLetter = function(c) {
const isLetter = function (c) {
return isLower(c) || isUpper(c)
}

const getStrength = val => {
const getStrength = (val) => {
if (!val) return 0
let num = 0
let lower = 0
Expand Down Expand Up @@ -147,7 +155,7 @@ const getStrength = val => {
}
}

export const PasswordStrength: React.FC<IPasswordStrengthProps> = props => {
export const PasswordStrength: React.FC<IPasswordStrengthProps> = (props) => {
if (isFn(props.children)) {
return props.children(getStrength(String(props.value)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/antd/src/password/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { connect, mapReadPretty } from '@formily/react'
import { Input } from 'antd'
import { PasswordProps } from 'antd/lib/input'
import { PasswordStrength } from '@formily/react-shared-components'
import { PasswordStrength } from './PasswordStrength'
import { PreviewText } from '../preview-text'

export interface IPasswordProps extends PasswordProps {
Expand Down
1 change: 0 additions & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@formily/json-schema": "^2.0.0-beta.3",
"@formily/react": "^2.0.0-beta.3",
"@formily/react-schema-field": "^2.0.0-beta.3",
"@formily/react-shared-components": "^2.0.0-beta.3",
"@formily/shared": "^2.0.0-beta.3",
"classnames": "^2.2.6",
"react-sortable-hoc": "^1.11.0",
Expand Down
164 changes: 164 additions & 0 deletions packages/next/src/password/PasswordStrength.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import React, { Fragment } from 'react'
import { isFn } from '@formily/shared'

type ReactRenderPropsChildren<T = any> =
| React.ReactNode
| ((props: T) => React.ReactElement)

interface IPasswordStrengthProps {
value?: React.ReactText
children?: ReactRenderPropsChildren<number>
}

const isNum = function (c) {
return c >= 48 && c <= 57
}
const isLower = function (c) {
return c >= 97 && c <= 122
}
const isUpper = function (c) {
return c >= 65 && c <= 90
}
const isSymbol = function (c) {
return !(isLower(c) || isUpper(c) || isNum(c))
}
const isLetter = function (c) {
return isLower(c) || isUpper(c)
}

const getStrength = (val) => {
if (!val) return 0
let num = 0
let lower = 0
let upper = 0
let symbol = 0
let MNS = 0
let rep = 0
let repC = 0
let consecutive = 0
let sequential = 0
const len = () => num + lower + upper + symbol
const callme = () => {
let re = num > 0 ? 1 : 0
re += lower > 0 ? 1 : 0
re += upper > 0 ? 1 : 0
re += symbol > 0 ? 1 : 0
if (re > 2 && len() >= 8) {
return re + 1
} else {
return 0
}
}
for (let i = 0; i < val.length; i++) {
const c = val.charCodeAt(i)
if (isNum(c)) {
num++
if (i !== 0 && i !== val.length - 1) {
MNS++
}
if (i > 0 && isNum(val.charCodeAt(i - 1))) {
consecutive++
}
} else if (isLower(c)) {
lower++
if (i > 0 && isLower(val.charCodeAt(i - 1))) {
consecutive++
}
} else if (isUpper(c)) {
upper++
if (i > 0 && isUpper(val.charCodeAt(i - 1))) {
consecutive++
}
} else {
symbol++
if (i !== 0 && i !== val.length - 1) {
MNS++
}
}
let exists = false
for (let j = 0; j < val.length; j++) {
if (val[i] === val[j] && i !== j) {
exists = true
repC += Math.abs(val.length / (j - i))
}
}
if (exists) {
rep++
const unique = val.length - rep
repC = unique ? Math.ceil(repC / unique) : Math.ceil(repC)
}
if (i > 1) {
const last1 = val.charCodeAt(i - 1)
const last2 = val.charCodeAt(i - 2)
if (isLetter(c)) {
if (isLetter(last1) && isLetter(last2)) {
const v = val.toLowerCase()
const vi = v.charCodeAt(i)
const vi1 = v.charCodeAt(i - 1)
const vi2 = v.charCodeAt(i - 2)
if (vi - vi1 === vi1 - vi2 && Math.abs(vi - vi1) === 1) {
sequential++
}
}
} else if (isNum(c)) {
if (isNum(last1) && isNum(last2)) {
if (c - last1 === last1 - last2 && Math.abs(c - last1) === 1) {
sequential++
}
}
} else {
if (isSymbol(last1) && isSymbol(last2)) {
if (c - last1 === last1 - last2 && Math.abs(c - last1) === 1) {
sequential++
}
}
}
}
}
let sum = 0
const length = len()
sum += 4 * length
if (lower > 0) {
sum += 2 * (length - lower)
}
if (upper > 0) {
sum += 2 * (length - upper)
}
if (num !== length) {
sum += 4 * num
}
sum += 6 * symbol
sum += 2 * MNS
sum += 2 * callme()
if (length === lower + upper) {
sum -= length
}
if (length === num) {
sum -= num
}
sum -= repC
sum -= 2 * consecutive
sum -= 3 * sequential
sum = sum < 0 ? 0 : sum
sum = sum > 100 ? 100 : sum

if (sum >= 80) {
return 100
} else if (sum >= 60) {
return 80
} else if (sum >= 40) {
return 60
} else if (sum >= 20) {
return 40
} else {
return 20
}
}

export const PasswordStrength: React.FC<IPasswordStrengthProps> = (props) => {
if (isFn(props.children)) {
return props.children(getStrength(String(props.value)))
} else {
return <Fragment>{props.children}</Fragment>
}
}
2 changes: 1 addition & 1 deletion packages/next/src/password/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { connect, mapReadPretty } from '@formily/react'
import { Input } from '@alifd/next'
import { PasswordProps } from '@alifd/next/lib/input'
import { PasswordStrength } from '@formily/react-shared-components'
import { PasswordStrength } from './PasswordStrength'
import { PreviewText } from '../preview-text'

export interface IPasswordProps extends PasswordProps {
Expand Down
10 changes: 0 additions & 10 deletions packages/react-shared-components/.npmignore

This file was deleted.

20 changes: 0 additions & 20 deletions packages/react-shared-components/LICENSE.md

This file was deleted.

3 changes: 0 additions & 3 deletions packages/react-shared-components/README.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/react-shared-components/jest.config.js

This file was deleted.

37 changes: 0 additions & 37 deletions packages/react-shared-components/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions packages/react-shared-components/rollup.config.js

This file was deleted.

18 changes: 0 additions & 18 deletions packages/react-shared-components/src/DragListView.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions packages/react-shared-components/src/index.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/react-shared-components/src/types.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/react-shared-components/tsconfig.json

This file was deleted.

8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15313,14 +15313,6 @@ react-dom@^17.0.0, react-dom@^17.0.1:
object-assign "^4.1.1"
scheduler "^0.20.1"

react-drag-listview@^0.1.6:
version "0.1.8"
resolved "https://registry.yarnpkg.com/react-drag-listview/-/react-drag-listview-0.1.8.tgz#e04de326ecbe97d6ad84fb68664e405765e30d72"
integrity sha512-ZJnjFEz89RPZ1DzI8f6LngmtsmJbLry/pMz2tEqABxHA+d8cUFRmVPS1DxZdoz/htc+uri9fCdv4dqIiPz0xIA==
dependencies:
babel-runtime "^6.26.0"
prop-types "^15.5.8"

react-error-overlay@6.0.8:
version "6.0.8"
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.8.tgz#474ed11d04fc6bda3af643447d85e9127ed6b5de"
Expand Down

0 comments on commit 6f6dbed

Please sign in to comment.