[英]How to manipulate the DOM inside of map function using onChange event on one of the checkboxes with react hooks
[英]React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
我有一個帶有復選框和輸入的功能組件。 我正在嘗試為復選框創建一個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
您可以使用useState
掛鈎創建狀態變量,該狀態變量跟蹤用戶是否單擊了復選框以禁用其他輸入。 useState
鈎子是一個帶有初始值並返回狀態變量的函數,以及一個用於更新該狀態變量的值的函數(其功能與setState
幾乎相同,但僅適用於單個狀態變量)。
例如:
const [x, setX] = React.useState(1); // `x` is initially 1, but that will change on later renders
setX(prevX => prevX + 1); // increments `x` for the next render;
您還需要創建一個事件處理程序函數來響應此復選框的單擊。
這是一個完整的示例:
import React from "react";
const MyComponent = props => {
const [disableInputIsChecked, setDisableInputIsChecked] = React.useState(false);
const [inputValue, setInputValue] = React.useState("");
function clearInput() {
setInputValue("");
}
function handleInputChange(event) {
setInputValue(event.target.value);
}
function handleCheckboxClick(event) {
if (!disableInputIsChecked) { // this click was to check the box
clearInput();
}
setDisableInputIsChecked(prevValue => !prevValue); // invert value
}
return (
<form>
<input type="checkbox" value={ disableInputIsChecked } onChange={ handleCheckboxClick }/>
<input type="text" value={ inputValue } onChange={ handleInputChange } disabled={ disableInputIsChecked }/>
</form>
)
}
這是一個有效的例子 。
也許您會想為這些值提出更好的名稱,但希望您能明白這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.