【发布时间】:2019-05-31 15:30:40
【问题描述】:
我有一个带有复选框和输入的功能组件。我正在尝试为复选框创建一个 onChange 函数,该函数通过实现useState React Hook 来清除输入并在用户单击它时禁用它。有没有人有任何以前做过类似事情的 Hooks 经验?我正在尝试查看代码示例以更好地解决这个问题,因为我还是 React Hooks 的新手
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import {
Col, Row, Icon, Input, Tooltip
} from 'antd'
import Checkbox from '../elements/Checkbox'
const CustomerDetails = ({ customer }) => {
if (customer === null) {
return (
<Container>
<Row>
<Col span={24}>
<ErrorContainer>
<Icon type="exclamation-circle" />
</ErrorContainer>
</Col>
</Row>
</Container>
)
}
return (
<Container>
<h2>{customer.contact.name}</h2>
<Row>
<Col span={8}>
<H3>
<strong>Primary Contact:</strong>
</H3>
<P>{customer.contact.name}</P>
<P>{customer.contact.phone}</P>
</Col>
<Col span={8}>
<H3>
<strong>Service Address:</strong>
</H3>
<P>{customer.site.address1}</P>
<P>{customer.site.address2}</P>
<P>
{customer.site.city}, {customer.site.state}
{customer.site.postalCode}
</P>
</Col>
<Col span={8}>
<H3>
<strong>Billing Address:</strong>
</H3>
<P>{customer.account.billingStreet}</P>
<P>
{customer.account.billingCity}, {customer.account.billingState}
{customer.account.billingPostalCode}
</P>
</Col>
</Row>
<br />
<Row>
<Col span={10}>
<h4>
PRIMARY CONTACT EMAIL
<Tooltip
placement="topRight"
title={primaryContact}
>
<StyledTooltipIcon
type="question-circle"
theme="filled"
/>
</Tooltip>
</h4>
</Col>
</Row>
<Row>
<Col span={8}>
<StyledInput value={customer.contact.email} />
</Col>
<Col span={2} />
<Col span={8}>
<StyledCheckbox /> EMAIL OPT OUT{' '}
<Tooltip
placement="topRight"
title={emailText}
>
<StyledTooltipIcon
type="question-circle"
theme="filled"
/>
</Tooltip>
</Col>
</Row>
</Container>
)
}
CustomerDetails.propTypes = {
customer: PropTypes.object
}
CustomerDetails.defaultProps = {
customer: {}
}
const Container = styled.div`
text-align: left;
`
const StyledCheckbox = styled(Checkbox)`
input + span {
border-radius: 0px;
width: 35px;
height: 35px;
border: 2px solid ${({ theme }) => theme.colors.black};
background-color: transparent;
color: ${({ theme }) => theme.colors.black};
border-color: ${({ theme }) => theme.colors.black};
transition: none;
}
input:checked + span {
border: 2px solid ${({ theme }) => theme.colors.black};
width: 30px;
height: 30px;
}
input + span:after {
border-color: ${({ theme }) => theme.colors.black};
left: 20%;
transition: none;
width: 12.5px;
height: 20px;
}
input:disabled + span:after {
border-color: ${({ theme }) => theme.colors.gray};
}
input:not(:checked):hover + span:after {
border-color: ${({ theme }) => theme.colors.gray};
opacity: 1;
transform: rotate(45deg) scale(1) translate(-50%, -50%);
}
input:focus + span {
border-color: ${({ theme }) => theme.colors.primary};
}
`
const StyledInput = styled(Input)`
max-width: 100%;
&&& {
border: 2px solid ${({ theme }) => theme.colors.black};
border-radius: 0px;
height: 35px;
}
`
const ErrorContainer = styled.div`
/* margin-left: 25%; */
`
const StyledTooltipIcon = styled(Icon)`
color: #1571da;
`
const H3 = styled.h3`
white-space: nowrap;
margin-top: 0;
margin-bottom: 0;
line-height: 1.5;
}
`
const H4 = styled.h4`
text-decoration: underline;
color: #1590ff;
`
const P = styled.p`
margin-top: 0;
margin-bottom: 0;
font-size: 1rem;
`
export default CustomerDetails
【问题讨论】:
-
澄清一下,无论复选框的原始值是什么,您都希望在单击复选框时清除和禁用输入字段?如果再次单击复选框,您是否希望输入再次变为活动状态?
标签: reactjs onclick onchange react-hooks