Skip to content

Commit

Permalink
feat: add silent option (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnIsOnTheRoad authored and janryWang committed Nov 7, 2019
1 parent cf3787b commit 4377180
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
43 changes: 43 additions & 0 deletions packages/core/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,31 @@ describe('setFormState', () => {
props: { hello: 'world' }
})
})

test('set with slient', async () => {
const fieldChange = jest.fn()
const formChange = jest.fn()
const form = createForm({
lifecycles: [
new FormLifeCycle(LifeCycleTypes.ON_FIELD_CHANGE, fieldChange),
new FormLifeCycle(LifeCycleTypes.ON_FORM_CHANGE, formChange)
]
})

form.registerField({ path: 'a' })

form.setFormState(state => {
state.values = { a: '1234' }
})
expect(form.getFormState((state) => state.values)).toEqual({ a: '1234' })
expect(fieldChange).toBeCalledTimes(2)
expect(formChange).toBeCalledTimes(2)

form.setFormState((state) => state.values = { a: '5678' }, true)
expect(form.getFormState((state) => state.values)).toEqual({ a: '5678' })
expect(formChange).toBeCalledTimes(2)
expect(fieldChange).toBeCalledTimes(2)
})
})

describe('getFormState', () => {
Expand Down Expand Up @@ -621,6 +646,24 @@ describe('setFieldState', () => {
expect(form.getFieldState('a')).toEqual(state)
})

test('set with slient', async () => {
const fieldChange = jest.fn()
const form = createForm({
lifecycles: [
new FormLifeCycle(LifeCycleTypes.ON_FIELD_CHANGE, fieldChange),
]
})
form.registerField({ path: 'a' })
form.getFieldState('a')
form.setFieldState('a', state => (state.value = '1234'))
expect(form.getFieldState('a', state => state.value)).toEqual('1234')
expect(fieldChange).toBeCalledTimes(2)

form.setFieldState('a', state => (state.value = '5678'), true)
expect(form.getFieldState('a', state => state.value)).toEqual('5678')
expect(fieldChange).toBeCalledTimes(2)
})

test('validating and loading', () => {
const form = createForm()
form.registerField({
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,9 @@ export function createForm<FieldProps, VirtualFieldProps>(
})
}

function setFormState(callback?: (state: IFormState) => any) {
function setFormState(callback?: (state: IFormState) => any, silent?: boolean) {
leadingUpdate(() => {
state.setState(callback)
state.setState(callback, silent)
})
}

Expand Down Expand Up @@ -800,13 +800,14 @@ export function createForm<FieldProps, VirtualFieldProps>(

function setFieldState(
path: FormPathPattern,
callback?: (state: IFieldState<FieldProps>) => void
callback?: (state: IFieldState<FieldProps>) => void,
silent?: boolean
) {
if (!isFn(callback)) return
let matchCount = 0
let pattern = FormPath.getPath(path)
graph.select(pattern, field => {
field.setState(callback)
field.setState(callback, silent)
matchCount++
})
if (matchCount === 0 || pattern.isWildMatchPattern) {
Expand All @@ -828,10 +829,10 @@ export function createForm<FieldProps, VirtualFieldProps>(
}
}

function setFieldValue(path: FormPathPattern, value?: any) {
function setFieldValue(path: FormPathPattern, value?: any, silent?: boolean) {
setFieldState(path, state => {
state.value = value
})
}, silent)
}

function getFieldValue(path?: FormPathPattern) {
Expand All @@ -840,10 +841,10 @@ export function createForm<FieldProps, VirtualFieldProps>(
})
}

function setFieldInitialValue(path?: FormPathPattern, value?: any) {
function setFieldInitialValue(path?: FormPathPattern, value?: any, silent?: boolean) {
setFieldState(path, state => {
state.initialValue = value
})
}, silent)
}

function getFieldInitialValue(path?: FormPathPattern) {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,12 @@ export interface IForm {
clearErrors: (pattern?: FormPathPattern) => void
reset(options?: IFormResetOptions): Promise<void | IFormValidateResult>
validate(path?: FormPathPattern, options?: {}): Promise<IFormValidateResult>
setFormState(callback?: (state: IFormState) => any): void
setFormState(callback?: (state: IFormState) => any, silent?: boolean): void
getFormState(callback?: (state: IFormState) => any): any
setFieldState(
path: FormPathPattern,
callback?: (state: IFieldState) => void
callback?: (state: IFieldState) => void,
silent?: boolean
): void
getFieldState(
path: FormPathPattern,
Expand Down

0 comments on commit 4377180

Please sign in to comment.