-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0748b13
commit 59d2a74
Showing
6 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Card from 'react-bootstrap/Card'; | ||
import Form from '../Form'; | ||
|
||
const CardSelect = ({ | ||
className, | ||
cardsData, | ||
onChange, | ||
selectedCardName, | ||
}) => { | ||
const [currentRecord, setCurrentRecord] = useState(null); | ||
useEffect(() => { | ||
setCurrentRecord(selectedCardName); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Form.Group> | ||
<Form.RadioSet | ||
name="radio-select-card" | ||
onChange={(e) => { | ||
setCurrentRecord(e.target.value); | ||
onChange(e.target.value); | ||
}} | ||
defaultValue={selectedCardName} | ||
isInline | ||
className="row" | ||
> | ||
{cardsData?.map(data => ( | ||
<div | ||
key={data.name} | ||
className={className} | ||
role="group" | ||
aria-label={data.title} | ||
> | ||
<Card className={currentRecord === data.name ? 'border border-dark' : null}> | ||
<Card.Img variant="top" src={data.img} /> | ||
<Card.Body> | ||
<Card.Title as="h5" className="card-title mb-1"> | ||
<label htmlFor={data.name}>{data.title}</label> | ||
<div className="float-right"><Form.Radio id={data.name} value={data.name} /></div> | ||
</Card.Title> | ||
<Card.Text> | ||
<>{data.text}</><br /> | ||
<>{data.subtext}</> | ||
</Card.Text> | ||
</Card.Body> | ||
<Card.Footer> | ||
<small className="text-muted">{data.footer}</small> | ||
</Card.Footer> | ||
</Card> | ||
</div> | ||
))} | ||
</Form.RadioSet> | ||
</Form.Group> | ||
</> | ||
); | ||
}; | ||
|
||
CardSelect.defaultProps = { | ||
className: 'mb-3 col-md-3 offset-md-1', | ||
onChange: () => {}, | ||
}; | ||
|
||
CardSelect.propTypes = { | ||
/** The class name for the CardSelect component */ | ||
className: PropTypes.string, | ||
/** The Card components data to organize into a radio selectable form */ | ||
cardsData: PropTypes.node.isRequired, | ||
/** specifies the callback for the onChange event. The default value is a no-op function. */ | ||
onChange: PropTypes.func, | ||
/** The selected card */ | ||
selectedCardName: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default CardSelect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import CardSelect from './CardSelect'; | ||
|
||
describe('<CardSelect />', () => { | ||
describe('First card selected', () => { | ||
it('First card selected', () => { | ||
const tree = renderer.create(( | ||
<CardSelect | ||
cardsData={[ | ||
{ name: 'card1', title: 'Lorem Ipsum 1' }, | ||
{ name: 'card2', title: 'Lorem Ipsum 2', img: 'https://source.unsplash.com/360x200/?nature' }, | ||
{ name: 'card3', title: 'Lorem Ipsum 3', img: 'https://source.unsplash.com/360x200' }]} | ||
selectedCardName="card1" | ||
/> | ||
)).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('Second card selected', () => { | ||
it('Second card selected', () => { | ||
const tree = renderer.create(( | ||
<CardSelect | ||
cardsData={[ | ||
{ name: 'card1', title: 'Lorem Ipsum 1' }, | ||
{ name: 'card2', title: 'Lorem Ipsum 2', img: 'https://source.unsplash.com/360x200/?nature' }, | ||
{ name: 'card3', title: 'Lorem Ipsum 3', img: 'https://source.unsplash.com/360x200' }]} | ||
selectedCardName="card2" | ||
/> | ||
)).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.