-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Polish Label Color Picker #12464
Merged
Merged
Polish Label Color Picker #12464
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c1175d3
WIP introduce color picker component
alexpaxton 475bdb5
revert this commit
alexpaxton 8b2bd4d
Merge branch 'master' into clockface/color-picker
alexpaxton 1a8374f
Polish appearance of color selector
alexpaxton 61ea922
Allow color picker to conform to its parent's size
alexpaxton 543e284
Replace label color dropdown with clockface color selector
alexpaxton 340f4ec
Add testID props to color picker and associated components
alexpaxton 7a48f76
Update changelog
alexpaxton 305242d
Fix snapshot test
alexpaxton 7950631
Remove temporary debugging code for color picker
alexpaxton e4cd8c7
Refactor color picker to be a controlled component
alexpaxton 3d1ba1f
Update snapshots
alexpaxton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
WIP introduce color picker component
- Loading branch information
commit c1175d37845a6b2a1ac58980da524cd7667b0542
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
@import 'src/style/modules'; | ||
|
||
/* | ||
Color Picker Widget | ||
------------------------------------------------------------------------------ | ||
*/ | ||
|
||
$color-picker--margin: 0; | ||
|
||
.color-picker { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: stretch; | ||
width: 240px; | ||
background-color: $g2-kevlar; | ||
padding: $ix-marg-b; | ||
} | ||
|
||
.color-picker--swatches { | ||
margin-bottom: $ix-marg-b; | ||
position: relative; | ||
padding: $color-picker--margin; | ||
border-radius: $radius; | ||
overflow: hidden; | ||
} | ||
|
||
.color-picker--swatch { | ||
width: 10%; | ||
padding-bottom: 10%; | ||
position: relative; | ||
float: left; | ||
opacity: 1; | ||
transition: opacity 0.25s ease; | ||
|
||
> span { | ||
position: absolute; | ||
top: $color-picker--margin; | ||
left: $color-picker--margin; | ||
right: $color-picker--margin; | ||
bottom: $color-picker--margin; | ||
} | ||
|
||
&:hover { | ||
cursor: pointer; | ||
opacity: 0.5; | ||
} | ||
} | ||
|
||
.color-picker--form { | ||
display: flex; | ||
align-items: center; | ||
position: relative; | ||
} | ||
|
||
.input.color-picker--input { | ||
flex: 1 0 0; | ||
margin-right: $ix-marg-a; | ||
|
||
> input { | ||
padding-left: $ix-marg-d; | ||
} | ||
} | ||
|
||
.color-picker--selected { | ||
pointer-events: none; | ||
z-index: 2; | ||
position: absolute; | ||
top: 50%; | ||
left: $ix-marg-c; | ||
transform: translate(-50%, -50%); | ||
width: 18px; | ||
height: 18px; | ||
border-radius: 50%; | ||
} |
141 changes: 141 additions & 0 deletions
141
ui/src/clockface/components/color_picker/ColorPicker.tsx
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,141 @@ | ||
// Libraries | ||
import React, {Component, ChangeEvent} from 'react' | ||
import _ from 'lodash' | ||
|
||
// Components | ||
import {Button, IconFont, ButtonShape} from '@influxdata/clockface' | ||
import {Input} from 'src/clockface' | ||
import Swatch from 'src/clockface/components/color_picker/ColorPickerSwatch' | ||
|
||
// Constants | ||
import {colors} from 'src/clockface/constants/colors' | ||
|
||
// Styles | ||
import 'src/clockface/components/color_picker/ColorPicker.scss' | ||
|
||
interface Props { | ||
selectedHex: string | ||
onSelect: (hex: string) => void | ||
maintainInputFocus?: boolean | ||
} | ||
|
||
interface State { | ||
inputValue: string | ||
} | ||
|
||
export default class ColorPicker extends Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props) | ||
|
||
this.state = { | ||
inputValue: this.props.selectedHex || '', | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (prevProps.selectedHex !== this.props.selectedHex) { | ||
this.setState({inputValue: this.props.selectedHex}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it necessary to keep track of the hex value in state? it seems like this will be creating two sources of truth? could all the places that state instead call the onSelect() prop? |
||
} | ||
} | ||
|
||
render() { | ||
const {maintainInputFocus} = this.props | ||
const {inputValue} = this.state | ||
|
||
return ( | ||
<div className="color-picker"> | ||
<div className="color-picker--swatches"> | ||
{colors.map(color => ( | ||
<Swatch | ||
key={color.name} | ||
hex={color.hex} | ||
name={color.name} | ||
onClick={this.handleSwatchClick} | ||
/> | ||
))} | ||
</div> | ||
<div className="color-picker--form"> | ||
{this.selectedColor} | ||
<Input | ||
customClass="color-picker--input" | ||
placeholder="#000000" | ||
value={inputValue} | ||
onChange={this.handleInputChange} | ||
maxLength={7} | ||
onBlur={this.handleInputBlur} | ||
autoFocus={maintainInputFocus} | ||
/> | ||
<Button | ||
icon={IconFont.Refresh} | ||
shape={ButtonShape.Square} | ||
onClick={this.handleRandomizeColor} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
private handleSwatchClick = (hex: string): void => { | ||
const {onSelect} = this.props | ||
|
||
this.setState({inputValue: hex}) | ||
onSelect(hex) | ||
} | ||
|
||
private handleInputChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
const acceptedChars = [ | ||
'#', | ||
'a', | ||
'b', | ||
'c', | ||
'd', | ||
'e', | ||
'f', | ||
'0', | ||
'1', | ||
'2', | ||
'3', | ||
'4', | ||
'5', | ||
'6', | ||
'7', | ||
'8', | ||
'9', | ||
] | ||
|
||
const trimmedValue = e.target.value.trim() | ||
const inputValue = trimmedValue | ||
.split('') | ||
.filter(char => acceptedChars.includes(char.toLowerCase())) | ||
.join('') | ||
|
||
this.setState({inputValue}) | ||
} | ||
|
||
private handleInputBlur = (e: ChangeEvent<HTMLInputElement>) => { | ||
const {maintainInputFocus} = this.props | ||
|
||
if (maintainInputFocus) { | ||
e.target.focus() | ||
} | ||
} | ||
|
||
private handleRandomizeColor = (): void => { | ||
const {onSelect} = this.props | ||
const {hex} = _.sample(colors) | ||
|
||
this.setState({inputValue: hex}) | ||
onSelect(hex) | ||
} | ||
|
||
private get selectedColor(): JSX.Element { | ||
const {inputValue} = this.state | ||
|
||
return ( | ||
<div | ||
className="color-picker--selected" | ||
style={{backgroundColor: inputValue}} | ||
/> | ||
) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
ui/src/clockface/components/color_picker/ColorPickerSwatch.tsx
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 @@ | ||
// Libraries | ||
import React, {Component} from 'react' | ||
|
||
interface Props { | ||
name: string | ||
hex: string | ||
onClick: (hex: string) => void | ||
} | ||
|
||
export default class ColorPickerSwatch extends Component<Props> { | ||
render() { | ||
const {name, hex} = this.props | ||
return ( | ||
<div | ||
className="color-picker--swatch" | ||
title={name} | ||
onClick={this.handleClick} | ||
> | ||
<span style={{backgroundColor: hex}} /> | ||
</div> | ||
) | ||
} | ||
|
||
private handleClick = (): void => { | ||
this.props.onClick(this.props.hex) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should prevProps be typed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YES