-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
11 changed files
with
247 additions
and
15 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
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,71 @@ | ||
import React, { useCallback, useState } from 'react' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
import { SketchPicker } from 'react-color' | ||
import { Typography } from '@material-ui/core' | ||
import PropTypes from 'prop-types' | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
root: { | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
}, | ||
gridList: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
width: '100%', | ||
}, | ||
previewContainer: { | ||
marginTop: 0, | ||
margin: theme.spacing(2), | ||
width: '100%', | ||
height: '60%', | ||
}, | ||
header: { | ||
paddingLeft: 0, | ||
}, | ||
divider: { | ||
marginBottom: 10, | ||
}, | ||
})) | ||
const BackgroundColorPicker = ({ user, onBackgroundColorSelection }) => { | ||
const classes = useStyles() | ||
const [selectedColor, setSelectedColor] = useState( | ||
user.backgroundColor || '#000' | ||
) | ||
|
||
const onColorChanged = useCallback( | ||
(color) => { | ||
setSelectedColor(color.hex) | ||
onBackgroundColorSelection(color.hex) | ||
}, | ||
[onBackgroundColorSelection] | ||
) | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography>Select your color</Typography> | ||
<div className={classes.gridList}> | ||
<SketchPicker | ||
color={selectedColor} | ||
disableAlpha | ||
onChangeComplete={onColorChanged} | ||
/> | ||
<div | ||
className={classes.previewContainer} | ||
style={{ | ||
backgroundColor: selectedColor, | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
BackgroundColorPicker.propTypes = { | ||
user: PropTypes.shape({ | ||
backgroundColor: PropTypes.string.isRequired, | ||
}).isRequired, | ||
onBackgroundColorSelection: PropTypes.func.isRequired, | ||
} | ||
|
||
export default BackgroundColorPicker |
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,37 @@ | ||
import React from 'react' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
import blue from '@material-ui/core/colors/blue' | ||
import BackgroundColorPicker from './BackgroundColorPicker' | ||
|
||
export default { | ||
title: 'Components/BackgroundColorPicker', | ||
component: BackgroundColorPicker, | ||
} | ||
|
||
const useStyles = makeStyles(() => ({ | ||
templateContainer: { | ||
background: blue['200'], | ||
height: 700, | ||
}, | ||
})) | ||
|
||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
const Template = (args) => { | ||
const classes = useStyles() | ||
return ( | ||
<div className={classes.templateContainer}> | ||
<BackgroundColorPicker {...args} /> | ||
</div> | ||
) | ||
} | ||
|
||
export const normal = Template.bind({}) | ||
normal.args = { | ||
user: { | ||
backgroundColor: '#138', | ||
}, | ||
onBackgroundColorSelection: () => { | ||
// eslint-disable-next-line no-console | ||
console.log('onBackgroundColorSelection') | ||
}, | ||
} |
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,40 @@ | ||
import React from 'react' | ||
import { shallow } from 'enzyme' | ||
import { SketchPicker } from 'react-color' | ||
|
||
jest.mock('react-color') | ||
|
||
const mockOnBackgroundColorSelection = jest.fn() | ||
const mockProps = { | ||
user: { | ||
backgroundColor: '#FF0000', | ||
}, | ||
onBackgroundColorSelection: mockOnBackgroundColorSelection, | ||
} | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('Background color picker component', () => { | ||
it('renders without error', () => { | ||
const BackgroundColorPicker = | ||
require('src/components/BackgroundColorPicker').default | ||
expect(() => | ||
shallow(<BackgroundColorPicker {...mockProps} />) | ||
).not.toThrow() | ||
}) | ||
|
||
it('calls onBackgroundColorSelection callback when the color changes', () => { | ||
const BackgroundColorPicker = | ||
require('src/components/BackgroundColorPicker').default | ||
const wrapper = shallow(<BackgroundColorPicker {...mockProps} />) | ||
|
||
// Mock that our color picker fires its change callback | ||
const colorData = { | ||
hex: '#CDCDCD', | ||
} | ||
wrapper.find(SketchPicker).prop('onChangeComplete')(colorData) | ||
expect(mockOnBackgroundColorSelection).toHaveBeenCalledWith('#CDCDCD') | ||
}) | ||
}) |
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
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
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
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
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
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.