Skip to content

Commit 4e26765

Browse files
committed
feat: Ability to pass a new card template to be rendered when adding new cards
#48
1 parent e6cc111 commit 4e26765

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ This is the container component that encapsulates the lanes and cards
8080
| onCardClick | function | Called when a card is clicked: `onCardClick(cardId, metadata, laneId) ` |
8181
| onCardAdd | function | Called when a new card is added: `onCardAdd(card, laneId) ` |
8282
| addCardLink | node | Pass custom element to replace the `Add Card` link at the end of the lane (when board is editable) |
83+
| newCardTemplate | node | Pass a custom new card template to add new cards to a lane (when board is editable) |
8384
| onCardDelete | function | Called when a card is deleted: `onCardDelete(cardId, laneId) ` |
8485
| onLaneClick | function | Called when a lane is clicked: `onLaneClick(laneId) `. Card clicks are not propagated to lane click event |
8586
| laneSortFunction | function | Used to specify the logic to sort cards on a lane: `laneSortFunction(card1, card2)` |

src/components/BoardContainer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class BoardContainer extends Component {
7171
'handleDragStart',
7272
'handleDragEnd',
7373
'customCardLayout',
74+
'newCardTemplate',
7475
'customLaneHeader',
7576
'tagStyle',
7677
'children'
@@ -106,6 +107,7 @@ BoardContainer.propTypes = {
106107
handleDragStart: PropTypes.func,
107108
handleDragEnd: PropTypes.func,
108109
customCardLayout: PropTypes.bool,
110+
newCardTemplate: PropTypes.node,
109111
customLaneHeader: PropTypes.element,
110112
style: PropTypes.object,
111113
tagStyle: PropTypes.object

src/components/Card.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Card extends Component {
2323
if (this.props.customCardLayout) {
2424
const {customCard, ...otherProps} = this.props
2525
const customCardWithProps = React.cloneElement(customCard, {...otherProps})
26-
return <span>{customCardWithProps}</span>
26+
return customCardWithProps
2727
} else {
2828
const {title, description, label, tags} = this.props
2929
return (

src/components/Lane.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,6 @@ class Lane extends Component {
7676
)
7777
}
7878

79-
sameCards = (cardsA, cardsB) => {
80-
return (
81-
cardsA.length === cardsB.length &&
82-
cardsA.every(
83-
(el, ix) =>
84-
el.id === cardsB[ix].id &&
85-
el.title === cardsB[ix].title &&
86-
el.label === cardsB[ix].label &&
87-
el.description === cardsB[ix].description
88-
)
89-
)
90-
}
91-
9279
componentWillReceiveProps (nextProps) {
9380
if (!isEqual(this.props.cards, nextProps.cards)) {
9481
this.setState({cards: nextProps.cards, currentPage: nextProps.currentPage})
@@ -142,6 +129,15 @@ class Lane extends Component {
142129
}
143130
}
144131

132+
renderNewCard = () => {
133+
const {newCardTemplate} = this.props
134+
if (newCardTemplate) {
135+
const newCardWithProps = React.cloneElement(newCardTemplate, {onCancel: this.hideEditableCard, onAdd: this.addNewCard})
136+
return <span>{newCardWithProps}</span>
137+
} else {
138+
return <NewCard onCancel={this.hideEditableCard} onAdd={this.addNewCard} />
139+
}
140+
}
145141

146142
renderDragContainer = () => {
147143
const {connectDropTarget, laneSortFunction, editable, tagStyle, cardStyle, draggable} = this.props
@@ -176,7 +172,7 @@ class Lane extends Component {
176172
<div>
177173
<DraggableList>{cardList}</DraggableList>
178174
{editable && !addCardMode && this.renderAddCardLink()}
179-
{addCardMode && <NewCard onCancel={this.hideEditableCard} onAdd={this.addNewCard} />}
175+
{addCardMode && this.renderNewCard()}
180176
</div>
181177
)
182178
}
@@ -229,6 +225,7 @@ Lane.propTypes = {
229225
onCardClick: PropTypes.func,
230226
onCardDelete: PropTypes.func,
231227
onCardAdd: PropTypes.func,
228+
newCardTemplate: PropTypes.node,
232229
addCardLink: PropTypes.node,
233230
editable: PropTypes.bool
234231
}

stories/EditableBoard.story.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
1-
import React from 'react'
1+
import React, {Component} from 'react'
22
import {withInfo} from '@storybook/addon-info'
33
import {storiesOf} from '@storybook/react'
44

55
import Board from '../src'
66

77
const data = require('./data.json')
88

9+
class NewCard extends Component {
10+
updateField = (field, evt) => {
11+
this.setState({[field]: evt.target.value})
12+
}
13+
14+
handleAdd = () => {
15+
this.props.onAdd(this.state)
16+
}
17+
18+
render () {
19+
const {onCancel} = this.props
20+
return (
21+
<div style={{background: 'white', borderRadius: 3, border: '1px solid #eee', borderBottom: '1px solid #ccc'}}>
22+
<div style={{padding: 5, margin: 5}}>
23+
<div>
24+
<div style={{marginBottom: 5}}>
25+
<input type='text' onChange={evt => this.updateField('title', evt)} placeholder='Title' />
26+
</div>
27+
<div style={{marginBottom: 5}}>
28+
<input type='text' onChange={evt => this.updateField('description', evt)} placeholder='Description'/>
29+
</div>
30+
</div>
31+
<button onClick={this.handleAdd}>Add</button>
32+
<button onClick={onCancel}>Cancel</button>
33+
</div>
34+
</div>
35+
)
36+
}
37+
}
38+
939
storiesOf('Editable Board', module)
1040
.add(
1141
'Add/Delete Cards',
@@ -28,7 +58,7 @@ storiesOf('Editable Board', module)
2858
<Board
2959
data={data}
3060
draggable
31-
id='EditableBoard1'
61+
id='EditableBoard1'
3262
onDataChange={shouldReceiveNewData}
3363
onCardDelete={handleCardDelete}
3464
onCardAdd={handleCardAdd}
@@ -39,14 +69,14 @@ storiesOf('Editable Board', module)
3969
})
4070
)
4171
.add(
42-
'Customizations',
72+
'Custom Buttons',
4373
withInfo('Allow editable elements on the board to be customized')(() => {
44-
return (
45-
<Board
46-
data={data}
47-
editable
48-
addCardLink={<button>New Card</button>}
49-
/>
50-
)
74+
return <Board data={data} editable addCardLink={<button>New Card</button>} />
75+
})
76+
)
77+
.add(
78+
'New Card Template',
79+
withInfo('Pass a custom new card template to add card')(() => {
80+
return <Board data={data} editable newCardTemplate={<NewCard />} />
5181
})
5282
)

0 commit comments

Comments
 (0)