Skip to content

Commit

Permalink
fix: mutator insert (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqiao1 authored Jul 27, 2020
1 parent 71025e6 commit f335632
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/zh-cn/jsx-develop/self-inc-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const App = () => {
return (
<div>
{state.value.map((item, index) => {
const onInsertAfter = index => mutators.insert(index + 1, {})
const onRemove = index => mutators.remove(index)
const onMoveUp = index => mutators.moveUp(index)
const onMoveDown = index => mutators.moveDown(index)
Expand All @@ -32,6 +33,9 @@ const App = () => {
component={Input}
title="年龄"
/>
<Button onClick={onInsertAfter.bind(null, index)}>
insertAfter
</Button>
<Button onClick={onRemove.bind(null, index)}>remove</Button>
<Button onClick={onMoveUp.bind(null, index)}>up</Button>
<Button onClick={onMoveDown.bind(null, index)}>down</Button>
Expand Down Expand Up @@ -87,6 +91,7 @@ const App = () => {
return (
<div>
{state.value.map((item, index) => {
const onInsertAfter = index => mutators.insert(index + 1, {})
const onRemove = index => mutators.remove(index)
const onMoveUp = index => mutators.moveUp(index)
const onMoveDown = index => mutators.moveDown(index)
Expand All @@ -102,6 +107,9 @@ const App = () => {
component={Input}
title="年龄"
/>
<Button onClick={onInsertAfter.bind(null, index)}>
insertAfter
</Button>
<Button onClick={onRemove.bind(null, index)}>remove</Button>
<Button onClick={onMoveUp.bind(null, index)}>up</Button>
<Button onClick={onMoveDown.bind(null, index)}>down</Button>
Expand Down
13 changes: 10 additions & 3 deletions docs/zh-cn/schema-develop/complext-self-inc-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ const App = () => {
selector={[['onFieldValueChange', `userList.${idx}.username`]]}
>
{({ state }) => {
return (state.value === 'morally' ? null : <Button onClick={() => {
mutators.remove(idx)
}}>remove</Button> )
return state.value === 'morally' ? null : (
<Button
onClick={() => {
mutators.remove(idx)
}}
>
remove
</Button>
)
}}
</FormSpy>
)
Expand Down Expand Up @@ -232,6 +238,7 @@ import { SchemaForm, SchemaField, toArr, FormPath } from '@formily/antd'
const ArrayCustom = props => {
const { value, schema, className, editable, path, mutators } = props
const componentProps = schema.getExtendsComponentProps() || {}

const onAdd = () => mutators.push(schema.items.getEmptyValue())
const onRemove = index => mutators.remove(index)
const onMoveUp = index => mutators.moveUp(index)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,11 @@ export const createFormExternals = (
setValue(arr)
return arr
}
if (origin.length === index) {
const arr = origin.concat([value])
setValue(arr)
return arr
}
const arr = origin.reduce((buf, item, idx) => {
return idx === index ? buf.concat([value, item]) : buf.concat(item)
}, [])
Expand Down

0 comments on commit f335632

Please sign in to comment.