【发布时间】:2018-05-30 18:03:27
【问题描述】:
我有一个样式为styled components 的输入。它有一个min 和max 编号,并作为道具传递给红色边框:
export const StyledInput = styled.input`
::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
display: block;
outline: none;
font-size: 16px;
font-weight: 500;
-webkit-appearance: none;
border: 1px solid #e6e6e6;
border-radius: 5px;
background-color: #fff;
border: ${(props) => (props.option === true ? '0px' : '')};
line-height: 30px;
/* vertical-align: middle; */
z-index: 1;
height: 33px;
width: ${(props) => (props.numberInputWidth)};
padding: 0 8px;
margin-left: 5px;
margin-top: 20px;
transition: border-color 0.3s ease-in-out;
&:focus {
border-color: ${COLORS.LIGHTGREEN};
transition: border-color 0.3s ease-in-out;
${({ error }) => error && `
border-color: ${COLORS.ERROR};
`}
}
`
还有我的组件的状态:
this.state = {
inputTouched: false,
inputValue: 0,
}
和错误处理道具:
error={!!(inputValue > max || inputValue < min)}
假设min = 1 和max = 100。它按预期工作:
所以,当this.state.inputValue 超过最小值/最大值时,它会产生一个红色边框来指示错误。但是,在渲染页面时,输入设置为0,单击它会出现红色边框。
我希望它最初是绿色的,然后让错误逻辑处理它。 我希望它看起来像这样:
但仅在初始点击时
有没有办法这样做?
【问题讨论】:
标签: javascript css reactjs styled-components