An open API service indexing awesome lists of open source software.

https://github.com/larvenllc/react-cloner

Add/Remove Form Fields or other Components Dynamically
https://github.com/larvenllc/react-cloner

clone cloner npm react react-clone react-cloner reactjs

Last synced: about 2 months ago
JSON representation

Add/Remove Form Fields or other Components Dynamically

Awesome Lists containing this project

README

          

# react-cloner

> Add/Remove Form Fields or other Components Dynamically

[![NPM](https://img.shields.io/npm/v/react-cloner.svg)](https://www.npmjs.com/package/react-cloner) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Demo

![React Cloner Demo](/example/public/react-cloner.gif)

## Install

```bash
npm install --save react-cloner
```

OR

```bash
yarn add react-cloner
```

## Usage

```jsx
import Cloner from 'react-cloner'

export default function ContactPeopleForm() {
function handleSubmit(event) {
event.preventDefault()
let form = {},
formData = new FormData(event.target)
for (let pair of formData.entries()) {
let key = pair[0],
value = pair[1]
form[key] = value
}
alert(JSON.stringify(form, 0, 2))
}
return (



}
componentWrapperClassName='d-flex w-100 align-items-end justify-content-between mb-3'
/>

Submit



)
}

// increment prop is passed onto the Cloner component by default
// it counts the number of the cloned component from 1 onwards
function ContactPerson({ increment }) {
return (



Name



Email



Phone



)
}
```

## Props

| Prop | Explanation | Data Type | (Sample) Values | Default |
| ---------------------------- | :----------------------------------------------------------: | :-------: | :--------------------------------------------------------------------------------------------------------------------------------: | :-----: |
| title | semantic name of component to be cloned | String | `Contact Person`
`Referral`
| `null` |
| initialItems | number of times component specified to appear initially | Number | `5`
`2` | `1` |
| component | react component to be cloned | React | `const Example = () => { return

}`
`class Example extends Component { render(){ return(
) }} => { return
}` | `null` |
| onDelete | callback on each cloned component delete | Func | `(index) => console.log("deleted %s", index)` | `null` |
| shouldHideLabel | hide cloned component label? | Bool | `true`
`false` | `true` |
| addButtonPosition | horizontal alignment of add button | String | `left`
`right` | `right` |
| componentWrapperStyle | Cloner parent wrapper style | Object | `{}`
`{background: "red"}` | `null` |
| componentWrapperClassName | Cloner parent wrapper class | String | `margin-auto`
`py-3` | `null` |
| labelClassName | cloned component label class | String | `text-muted` | `null` |
| addButtonClassName | add (component) button class | String | `btn`
`button-primary` | `""` |
| deleteButtonClassName | cloned component delete button class | String | `btn-danger`
`b-red` | `null` |
| deleteButtonWrapperClassName | cloned component delete button parent wrapper class | String | `d-inline`
`py-3` | `null` |
| shouldRemoveDeleteWrapper | whether to remove the wrapper component around delete button | Bool | `true`
`false` | `false` |

## License

MIT © [BossBele](https://github.com/BossBele)