forked from Graylog2/graylog2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add colour picker components (Graylog2#4626)
* Add first version of ColorPicker component * Add missing details of SelectPopover * Add `ColorPickerPopover` component
- Loading branch information
Showing
10 changed files
with
215 additions
and
2 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
61 changes: 61 additions & 0 deletions
61
graylog2-web-interface/src/components/common/ColorPicker.jsx
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,61 @@ | ||
import React from 'react'; | ||
import createReactClass from 'create-react-class'; | ||
import PropTypes from 'prop-types'; | ||
import { SwatchesPicker } from 'react-color'; | ||
|
||
/** | ||
* Color picker component that let the user select a color from a list of 95 colors grouped by hue. | ||
*/ | ||
const ColorPicker = createReactClass({ | ||
propTypes: { | ||
/** Indicates the selected color in hexadecimal format. */ | ||
color: PropTypes.string, | ||
/** | ||
* Color palette in hexadecimal format. By default it uses the color palette defined by react-color, | ||
* including 95 colors to pick from. | ||
*/ | ||
colors: PropTypes.array, | ||
/** | ||
* Height of the color picker in pixels. By default it displays 2 rows of colors and the first color | ||
* of the third row, indicating users that they can scroll through the list: | ||
* | ||
* `135px height per color row * 2 rows + 24px first color of 3rd row + 16px padding = 310px` | ||
* | ||
* You can set `Infinity` as `height` if you don't want the component to scroll. | ||
*/ | ||
height: PropTypes.number, | ||
/** | ||
* Width of the color picker in pixels. By default it displays 5 columns of colors: | ||
* | ||
* `50px width per color column * 5 columns + 22px of padding = 272px` | ||
*/ | ||
width: PropTypes.number, | ||
/** | ||
* Function that will be called when the selected color changes. | ||
* The function receives the color in hexadecimal format as first | ||
* argument and the event as the second argument. | ||
*/ | ||
onChange: PropTypes.func.isRequired, | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
color: undefined, | ||
colors: undefined, // Use default color palette. | ||
height: (135 * 2) + 24 + 16, // 135px color row * 2 rows + 24px first color 3rd row + 16px padding | ||
width: (50 * 5) + 16 + 6, // 50px color columns * 5 columns + 22px padding | ||
}; | ||
}, | ||
|
||
onColorChange(color, event) { | ||
this.props.onChange(color.hex, event); | ||
}, | ||
|
||
render() { | ||
return ( | ||
<SwatchesPicker {...this.props} onChange={this.onColorChange} /> | ||
); | ||
}, | ||
}); | ||
|
||
export default ColorPicker; |
27 changes: 27 additions & 0 deletions
27
graylog2-web-interface/src/components/common/ColorPicker.md
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,27 @@ | ||
```js | ||
const createReactClass = require('create-react-class'); | ||
|
||
const ColorPickerExample = createReactClass({ | ||
getInitialState() { | ||
return { | ||
color: undefined, | ||
}; | ||
}, | ||
|
||
handleColorChange(color) { | ||
this.setState({ color: color }); | ||
}, | ||
|
||
render() { | ||
const { color } = this.state; | ||
return ( | ||
<div> | ||
<p>{color ? `You picked ${color}.` : 'Pick a color'}</p> | ||
<ColorPicker color={color} onChange={this.handleColorChange} /> | ||
</div> | ||
); | ||
}, | ||
}); | ||
|
||
<ColorPickerExample /> | ||
``` |
3 changes: 3 additions & 0 deletions
3
graylog2-web-interface/src/components/common/ColorPickerPopover.css
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,3 @@ | ||
:local(.customPopover) .popover-content { | ||
padding: 0; | ||
} |
56 changes: 56 additions & 0 deletions
56
graylog2-web-interface/src/components/common/ColorPickerPopover.jsx
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,56 @@ | ||
import React from 'react'; | ||
import createReactClass from 'create-react-class'; | ||
import PropTypes from 'prop-types'; | ||
import { OverlayTrigger, Popover } from 'react-bootstrap'; | ||
import { ColorPicker } from 'components/common'; | ||
|
||
import style from './ColorPickerPopover.css'; | ||
|
||
/** | ||
* Component that renders a `ColorPicker` component inside a react bootstrap | ||
* popover. This is meant to use in forms and UIs with limited space, as the color | ||
* picker will only appear when the user interacts with the trigger node. | ||
* | ||
* This component will pass any additional props to `ColorPicker`, but their validation | ||
* is left for that component. Please look at `ColorPicker`'s documentation for more | ||
* information. | ||
*/ | ||
const ColorPickerPopover = createReactClass({ | ||
propTypes: { | ||
/** Provides an ID for this popover element. */ | ||
id: PropTypes.string.isRequired, | ||
/** Indicates where the popover should appear. */ | ||
placement: PropTypes.oneOf(['top', 'right', 'bottom', 'left']), | ||
/** Title to use in the popover header. */ | ||
title: PropTypes.string, | ||
/** React node that will be used as trigger to show/hide the popover. */ | ||
triggerNode: PropTypes.node.isRequired, | ||
/** Event that will show/hide the popover. */ | ||
triggerAction: PropTypes.oneOf(['click', 'hover', 'focus']), | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
placement: 'bottom', | ||
triggerAction: 'click', | ||
title: 'Pick a color', | ||
}; | ||
}, | ||
|
||
render() { | ||
const { id, placement, title, triggerNode, triggerAction, ...colorPickerProps } = this.props; | ||
const popover = ( | ||
<Popover id={id} title={title} className={style.customPopover}> | ||
<ColorPicker {...colorPickerProps} /> | ||
</Popover> | ||
); | ||
|
||
return ( | ||
<OverlayTrigger trigger={triggerAction} placement={placement} overlay={popover} rootClose> | ||
{triggerNode} | ||
</OverlayTrigger> | ||
); | ||
}, | ||
}); | ||
|
||
export default ColorPickerPopover; |
33 changes: 33 additions & 0 deletions
33
graylog2-web-interface/src/components/common/ColorPickerPopover.md
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,33 @@ | ||
```js | ||
const createReactClass = require('create-react-class'); | ||
const { Button } = require('react-bootstrap'); | ||
|
||
const ColorPickerOverlayExample = createReactClass({ | ||
getInitialState() { | ||
return { | ||
color: undefined, | ||
}; | ||
}, | ||
|
||
handleColorChange(color) { | ||
this.setState({ color: color }); | ||
}, | ||
|
||
render() { | ||
const { color } = this.state; | ||
|
||
return ( | ||
<div> | ||
<p>{color ? `You picked ${color}.` : 'Pick a color'}</p> | ||
<ColorPickerPopover id="example-color-picker" | ||
placement="right" | ||
color={color} | ||
triggerNode={<Button bsStyle="primary">Toggle color picker</Button>} | ||
onChange={this.handleColorChange} /> | ||
</div> | ||
); | ||
}, | ||
}); | ||
|
||
<ColorPickerOverlayExample /> | ||
``` |
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 |
---|---|---|
|
@@ -33,6 +33,6 @@ | |
} | ||
|
||
:local(.dataFilterInput) { | ||
margin-bottom: 0; | ||
margin-bottom: 0 !important; | ||
padding: 5px; | ||
} |
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