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

Use ferdaber/react-focus-within to allow closing the colorpicker when… #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@
"babel-preset-stage-0": "^6.16.0"
},
"peerDependencies": {
"prop-types": "^15.6.0",
"ra-core": "^2.0.2",
"react": "^16.3.0",
"prop-types": "^15.6.0"
"react": "^16.3.0"
},
"dependencies": {
"@material-ui/core": "^1.0.0",
"inflection": "^1.12.0",
"lodash.get": "^4.4.2",
"react-color": "^2.14.1",
"@material-ui/core": "^1.0.0",
"react-focus-within": "^2.0.2",
"recompose": "^0.27.1"
}
}
71 changes: 38 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TextField from '@material-ui/core/TextField';
import * as ReactColor from 'react-color';
import get from 'lodash.get';
import pure from 'recompose/pure';
import { FocusWithin } from 'react-focus-within'

require('./ColorInput.css');

Expand Down Expand Up @@ -72,39 +73,43 @@ class ColorInputComponent extends React.Component {
const Picker = ReactColor[`${picker}Picker`];

return (
<div>
<TextField
{...input}
margin="normal"
onFocus={this.handleOpen}
label={
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
<FocusWithin onBlur={(event) => this.handleClose()}>
{({ focusProps, isFocused }) => (
<div {...focusProps}>
<TextField
{...input}
margin="normal"
onFocus={this.handleOpen}
label={
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
}
error={!!(touched && error)}
helperText={touched && error || helperText}
className={className}
/>
}
error={!!(touched && error)}
helperText={touched && error || helperText}
className={className}
/>
{
this.state.show?
<div className="ColorInput-popup">
<div
className="ColorInput-cover"
onClick={this.handleClose}
/>
<Picker
{...options}
color={input.value}
onChange={this.handleChange}
/>
</div>
: null
}
</div>
{
this.state.show?
<div className="ColorInput-popup">
<div
className="ColorInput-cover"
onClick={this.handleClose}
/>
<Picker
{...options}
color={input.value}
onChange={this.handleChange}
/>
</div>
: null
}
</div>
)}
</FocusWithin>
)
}
};
Expand Down Expand Up @@ -133,4 +138,4 @@ ColorInputComponent.defaultProps = {
};

export const ColorField = PureTextField;
export const ColorInput = addField(ColorInputComponent);
export const ColorInput = addField(ColorInputComponent);