ant-design-vue/components/vc-form/demo/input-array.jsx

70 lines
1.5 KiB
Vue
Raw Normal View History

2018-05-03 11:10:43 +00:00
/* eslint no-console:0 */
2019-01-12 03:33:27 +00:00
import { createForm } from '../index';
import { regionStyle } from './styles';
2018-05-03 11:10:43 +00:00
2019-01-12 03:33:27 +00:00
let uuid = 0;
2018-05-03 11:10:43 +00:00
const Form = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
remove(k) {
const { form } = this;
2018-05-03 11:10:43 +00:00
// can use data-binding to get
2019-01-12 03:33:27 +00:00
let keys = form.getFieldValue('keys');
keys = keys.filter(key => {
return key !== k;
});
2018-05-03 11:10:43 +00:00
// can use data-binding to set
form.setFieldsValue({
keys,
2019-01-12 03:33:27 +00:00
});
2018-05-03 11:10:43 +00:00
},
2019-01-12 03:33:27 +00:00
add() {
uuid++;
const { form } = this;
2018-05-03 11:10:43 +00:00
// can use data-binding to get
2019-01-12 03:33:27 +00:00
let keys = form.getFieldValue('keys');
keys = keys.concat(uuid);
2018-05-03 11:10:43 +00:00
// can use data-binding to set
// important! notify form to detect changes
form.setFieldsValue({
keys,
2019-01-12 03:33:27 +00:00
});
2018-05-03 11:10:43 +00:00
},
2019-01-12 03:33:27 +00:00
submit(e) {
e.preventDefault();
console.log(this.form.getFieldsValue());
2018-05-03 11:10:43 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps, getFieldValue } = this.form;
2018-05-03 11:10:43 +00:00
getFieldProps('keys', {
initialValue: [],
2019-01-12 03:33:27 +00:00
});
const inputs = getFieldValue('keys').map(k => {
return (
<div key={k} style={regionStyle}>
<input {...getFieldProps(`name${k}`)} />
<a onClick={this.remove.bind(this, k)}>delete</a>
</div>
);
});
return (
<div>
{inputs}
<div style={regionStyle}>
<button onClick={this.submit}>submit</button>
2018-05-03 11:10:43 +00:00
2019-01-12 03:33:27 +00:00
<button onClick={this.add}>add</button>
</div>
2018-05-03 11:10:43 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-03 11:10:43 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-03 11:10:43 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);