React組件之多選Checkbox實(shí)例
更新時(shí)間:2023年10月23日 14:22:34 作者:追影的React開(kāi)發(fā)者
這篇文章主要介紹了React組件之多選Checkbox實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
React組件之多選Checkbox
import React, { PureComponent } from "react";
import { List, NavBar, Checkbox } from "antd-mobile";
import { Icon } from "antd";
import TouchFeedback from "rmc-feedback";
import NavContentContainer from "./NavContentContainer";
import PanelContentContainer from "./PanelContentContainer";
export default class Checkbox_ extends PureComponent {
constructor(props) {
super(props);
this.state = { select: [] };
}
componentWillReceiveProps(props) {
const { show, init } = props;
if (show) {
this.setState({ select: init || [] });
}
}
getDefaultChecked = value => {
const { init } = this.props;
const result = (init || []).filter(i => i === value);
return result.length !== 0;
};
render() {
const { show, data, title, hide, save } = this.props;
const { select } = this.state;
return (
<div
style={{
display: show ? "block" : "none",
zIndex: 1,
position: "absolute",
backgroundColor: "#fff",
overflowY: "auto",
top: 0,
bottom: 0,
left: 0,
right: 0
}}
>
<NavBar
className="global-navbar"
mode="dark"
icon={
<TouchFeedback activeClassName="primary-feedback-active">
<Icon type="left" />
</TouchFeedback>
}
onLeftClick={() => hide()}
rightContent={[
<Icon
type="check"
style={{ marginRight: "16px" }}
onClick={() => save(select)}
/>
]}
>
{title}
</NavBar>
<NavContentContainer>
<PanelContentContainer>
<List>
{data.map(i => (
<Checkbox.CheckboxItem
wrap
key={i.value}
defaultChecked={this.getDefaultChecked(i.value)}
onChange={() => {
if (select.indexOf(i.value) === -1) {
select.push(i.value);
} else {
const odd = select;
odd.splice(odd.indexOf(i.value), 1);
this.setState({
select: odd
});
}
}}
>
{i.key}
</Checkbox.CheckboxItem>
))}
</List>
</PanelContentContainer>
</NavContentContainer>
</div>
);
}
}<Checkbox
show={showCheckbox}
data={checkboxData}
title={checkboxTitle}
id={checkboxId}
init={checkboxNum[checkboxId]}
hide={() => this.setState({ showCheckbox: false })}
save={v => {
this.setState({
showCheckbox: false,
checkboxNum: { ...checkboxNum, [checkboxId]: v }
});
}}
/>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
React+Router多級(jí)導(dǎo)航切換路由方式
這篇文章主要介紹了React+Router多級(jí)導(dǎo)航切換路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Objects are not valid as a Rea
這篇文章主要為大家介紹了Objects are not valid as a React child報(bào)錯(cuò)解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
詳解Webpack+Babel+React開(kāi)發(fā)環(huán)境的搭建的方法步驟
本篇文章主要介紹了詳解Webpack+Babel+React開(kāi)發(fā)環(huán)境的搭建的方法步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
react學(xué)習(xí)每天一個(gè)hooks?useWhyDidYouUpdate
這篇文章主要為大家介紹了react學(xué)習(xí)每天一個(gè)hooks?useWhyDidYouUpdate使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
React自定義實(shí)現(xiàn)useWatch的方式和特點(diǎn)
這篇文章主要介紹了React自定義實(shí)現(xiàn)useWatch的方式和特點(diǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-05-05
漸進(jìn)式源碼解析React更新流程驅(qū)動(dòng)
這篇文章主要為大家介紹了漸進(jìn)式源碼解析React更新流程驅(qū)動(dòng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

