Skip to content

Commit

Permalink
chore(core): improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Nov 3, 2021
1 parent fb217f2 commit fa53832
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Form<ValueType extends object = any> {
graph: Graph
fields: IFormFields = {}
requests: IFormRequests = {}
indexes: Map<string, string> = new Map()
indexes: Record<string, string> = {}
disposers: (() => void)[] = []

constructor(props: IFormProps<ValueType>) {
Expand Down Expand Up @@ -566,7 +566,7 @@ export class Form<ValueType extends object = any> {
this.query('*').forEach((field) => field.destroy())
this.disposers.forEach((dispose) => dispose())
this.unmounted = true
this.indexes.clear()
this.indexes = {}
this.heart.clear()
if (globalThisPolyfill[DEV_TOOLS_HOOK] && !this.props.designable) {
globalThisPolyfill[DEV_TOOLS_HOOK].unmount(this.id)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/models/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class Query {
this.form = props.form
if (!this.pattern.isMatchPattern) {
const identifier = this.pattern.toString()
const index = this.form.indexes.get(identifier)
const indexIdentifier = this.form.indexes[identifier]
const absoluteField = this.form.fields[identifier]
const indexField = this.form.fields[index]
const indexField = this.form.fields[indexIdentifier]
if (absoluteField) {
this.addresses = [identifier]
} else if (indexField) {
this.addresses = [index]
this.addresses = [indexIdentifier]
}
} else {
each(this.form.fields, (field, address) => {
Expand Down
50 changes: 27 additions & 23 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ import {
ReadOnlyProperties,
} from './constants'

const hasOwnProperty = Object.prototype.hasOwnProperty

const notify = (
target: Form | Field,
formType: LifeCycleTypes,
fieldType: LifeCycleTypes
) => {
if (isForm(target)) {
target.notify(formType)
} else {
target.notify(fieldType)
}
}

export const isHTMLInputEvent = (event: any, stopPropagation = true) => {
if (event?.target) {
if (isValid(event.target.value) || isValid(event.target.checked))
Expand Down Expand Up @@ -86,23 +100,25 @@ export const getValuesFromEvent = (args: any[]) => {

export const buildFieldPath = (field: GeneralField) => {
let prevArray = false
return field.address.reduce((path: FormPath, key: string, index: number) => {
const currentPath = path.concat([key])
const currentAddress = field.address.slice(0, index + 1)
const current = field.form.fields[currentAddress.toString()]
const fields = field.form.fields
const segments = field.address.segments
const path = segments.reduce((path: string[], key: string, index: number) => {
const currentPath = path.concat(key)
const currentAddress = segments.slice(0, index + 1)
const current = fields[currentAddress.join('.')]
if (prevArray) {
prevArray = false
return path
}
if (index >= field.address.length - 1) {
if (index >= segments.length - 1) {
if (isVoidField(field)) {
return currentPath
}
return currentPath
}
if (isVoidField(current)) {
const parentAddress = field.address.slice(0, index)
const parent = field.form.fields[parentAddress.toString()]
const parentAddress = segments.slice(0, index)
const parent = fields[parentAddress.join('.')]
if (isArrayField(parent) && isNumberLike(key)) {
prevArray = true
return currentPath
Expand All @@ -112,7 +128,8 @@ export const buildFieldPath = (field: GeneralField) => {
prevArray = false
}
return currentPath
}, new FormPath(''))
}, [])
return new FormPath(path)
}

export const buildNodeIndexes = (
Expand All @@ -121,7 +138,7 @@ export const buildNodeIndexes = (
) => {
field.address = FormPath.parse(address)
field.path = buildFieldPath(field)
field.form.indexes.set(field.path.toString(), field.address.toString())
field.form.indexes[field.path.toString()] = field.address.toString()
return field
}

Expand Down Expand Up @@ -277,8 +294,6 @@ export const validateToFeedbacks = async (
return results
}

const hasOwnProperty = Object.prototype.hasOwnProperty

export const setValidatorRule = (field: Field, name: string, value: any) => {
if (!isValid(value)) return
const hasRule = parseValidatorDescriptions(field.validator).some(
Expand Down Expand Up @@ -666,6 +681,7 @@ export const createBatchStateSetter = (form: Form) => {
}
})
}

export const createBatchStateGetter = (form: Form) => {
return (pattern: FieldMatchPattern, payload?: any) => {
if (isQuery(pattern)) {
Expand Down Expand Up @@ -710,18 +726,6 @@ export const triggerFormValuesChange = (form: Form, change: DataChange) => {
}
}

const notify = (
target: Form | Field,
formType: LifeCycleTypes,
fieldType: LifeCycleTypes
) => {
if (isForm(target)) {
target.notify(formType)
} else {
target.notify(fieldType)
}
}

export const setValidating = (target: Form | Field, validating: boolean) => {
clearTimeout(target.requests.validate)
if (validating) {
Expand Down

0 comments on commit fa53832

Please sign in to comment.