Skip to content

Commit

Permalink
Added ability to clear new palette, generate new color, checks if new…
Browse files Browse the repository at this point in the history
… palette is full
  • Loading branch information
GpGardner committed Jun 25, 2020
1 parent e89930d commit 827fa42
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/Components/NewPaletteForm/DraggableColorBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const styles = {
};

const DraggableColorBox = SortableElement((props) => {
console.log(props);
const { classes, name, color } = props;

const handleDeleteColorBox = () => {
Expand Down
47 changes: 38 additions & 9 deletions src/Components/NewPaletteForm/NewPaletteForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ const styles = (theme) => ({
});

class NewPaletteForm extends Component {
static defaultProps = {
maxColors: 20,
};
constructor(props) {
super(props);
this.state = {
open: true,
currentColor: "teal",
newColorName: "",
newPaletteName: "",
colors: seedColors[0].colors,
colors: this.props.palettes[0].colors,
};
}

Expand Down Expand Up @@ -159,15 +162,31 @@ class NewPaletteForm extends Component {
});
};

clearPalette = () => {
this.setState({
colors: [],
});
};

addRandomColor = () => {
const allColors = this.props.palettes.map((p) => p.colors).flat();
let randomNumber = Math.floor(Math.random() * 100);
let randomlyPickedColor = allColors[randomNumber];
this.setState({
colors: [...this.state.colors, randomlyPickedColor],
});
};

onSortEnd = ({ oldIndex, newIndex }) => {
this.setState(({ colors }) => ({
colors: arrayMove(colors, oldIndex, newIndex),
}));
};

render() {
const { classes } = this.props;
const { open } = this.state;
const { classes, maxColors } = this.props;
const { open, colors } = this.state;
const paletteIsFull = colors.length >= maxColors;

return (
<div className={classes.root}>
Expand Down Expand Up @@ -226,10 +245,19 @@ class NewPaletteForm extends Component {
<Divider />
<Typography variant="h4">Design your palette</Typography>
<div>
<Button variant="contained" color="primary">
Random Color
<Button
variant="contained"
color="primary"
disabled={paletteIsFull}
onClick={this.addRandomColor}
>
{paletteIsFull ? "Palette Full" : "Random Color"}
</Button>
<Button variant="contained" color="secondary">
<Button
variant="contained"
color="secondary"
onClick={this.clearPalette}
>
Clear Palette
</Button>
</div>
Expand All @@ -254,9 +282,10 @@ class NewPaletteForm extends Component {
variant="contained"
type="submit"
color="primary"
style={{ backgroundColor: this.state.currentColor }}
disabled={paletteIsFull}
style={paletteIsFull ? {} : { backgroundColor: this.state.currentColor }}
>
Add Color
{paletteIsFull ? "Palette Full" : "Add Color!"}
</Button>
</ValidatorForm>
</Drawer>
Expand All @@ -267,7 +296,7 @@ class NewPaletteForm extends Component {
>
<div className={classes.drawerHeader} />
<DraggableColorList
colors={this.state.colors}
colors={colors}
removeColorBox={this.removeColorBox}
axis="xy"
onSortEnd={this.onSortEnd}
Expand Down

0 comments on commit 827fa42

Please sign in to comment.