Skip to content

Commit fa53832

Browse files
committed
chore(core): improve code
1 parent fb217f2 commit fa53832

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

packages/core/src/models/Form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Form<ValueType extends object = any> {
7878
graph: Graph
7979
fields: IFormFields = {}
8080
requests: IFormRequests = {}
81-
indexes: Map<string, string> = new Map()
81+
indexes: Record<string, string> = {}
8282
disposers: (() => void)[] = []
8383

8484
constructor(props: IFormProps<ValueType>) {
@@ -566,7 +566,7 @@ export class Form<ValueType extends object = any> {
566566
this.query('*').forEach((field) => field.destroy())
567567
this.disposers.forEach((dispose) => dispose())
568568
this.unmounted = true
569-
this.indexes.clear()
569+
this.indexes = {}
570570
this.heart.clear()
571571
if (globalThisPolyfill[DEV_TOOLS_HOOK] && !this.props.designable) {
572572
globalThisPolyfill[DEV_TOOLS_HOOK].unmount(this.id)

packages/core/src/models/Query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export class Query {
2222
this.form = props.form
2323
if (!this.pattern.isMatchPattern) {
2424
const identifier = this.pattern.toString()
25-
const index = this.form.indexes.get(identifier)
25+
const indexIdentifier = this.form.indexes[identifier]
2626
const absoluteField = this.form.fields[identifier]
27-
const indexField = this.form.fields[index]
27+
const indexField = this.form.fields[indexIdentifier]
2828
if (absoluteField) {
2929
this.addresses = [identifier]
3030
} else if (indexField) {
31-
this.addresses = [index]
31+
this.addresses = [indexIdentifier]
3232
}
3333
} else {
3434
each(this.form.fields, (field, address) => {

packages/core/src/shared/internals.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ import {
5555
ReadOnlyProperties,
5656
} from './constants'
5757

58+
const hasOwnProperty = Object.prototype.hasOwnProperty
59+
60+
const notify = (
61+
target: Form | Field,
62+
formType: LifeCycleTypes,
63+
fieldType: LifeCycleTypes
64+
) => {
65+
if (isForm(target)) {
66+
target.notify(formType)
67+
} else {
68+
target.notify(fieldType)
69+
}
70+
}
71+
5872
export const isHTMLInputEvent = (event: any, stopPropagation = true) => {
5973
if (event?.target) {
6074
if (isValid(event.target.value) || isValid(event.target.checked))
@@ -86,23 +100,25 @@ export const getValuesFromEvent = (args: any[]) => {
86100

87101
export const buildFieldPath = (field: GeneralField) => {
88102
let prevArray = false
89-
return field.address.reduce((path: FormPath, key: string, index: number) => {
90-
const currentPath = path.concat([key])
91-
const currentAddress = field.address.slice(0, index + 1)
92-
const current = field.form.fields[currentAddress.toString()]
103+
const fields = field.form.fields
104+
const segments = field.address.segments
105+
const path = segments.reduce((path: string[], key: string, index: number) => {
106+
const currentPath = path.concat(key)
107+
const currentAddress = segments.slice(0, index + 1)
108+
const current = fields[currentAddress.join('.')]
93109
if (prevArray) {
94110
prevArray = false
95111
return path
96112
}
97-
if (index >= field.address.length - 1) {
113+
if (index >= segments.length - 1) {
98114
if (isVoidField(field)) {
99115
return currentPath
100116
}
101117
return currentPath
102118
}
103119
if (isVoidField(current)) {
104-
const parentAddress = field.address.slice(0, index)
105-
const parent = field.form.fields[parentAddress.toString()]
120+
const parentAddress = segments.slice(0, index)
121+
const parent = fields[parentAddress.join('.')]
106122
if (isArrayField(parent) && isNumberLike(key)) {
107123
prevArray = true
108124
return currentPath
@@ -112,7 +128,8 @@ export const buildFieldPath = (field: GeneralField) => {
112128
prevArray = false
113129
}
114130
return currentPath
115-
}, new FormPath(''))
131+
}, [])
132+
return new FormPath(path)
116133
}
117134

118135
export const buildNodeIndexes = (
@@ -121,7 +138,7 @@ export const buildNodeIndexes = (
121138
) => {
122139
field.address = FormPath.parse(address)
123140
field.path = buildFieldPath(field)
124-
field.form.indexes.set(field.path.toString(), field.address.toString())
141+
field.form.indexes[field.path.toString()] = field.address.toString()
125142
return field
126143
}
127144

@@ -277,8 +294,6 @@ export const validateToFeedbacks = async (
277294
return results
278295
}
279296

280-
const hasOwnProperty = Object.prototype.hasOwnProperty
281-
282297
export const setValidatorRule = (field: Field, name: string, value: any) => {
283298
if (!isValid(value)) return
284299
const hasRule = parseValidatorDescriptions(field.validator).some(
@@ -666,6 +681,7 @@ export const createBatchStateSetter = (form: Form) => {
666681
}
667682
})
668683
}
684+
669685
export const createBatchStateGetter = (form: Form) => {
670686
return (pattern: FieldMatchPattern, payload?: any) => {
671687
if (isQuery(pattern)) {
@@ -710,18 +726,6 @@ export const triggerFormValuesChange = (form: Form, change: DataChange) => {
710726
}
711727
}
712728

713-
const notify = (
714-
target: Form | Field,
715-
formType: LifeCycleTypes,
716-
fieldType: LifeCycleTypes
717-
) => {
718-
if (isForm(target)) {
719-
target.notify(formType)
720-
} else {
721-
target.notify(fieldType)
722-
}
723-
}
724-
725729
export const setValidating = (target: Form | Field, validating: boolean) => {
726730
clearTimeout(target.requests.validate)
727731
if (validating) {

0 commit comments

Comments
 (0)