来自反应颜色的颜色选择器无法正常工作
Color picker from react-color doesn't work properly
我正在使用 react-color and this tutorial 来实现 ChromePicker。
我如何期待它的工作:
- 打开 ChromePicker
- select 使用色相条的颜色 and/or“大方块”
- 关闭 ChromePicker 后,我想更新道具上对象的颜色值
到目前为止它是这样工作的:
- 单击按钮时打开
- select颜色无法正常工作。当我单击色调并将那个小圆圈向侧面拖动时,颜色会发生变化(我知道这是因为有一个
this.state.color
)但圆圈保持相同的位置。另外,我希望在“大方块”上加载现在的调色板,但它没有这样做。如果我在正方形中单击 select 一种颜色,它的工作方式类似于色相条。即使值发生变化,圆圈仍保持在同一位置。
- 该值已保存,但也无法正常工作:每次更改颜色时都会多次调用该请求。我希望它仅在我 select 一种颜色并退出 ChromePicker(通过单击它之外的某个地方)后才改变,我认为
onChangeComplete
会那样做。
这是它的截图:
为什么ChromePicker这么烂?有办法解决吗?而且,我想仅在 ChromePicker 关闭后才将新颜色保存在 Company 上(似乎 onChangeComplete
不是由此触发的)
这是代码:
import React from 'react';
import { Button, Icon } from 'semantic-ui-react';
import { ChromePicker } from 'react-color';
import { connect } from 'react-redux';
import { Creators } from '../../actions';
class Banner extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
displayColorPicker: false,
};
}
handleClick = () => {
this.setState({ displayColorPicker: true });
};
handleClose = () => {
this.setState({ displayColorPicker: false });
};
handleChangeComplete = colors => {
const {
name,
color,
} = this.state;
this.setState({ color: colors.hex });
const { updateCompany } = this.props; // company is the entity from props that is updated
// it contains 2 values, its name and its color
updateCompany(this.props.company._id, {
name,
color,
});
};
render() {
this.props.color.color.color = this.state.color;
const popover = { // this is not essential, it's some styling for the picker
position: 'absolute',
zIndex: '2',
};
const cover = { // same as for popover
position: 'fixed',
top: '0px',
right: '0px',
bottom: '0px',
left: '0px',
};
const {company } = this.props; // gets the company from props
return (
<div className="banner-container settings-banner">
//the below div with its style is updating in real time when the color is changed
<div style={{ backgroundColor: this.state.color }}>
<div>
<Button
className="select-color-btn"
onClick={this.handleClick}>
Select a color
</Button>
{this.state.displayColorPicker ? (
<div style={popover}>
<div
style={cover}
onClick={this.handleClose}
onKeyDown={this.handleClick}
role="button"
tabIndex="0"
aria-label="Save"
/>
<ChromePicker
color={this.props.company.color}
onChangeComplete={this.handleChangeComplete}
/>
</div>
) : null}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
company: state.companies.selectedCompany,
});
const mapDispatchToProps = {
updateCompany: Creators.updateCompanyRequest,
};
export default connect(mapStateToProps, mapDispatchToProps)(Banner);
为了使 ChromePicker 正常工作(移动色相条和“大方块”内的圆圈,必须添加 onChange
功能并将其粘贴到 onHandleChange
。
此外,在 onHandleChangeComplete
中,颜色不能从状态中获取,而是从函数的参数中获取,colors
:
从 'react' 导入 React;
从 'semantic-ui-react' 导入 { 按钮,图标 };
从 'react-color' 导入 { ChromePicker };
从 'react-redux' 导入 { 连接 };
import { Creators } from '../../actions';
class Banner extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
displayColorPicker: false,
};
}
handleClick = () => {
this.setState({ displayColorPicker: true });
};
handleClose = () => {
this.setState({ displayColorPicker: false });
};
handleChange2 = colors => {
this.setState({ background: colors.hex });
};
handleChangeComplete = colors => {
this.setState({ background: colors.hex });
const {
name,
} = this.state;
const color = colors.hex;
this.setState({ color: colors.hex });
const { updateCompany } = this.props;
updateCompany(this.props.company._id, {
name,
color,
});
};
render() {
this.props.color.color.color = this.state.color;
const popover = { // this is not essential, it's some styling for the picker
position: 'absolute',
zIndex: '2',
};
const cover = { // same as for popover
position: 'fixed',
top: '0px',
right: '0px',
bottom: '0px',
left: '0px',
};
const {company } = this.props; // gets the company from props
return (
<div className="banner-container settings-banner">
<div style={{ backgroundColor: this.state.color }}>
<div>
<Button
className="select-color-btn"
onClick={this.handleClick}>
Select a color
</Button>
{this.state.displayColorPicker ? (
<div style={popover}>
<div
style={cover}
onClick={this.handleClose}
onKeyDown={this.handleClick}
role="button"
tabIndex="0"
aria-label="Save"
/>
<ChromePicker
color={this.state.background}
onChange={this.handleChange2}
onChangeComplete={this.handleChangeComplete}
/>
</div>
) : null}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
company: state.companies.selectedCompany,
});
const mapDispatchToProps = {
updateCompany: Creators.updateCompanyRequest,
};
export default connect(mapStateToProps, mapDispatchToProps)(Banner);
我正在使用 react-color and this tutorial 来实现 ChromePicker。
我如何期待它的工作:
- 打开 ChromePicker
- select 使用色相条的颜色 and/or“大方块”
- 关闭 ChromePicker 后,我想更新道具上对象的颜色值
到目前为止它是这样工作的:
- 单击按钮时打开
- select颜色无法正常工作。当我单击色调并将那个小圆圈向侧面拖动时,颜色会发生变化(我知道这是因为有一个
this.state.color
)但圆圈保持相同的位置。另外,我希望在“大方块”上加载现在的调色板,但它没有这样做。如果我在正方形中单击 select 一种颜色,它的工作方式类似于色相条。即使值发生变化,圆圈仍保持在同一位置。 - 该值已保存,但也无法正常工作:每次更改颜色时都会多次调用该请求。我希望它仅在我 select 一种颜色并退出 ChromePicker(通过单击它之外的某个地方)后才改变,我认为
onChangeComplete
会那样做。
这是它的截图:
为什么ChromePicker这么烂?有办法解决吗?而且,我想仅在 ChromePicker 关闭后才将新颜色保存在 Company 上(似乎 onChangeComplete
不是由此触发的)
这是代码:
import React from 'react';
import { Button, Icon } from 'semantic-ui-react';
import { ChromePicker } from 'react-color';
import { connect } from 'react-redux';
import { Creators } from '../../actions';
class Banner extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
displayColorPicker: false,
};
}
handleClick = () => {
this.setState({ displayColorPicker: true });
};
handleClose = () => {
this.setState({ displayColorPicker: false });
};
handleChangeComplete = colors => {
const {
name,
color,
} = this.state;
this.setState({ color: colors.hex });
const { updateCompany } = this.props; // company is the entity from props that is updated
// it contains 2 values, its name and its color
updateCompany(this.props.company._id, {
name,
color,
});
};
render() {
this.props.color.color.color = this.state.color;
const popover = { // this is not essential, it's some styling for the picker
position: 'absolute',
zIndex: '2',
};
const cover = { // same as for popover
position: 'fixed',
top: '0px',
right: '0px',
bottom: '0px',
left: '0px',
};
const {company } = this.props; // gets the company from props
return (
<div className="banner-container settings-banner">
//the below div with its style is updating in real time when the color is changed
<div style={{ backgroundColor: this.state.color }}>
<div>
<Button
className="select-color-btn"
onClick={this.handleClick}>
Select a color
</Button>
{this.state.displayColorPicker ? (
<div style={popover}>
<div
style={cover}
onClick={this.handleClose}
onKeyDown={this.handleClick}
role="button"
tabIndex="0"
aria-label="Save"
/>
<ChromePicker
color={this.props.company.color}
onChangeComplete={this.handleChangeComplete}
/>
</div>
) : null}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
company: state.companies.selectedCompany,
});
const mapDispatchToProps = {
updateCompany: Creators.updateCompanyRequest,
};
export default connect(mapStateToProps, mapDispatchToProps)(Banner);
为了使 ChromePicker 正常工作(移动色相条和“大方块”内的圆圈,必须添加 onChange
功能并将其粘贴到 onHandleChange
。
此外,在 onHandleChangeComplete
中,颜色不能从状态中获取,而是从函数的参数中获取,colors
:
从 'react' 导入 React; 从 'semantic-ui-react' 导入 { 按钮,图标 }; 从 'react-color' 导入 { ChromePicker }; 从 'react-redux' 导入 { 连接 }; import { Creators } from '../../actions';
class Banner extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
displayColorPicker: false,
};
}
handleClick = () => {
this.setState({ displayColorPicker: true });
};
handleClose = () => {
this.setState({ displayColorPicker: false });
};
handleChange2 = colors => {
this.setState({ background: colors.hex });
};
handleChangeComplete = colors => {
this.setState({ background: colors.hex });
const {
name,
} = this.state;
const color = colors.hex;
this.setState({ color: colors.hex });
const { updateCompany } = this.props;
updateCompany(this.props.company._id, {
name,
color,
});
};
render() {
this.props.color.color.color = this.state.color;
const popover = { // this is not essential, it's some styling for the picker
position: 'absolute',
zIndex: '2',
};
const cover = { // same as for popover
position: 'fixed',
top: '0px',
right: '0px',
bottom: '0px',
left: '0px',
};
const {company } = this.props; // gets the company from props
return (
<div className="banner-container settings-banner">
<div style={{ backgroundColor: this.state.color }}>
<div>
<Button
className="select-color-btn"
onClick={this.handleClick}>
Select a color
</Button>
{this.state.displayColorPicker ? (
<div style={popover}>
<div
style={cover}
onClick={this.handleClose}
onKeyDown={this.handleClick}
role="button"
tabIndex="0"
aria-label="Save"
/>
<ChromePicker
color={this.state.background}
onChange={this.handleChange2}
onChangeComplete={this.handleChangeComplete}
/>
</div>
) : null}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
company: state.companies.selectedCompany,
});
const mapDispatchToProps = {
updateCompany: Creators.updateCompanyRequest,
};
export default connect(mapStateToProps, mapDispatchToProps)(Banner);