Skip to content

Commit

Permalink
mostly working extra actions example
Browse files Browse the repository at this point in the history
  • Loading branch information
snide committed May 24, 2018
1 parent b51ad35 commit bcf03f2
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src-docs/src/views/color_picker/color_picker_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import { CustomButton } from './custom_button';
const customButtonSource = require('!!raw-loader!./custom_button');
const customButtonHtml = renderToHtml(CustomButton);

import { ExtraActions } from './extra_actions';
const extraActionsSource = require('!!raw-loader!./extra_actions');
const extraActionsHtml = renderToHtml(CustomButton);

export const ColorPickerExample = {
title: 'Color Picker',
sections: [{
Expand Down Expand Up @@ -66,5 +70,22 @@ export const ColorPickerExample = {
</p>
),
demo: <CustomButton />,
}, {
title: 'Extra actions',
source: [{
type: GuideSectionTypes.JS,
code: extraActionsSource,
}, {
type: GuideSectionTypes.HTML,
code: extraActionsHtml,
}],
text: (
<p>
You can optionally use a custom button as the trigger for selection using
the <EuiCode>button</EuiCode> prop. Please remember to add accessibility to this
component, using proper button markup and aria labeling.
</p>
),
demo: <ExtraActions />,
}],
};
2 changes: 1 addition & 1 deletion src-docs/src/views/color_picker/custom_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CustomButton extends Component {
constructor(props) {
super(props);
this.state = {
color: "",
color: "#00B3A4",
};
}

Expand Down
8 changes: 4 additions & 4 deletions src-docs/src/views/color_picker/custom_swatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export class CustomSwatches extends Component {
}

const customSwatches = [
'#333',
'#666',
'#999',
'#CCC',
{ color: '#333' },
{ color: '#666' },
{ color: '#999' },
{ color: '#CCC' },
]

return (
Expand Down
93 changes: 93 additions & 0 deletions src-docs/src/views/color_picker/extra_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, {
Component,
} from 'react';

import {
EuiColorPicker,
EuiFormRow,
EuiIcon,
EuiFlexGroup,
EuiFlexItem,
EuiButtonIcon,
EuiToolTip,
} from '../../../../src/components';

import { isValidHex } from '../../../../src/services';

export class ExtraActions extends Component {
constructor(props) {
super(props);
this.state = {
color: "",
customSwatches: ['#F44337', '#E92362', '#9C28B0', '#3F51B5', '#2196F3', '#18A9F5', '#1ABCD4', '#129688', '#4CAF50', '#CDDC38'],
};
}

handleChange = (value) => {
this.setState({ color: value });
};

addColor = (color) => {
this.state.customSwatches.push(color);

const addedSwatches = this.state.customSwatches;
console.log(addedSwatches);

this.setState({ customSwatches: addedSwatches });
}

removeColor = (color) => {
const trimmedSwatches = this.state.customSwatches.filter(swatch => swatch !== color);

this.setState({ customSwatches: trimmedSwatches });
}

render() {
const hasErrors = !isValidHex(this.state.color) && (this.state.color !== "");

let errors;
if (hasErrors) {
errors = ['Provide a valid hex value'];
}

const customSwatches = this.state.customSwatches.map(color => ({
color: color,
extraAction: (
<button onClick={this.removeColor.bind(this, color)} className="euiColorPickerSwatch__remove">
<EuiIcon type="cross" className="euiColorPickerSwatch__removeIcon" size="s" />
</button>
)
}));

return (
<div style={{ marginBottom: 100 }}>
<EuiFormRow
label="Pick a color"
isInvalid={hasErrors}
error={errors}
>
<EuiFlexGroup alignItems="center" gutterSize="s">
<EuiFlexItem grow={false}>
<EuiColorPicker
onChange={this.handleChange}
color={this.state.color}
isInvalid={hasErrors}
swatches={customSwatches}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiToolTip content={`Save color ${this.state.color} to my global palette`}>
<EuiButtonIcon
iconType="plusInCircle"
onClick={this.addColor.bind(this, this.state.color)}
aria-label="Add this color to the palette"
color="text"
/>
</EuiToolTip>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFormRow>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiColorPickerSwatch is rendered 1`] = `
<button
aria-label="aria-label"
class="euiColorPickerSwatch testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
4 changes: 4 additions & 0 deletions src/components/color_picker/_color_picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
max-width: ($euiSizeL * 5) + ($euiSizeS * 6) + 2px;

}

.euiColorPicker__flexItem {
position: relative;
}
}
20 changes: 20 additions & 0 deletions src/components/color_picker/_color_picker_swatch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,24 @@
&:focus {
@include euiFocusRing;
}

&:focus + .euiColorPickerSwatch__remove,
&:hover + .euiColorPickerSwatch__remove {
opacity: 1;
}
}

.euiColorPickerSwatch__remove {
@include euiFormControlLayout__clear('.euiColorPickerSwatch__removeIcon');

position: absolute;
top: -$euiSizeS;
right: -$euiSizeS;
opacity: 0;
transition: background-color $euiAnimSpeedNormal $euiAnimSlightResistance;

&:hover, &:focus {
opacity: 1;
background-color: $euiColorFullShade;
}
}
2 changes: 2 additions & 0 deletions src/components/color_picker/_index.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
@import "../form/form_control_layout/mixins";

@import "color_picker";
@import "color_picker_swatch";
13 changes: 8 additions & 5 deletions src/components/color_picker/color_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,24 @@ export class EuiColorPicker extends Component {
button,
} = this.props;
const classes = classNames('euiColorPicker', className);
const swatchOptions = swatches || VISUALIZATION_COLORS;
const defaultColors = VISUALIZATION_COLORS.map(color => ({ color: color }));

const swatchOptions = swatches || defaultColors;

const swatchButtons = swatchOptions.map((swatch) => (
<EuiFlexItem grow={false} key={swatch}>
<EuiFlexItem grow={false} key={swatch.color} className="euiColorPicker__flexItem">
<EuiColorPickerSwatch
className="euiColorPicker__swatchSelect"
color={swatch}
onClick={this.handleSwatchSelection.bind(this, swatch)}
color={swatch.color}
onClick={this.handleSwatchSelection.bind(this, swatch.color)}
extraAction={swatch.extraAction}
aria-label={`Select ${swatch} as the color`}
/>
</EuiFlexItem>
));

let swatchesPanel;
if (this.state.showColorSelector) {
if (this.state.showColorSelector && swatchOptions.length > 0) {
swatchesPanel = (
<EuiPanel
className="euiColorPicker__panel"
Expand Down
16 changes: 10 additions & 6 deletions src/components/color_picker/color_picker_swatch.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import React from 'react';
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

export const EuiColorPickerSwatch = ({
className,
color,
extraAction,
...rest,
}) => {
const classes = classNames('euiColorPickerSwatch', className);

return (
<button
className={classes}
style={{ background: color }}
{...rest}
/>
<Fragment>
<button
className={classes}
style={{ background: color }}
{...rest}
/>
{extraAction}
</Fragment>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/form/form_control_layout/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$clearSize: $euiSize;

@include size($clearSize);
background-color: transparentize($euiColorMediumShade, .5);
background-color: tintOrShade($euiColorMediumShade, 50%, 50%);
border-radius: $clearSize;
line-height: $clearSize;

Expand Down

0 comments on commit bcf03f2

Please sign in to comment.