UNPKG

@bigfishtv/cockpit

Version:

29 lines (24 loc) 789 B
import PropTypes from 'prop-types' import React, { Component } from 'react' import { withFormValue } from '@bigfishtv/react-forms' import Button from '../button/Button' import newId from '../../utils/newId' @withFormValue export default class AppendButton extends Component { static propTypes = { item: PropTypes.func, onAdd: PropTypes.func, } addItem = () => { const Items = (this.props.formValue.value || []).slice() const item = this.props.item ? this.props.item() : {} item.id = newId() Items.push(item) const formValue = this.props.formValue.update(Items) this.props.onAdd && this.props.onAdd(formValue.select(Items.length - 1)) } render() { const { formValue, item, onAdd, ...props } = this.props return <Button {...props} onClick={this.addItem} /> } }