Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds prepend and append to the EuiColorPicker #2819

Merged
merged 8 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Converted `EuiDescribedFormGroup` to TypeScript ([#2810](https://github.com/elastic/eui/pull/2810))
- Changed SASS comments to non-compiled comments in invisibles files ([#2807](https://github.com/elastic/eui/pull/2807))
- Added `rowHeader` prop to `EuiBasicTable` to allow consumers to set the identifying cell in a row ([#2802](https://github.com/elastic/eui/pull/2802))
- Added prepend and append to `EuiColorPicker` ([#2819](https://github.com/elastic/eui/pull/2819))
- Improved `EuiDescribedFormGroup` accessibility by avoiding duplicated output in screen readers ([#2783](https://github.com/elastic/eui/pull/2783))
- Added optional `key` attribute to `EuiContextMenu` items and relaxed `name` attribute to allow any React node ([#2817](https://github.com/elastic/eui/pull/2817))

Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/color_picker/kitchen_sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const KitchenSink = () => {
return (
<React.Fragment>
{/* DisplayToggles wrapper for Docs only */}
<DisplayToggles canLoading={false}>
<DisplayToggles canLoading={false} canPrepend={true} canAppend={true}>
<EuiColorPicker color={color} onChange={setColor} />
</DisplayToggles>
<EuiSpacer />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,92 @@ exports[`renders EuiColorPicker with an empty swatch when color is null 1`] = `
</div>
`;

exports[`renders a EuiColorPicker with a prepend and append 1`] = `
<div
class="euiPopover euiPopover--anchorDownLeft euiPopover--displayBlock"
>
<div
class="euiPopover__anchor"
>
<div
class="euiFormControlLayout euiFormControlLayout--group"
>
<label
class="euiFormLabel euiFormControlLayout__prepend"
>
prepend
</label>
<div
class="euiFormControlLayout__childrenWrapper"
>
<div
style="color:#ffeedd"
>
<div
class="euiFormControlLayout"
>
<div
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-label="Press the down key to open a popover containing color options"
autocomplete="off"
class="euiFieldText euiColorPicker__input euiColorPicker__input--inGroup euiFieldText--withIcon"
data-test-subj="colorPickerAnchor"
maxlength="7"
type="text"
value="#FFEEDD"
/>
<div
class="euiFormControlLayoutIcons"
>
<span
class="euiFormControlLayoutCustomIcon"
>
<svg
aria-hidden="true"
class="euiIcon euiIcon--medium euiIcon-isLoading euiFormControlLayoutCustomIcon__icon"
focusable="false"
height="16"
role="img"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
</span>
</div>
</div>
</div>
</div>
<div
class="euiFormControlLayoutIcons euiFormControlLayoutIcons--right"
>
<span
class="euiFormControlLayoutCustomIcon"
>
<svg
aria-hidden="true"
class="euiIcon euiIcon--medium euiIcon-isLoading euiFormControlLayoutCustomIcon__icon"
focusable="false"
height="16"
role="img"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
</span>
</div>
</div>
<label
class="euiFormLabel euiFormControlLayout__append"
>
append
</label>
</div>
</div>
</div>
`;

exports[`renders compressed EuiColorPicker 1`] = `
<div
class="euiPopover euiPopover--anchorDownLeft euiPopover--displayBlock"
Expand Down
11 changes: 10 additions & 1 deletion src/components/color_picker/_color_picker.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.euiColorPicker {
position: relative;
width: $euiColorPickerWidth;

}

.euiColorPicker__popoverAnchor {
Expand All @@ -28,3 +27,13 @@
// sass-lint:disable no-important
padding-bottom: 0 !important;
}

// sass-lint:disable no-important
.euiColorPicker__input--inGroup {
height: $euiFormControlLayoutGroupInputHeight !important;
box-shadow: none !important;

&.euiFieldText--compressed {
height: $euiFormControlLayoutGroupInputCompressedHeight !important;
}
}
14 changes: 14 additions & 0 deletions src/components/color_picker/color_picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ test('renders inline EuiColorPicker', () => {
expect(colorPicker).toMatchSnapshot();
});

test('renders a EuiColorPicker with a prepend and append', () => {
const component = render(
<EuiColorPicker
onChange={onChange}
color="#ffeedd"
prepend="prepend"
append="append"
{...requiredProps}
/>
);

expect(component).toMatchSnapshot();
});

test('renders EuiColorPicker with an empty swatch when color is null', () => {
const colorPicker = render(
<EuiColorPicker onChange={onChange} color={null} {...requiredProps} />
Expand Down
26 changes: 23 additions & 3 deletions src/components/color_picker/color_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { EuiFocusTrap } from '../focus_trap';
import { EuiFlexGroup, EuiFlexItem } from '../flex';
// @ts-ignore
import { EuiFieldText } from '../form/field_text';
import { EuiFormControlLayout } from '../form/form_control_layout';
import {
EuiFormControlLayout,
EuiFormControlLayoutProps,
} from '../form/form_control_layout';
import { EuiI18n } from '../i18n';
import { EuiPopover } from '../popover';
import {
Expand Down Expand Up @@ -77,6 +80,16 @@ export interface EuiColorPickerProps
* Array of hex strings (3 or 6 character) to use as swatch options. Defaults to EUI visualization colors
*/
swatches?: string[];

/**
* Creates an input group with element(s) coming before input
elizabetdev marked this conversation as resolved.
Show resolved Hide resolved
*/
prepend?: EuiFormControlLayoutProps['prepend'];

/**
* Creates an input group with element(s) coming after input
*/
append?: EuiFormControlLayoutProps['append'];
}

function isKeyboardEvent(
Expand All @@ -102,6 +115,8 @@ export const EuiColorPicker: FunctionComponent<EuiColorPickerProps> = ({
readOnly = false,
swatches = VISUALIZATION_COLORS,
popoverZIndex,
prepend,
append,
}) => {
const [isColorSelectorShown, setIsColorSelectorShown] = useState(false);
const [colorAsHsv, setColorAsHsv] = useState(
Expand Down Expand Up @@ -131,6 +146,9 @@ export const EuiColorPicker: FunctionComponent<EuiColorPickerProps> = ({
const swatchClass = 'euiColorPicker__swatchSelect';
const testSubjAnchor = 'colorPickerAnchor';
const testSubjPopover = 'colorPickerPopover';
const inputClasses = classNames('euiColorPicker__input', {
elizabetdev marked this conversation as resolved.
Show resolved Hide resolved
'euiColorPicker__input--inGroup': prepend || append,
});

const handleOnChange = (hex: string) => {
setLastHex(hex);
Expand Down Expand Up @@ -313,7 +331,9 @@ export const EuiColorPicker: FunctionComponent<EuiColorPickerProps> = ({
readOnly={readOnly}
fullWidth={fullWidth}
compressed={compressed}
onKeyDown={handleToggleOnKeyDown}>
onKeyDown={handleToggleOnKeyDown}
prepend={prepend}
append={append}>
<div
// Used to pass the chosen color through to form layout SVG using currentColor
style={{ color: showColor && color ? color : undefined }}>
Expand All @@ -325,7 +345,7 @@ export const EuiColorPicker: FunctionComponent<EuiColorPickerProps> = ({
]}>
{([openLabel, closeLabel]: string[]) => (
<EuiFieldText
className="euiColorPicker__input"
className={inputClasses}
onClick={handleInputActivity}
onKeyDown={handleInputActivity}
value={color ? color.toUpperCase() : ''}
Expand Down