Skip to content

Commit

Permalink
fix(core): fix euqal initial empty value will trigger change (#1967)
Browse files Browse the repository at this point in the history
* fix(core): fix euqal initial empty value will trigger change

* fix(core): fix ci
  • Loading branch information
janryWang authored Aug 9, 2021
1 parent 9233f99 commit 4ccf4e4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 40 deletions.
2 changes: 1 addition & 1 deletion designable/antd/playground/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const engine = createDesigner({

const App = () => {
return (
<Designer engine={engine} theme="dark">
<Designer engine={engine}>
<MainPanel logo={<LogoWidget />} actions={<ActionsWidget />}>
<CompositePanel>
<CompositePanel.Item title="panels.Component" icon="Component">
Expand Down
8 changes: 4 additions & 4 deletions designable/antd/playground/webpack.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra'
import { GlobSync } from 'glob'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import autoprefixer from 'autoprefixer'
import { getThemeVariables } from 'antd/dist/theme'
//import { getThemeVariables } from 'antd/dist/theme'

const getWorkspaceAlias = () => {
const basePath = path.resolve(__dirname, '../../../')
Expand Down Expand Up @@ -80,9 +80,9 @@ export default {
{
loader: 'less-loader',
options: {
modifyVars: getThemeVariables({
dark: true, // 开启暗黑模式
}),
// modifyVars: getThemeVariables({
// dark: true, // 开启暗黑模式
// }),
javascriptEnabled: true,
},
},
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,9 @@ test('auto clean with ObjectField', () => {
obj2.removeProperty('cc')
expect(form.fields['obj2.cc']).toBeUndefined()
})

test('initial value with empty', () => {
const form = attach(createForm())
const array = attach(form.createField({ name: 'array', initialValue: '' }))
expect(array.value).toEqual('')
})
10 changes: 10 additions & 0 deletions packages/core/src/__tests__/internals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
matchFeedback,
applyFieldPatches,
setModelState,
isHTMLInputEvent,
} from '../shared/internals'

test('getValuesFromEvent', () => {
Expand Down Expand Up @@ -38,3 +39,12 @@ test('setModelState', () => {
)
).toEqual({})
})

test('isHTMLInputEvent', () => {
expect(isHTMLInputEvent({ target: { checked: true } })).toBeTruthy()
expect(isHTMLInputEvent({ target: { value: 123 } })).toBeTruthy()
expect(isHTMLInputEvent({ target: { tagName: 'INPUT' } })).toBeTruthy()
expect(isHTMLInputEvent({ target: { tagName: 'DIV' } })).toBeFalsy()
expect(isHTMLInputEvent({ target: {}, stopPropagation() {} })).toBeTruthy()
expect(isHTMLInputEvent({})).toBeFalsy()
})
6 changes: 3 additions & 3 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Field<
this.makeIndexes(address)
this.makeObservable(designable)
this.makeReactive(designable)
this.onInit(designable)
this.onInit()
}

protected makeIndexes(address: FormPathPattern) {
Expand Down Expand Up @@ -668,10 +668,10 @@ export class Field<

getState: IModelGetter<IFieldState> = modelStateGetter(this)

onInit = (designable: boolean) => {
onInit = () => {
this.initialized = true
batch.scope(() => {
initFieldValue(this, designable)
initFieldValue(this)
})
batch.scope(() => {
initFieldUpdate(this)
Expand Down
73 changes: 41 additions & 32 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
toArr,
isNumberLike,
shallowClone,
isEqual,
} from '@formily/shared'
import { ValidatorTriggerType, validate } from '@formily/validator'
import { batch, toJS, DataChange } from '@formily/reactive'
Expand All @@ -28,7 +29,6 @@ import {
} from '../types'
import { isArrayField, isGeneralField, isQuery, isVoidField } from './externals'
import { ReservedProperties, GlobalState } from './constants'
import { isObjectField } from './checkers'

export const isHTMLInputEvent = (event: any, stopPropagation = true) => {
if (event?.target) {
Expand Down Expand Up @@ -411,40 +411,49 @@ export const cleanupObjectChildren = (field: ObjectField, keys: string[]) => {
})
}

export const isEmptyWithField = (field: GeneralField, value: any) => {
if (isArrayField(field) || isObjectField(field)) {
return isEmpty(value, true)
}
return !isValid(value)
}

export const initFieldValue = (field: Field, designable: boolean) => {
export const initFieldValue = (field: Field) => {
GlobalState.initializing = true
if (isEmptyWithField(field, field.initialValue)) {
if (isValid(field.props.initialValue)) {
field.initialValue = field.props.initialValue
}
}
if (isEmptyWithField(field, field.value)) {
const isEmptyValue = isEmptyWithField(field, field.props.value)
const isEmptyInitialValue = isEmptyWithField(
field,
field.props.initialValue
)
if (isEmptyValue && !isEmptyInitialValue) {
field.value = shallowClone(field.props.initialValue)
} else if (isValid(field.props.value)) {
field.value = field.props.value
} else if (isValid(field.props.initialValue)) {
field.value = shallowClone(field.props.initialValue)
const fromValue = (value: any) => [
isValid(value),
isEmpty(value, true),
value,
]
const [, isEmptySelfValue, selfValue] = fromValue(field.value)
const [, isEmptySelfInitialValue, selfInitialValue] = fromValue(
field.initialValue
)
const [isValidPropsValue, isEmptyPropsValue, propsValue] = fromValue(
field.props.value
)
const [
isValidPropsInitialValue,
isEmptyPropsInitialValue,
propsInitialValue,
] = fromValue(field.props.initialValue)
if (isEmptySelfInitialValue) {
if (isEmptyPropsInitialValue) {
if (!isEqual(selfInitialValue, propsInitialValue)) {
field.initialValue = shallowClone(propsInitialValue)
}
} else if (isValidPropsInitialValue) {
field.initialValue = shallowClone(propsInitialValue)
}
}
if (designable) {
if (isValid(field.props.initialValue)) {
field.initialValue = shallowClone(field.props.initialValue)
}
if (isValid(field.props.value)) {
field.value = field.props.value
if (isEmptySelfValue) {
if (!isEmptyPropsValue) {
field.value = shallowClone(propsValue)
} else {
if (!isEmptyPropsInitialValue) {
field.value = shallowClone(propsInitialValue)
} else if (isValidPropsValue) {
if (!isEqual(selfValue, propsValue)) {
field.value = shallowClone(propsValue)
}
} else if (isValidPropsInitialValue) {
if (!isEqual(selfValue, propsInitialValue)) {
field.value = shallowClone(propsInitialValue)
}
}
}
}
GlobalState.initializing = false
Expand Down

0 comments on commit 4ccf4e4

Please sign in to comment.