第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React Native,創(chuàng)建多個(gè)復(fù)選框

React Native,創(chuàng)建多個(gè)復(fù)選框

喵喔喔 2023-08-24 15:46:49
我正在嘗試為我的應(yīng)用程序創(chuàng)建多個(gè)復(fù)選框,如下圖所示。我創(chuàng)建了它,但面臨的一個(gè)問題是它的對齊不好。這是我嘗試創(chuàng)建它的代碼,我為此創(chuàng)建了單獨(dú)的組件,這樣我就可以在多個(gè)地方使用它。我只使用 facebook 的代碼,看起來很糟糕。有沒有任何庫可以做到這一點(diǎn)或更好的方法?與圖像相比,它看起來不太好。//component codefunction Choice({data, onValueChange, style}) {  const [selectedIndex, setSelectedIndex] = useState(-1);  const FilterButton = ({    callback,    text,    id,    selectedIndex,    btnstyles,    btnTxtStyles,    btnstylesSelect,    btnTxtStylesSelect,    imageStyle,  }) => {    const clicked = selectedIndex === id;    return (      <View style={{flexDirection: 'row'}}>        {!clicked ? (          <>            <TouchableOpacity              style={[btnstyles]}              onPress={() => {                callback(id);              }}></TouchableOpacity>          </>        ) : (          <>            <TouchableOpacity              style={btnstylesSelect}              onPress={() => {                callback(id);              }}>              <Image source={imagePath.tick} style={{borderRadius: 5}} />            </TouchableOpacity>          </>        )}      </View>    );  };  return (    <View style={[style]}>      {data.map((x, i) => (        <FilterButton          text={x.title}          id={i}          btnstyles={x.btnstyles}          btnTxtStyles={x.btnTxtStyles}          selectedIndex={selectedIndex}          btnTxtStylesSelect={x.btnTxtStylesSelect}          imageStyle={x.imageStyle}          btnstylesSelect={x.btnstylesSelect}          callback={(id) => {            setSelectedIndex(id);            if (onValueChange) {              onValueChange(id);            }          }}        />      ))}    </View>  );}如果有人知道如何做得更好或有任何圖書館,請?zhí)岢鼋ㄗh。
查看完整描述

2 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

為了更好地理解,我更改了組件的名稱:


// ChoicesHeaders.js


import Checkbox from './CheckBox';


const ChoicesHeaders = ({

? headersName,

? choicesHeaderStyles,

? choicesHeaderItemStyles,

}) => {

? return (

? ? <View style={choicesHeaderStyles}>

? ? ? {headersName.map((header) => (

? ? ? ? <View style={choicesHeaderItemStyles}>

? ? ? ? ? <Text>{header}</Text>

? ? ? ? </View>

? ? ? ))}

? ? </View>

? );

};


export default ChoicesHeaders;

// Checkbox.js


const Checkbox = ({

? id,

? btnstyles,

? btnstylesSelect,

? checked,

? selectedIndex,

? onCheckboxChange,

}) => {

? return selectedIndex !== id ? (

? ? <TouchableOpacity

? ? ? style={btnstyles}

? ? ? onPress={() => {

? ? ? ? onCheckboxChange(id);

? ? ? }}></TouchableOpacity>

? ) : (

? ? <TouchableOpacity

? ? ? style={btnstylesSelect}

? ? ? onPress={() => {

? ? ? ? onCheckboxChange(id);

? ? ? }}></TouchableOpacity>

? );

};


export default Checkbox;

// Choice.js


import Checkbox from './CheckBox';


const Choice = ({

? callback,

? text,

? btnstyles,

? btnTxtStyles,

? btnstylesSelect,

? btnTxtStylesSelect,

? onValueChange,

? choicesCount

}) => {

? const [selectedIndex, setSelectedIndex] = React.useState(-1);

? const handleCheckboxChange = (id) => {

? ? setSelectedIndex(id)

? ? if (onValueChange) {

? ? ? onValueChange(text, id);

? ? }

? };


? return (

? ? <View style={{ flexDirection: 'row', alignItems: 'center' }}>

? ? ? <View style={btnTxtStyles}>

? ? ? ? <Text>{text}</Text>

? ? ? </View>

? ? ? {Array.from({length: choicesCount}).map((item, index) => (

? ? ? <Checkbox

? ? ? ? id={index}

? ? ? ? btnstyles={btnstyles}

? ? ? ? btnstylesSelect={btnstylesSelect}

? ? ? ? selectedIndex={selectedIndex}

? ? ? ? onCheckboxChange={handleCheckboxChange}

? ? ? />

? ? ? ))}

? ? </View>

? );

};


export default Choice;


// App.js


import Choice from './components/Choice';

import ChoicesHeader from './components/ChoicesHeader';

// or any pure javascript modules available in npm

import { Card } from 'react-native-paper';



const data = [

? { title: 'Instagram', /* extra properties(e.g. keys to save in db) */},

? { title: 'Facebook',? },

? { title: 'Twitter',? ?},

? { title: 'Linkdin',? ?},

];


const choicesHeaders=['1 hr', '2 hr', '3 hr', '4 hr'];


export default function App() {

? const handleValueChange = (socialMediaName, checkboxId) => {

? ? // do what ever you want with this two

? };


? return (

? ? <View style={styles.container}>

? ? ? <ChoicesHeader

? ? ? ? headersName={choicesHeaders}

? ? ? ? choicesHeaderStyles={styles.choicesHeader}

? ? ? ? choicesHeaderItemStyles={styles.choicesHeaderItem}

? ? ? />

? ? ? {data.map((x) => (

? ? ? ? <Choice

? ? ? ? ? text={x.title}

? ? ? ? ? btnTxtStyles={styles.btnTxtStyles}

? ? ? ? ? btnstyles={styles.btnstyles}

? ? ? ? ? btnstylesSelect={styles.btnstylesSelect}

? ? ? ? ? onValueChange={handleValueChange}

? ? ? ? ? choicesCount={choicesHeaders.length}

? ? ? ? />

? ? ? ))}

? ? </View>

? );

}


const checkBoxBaseStyles = {

? ? height: 40,

? ? width: 40,

? ? margin: 10,

};


const labelDimentions = {

? width: 100

};


const styles = StyleSheet.create({

? btnstyles: {

? ? ...checkBoxBaseStyles,

? ? borderWidth: 1,

? ? borderColor: 'orange',

? },

? btnstylesSelect: {

? ? ...checkBoxBaseStyles,

? ? backgroundColor: 'orange',

? },

? btnTxtStyles: {

? ? ...labelDimentions,

? },

? choicesHeader: {

? ? flexDirection: 'row',

? ? alignItems: 'center',

? ? marginLeft: labelDimentions.width

? },

? choicesHeaderItem: {

? ? ...checkBoxBaseStyles,

? ? textAlign: 'center'

? },

});

查看完整回答
反對 回復(fù) 2023-08-24
?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

它絕對不完美,但這里至少是復(fù)選框上方文本的一個(gè)工作示例。我沒有將標(biāo)簽與復(fù)選框鏈接起來,而是將它們“放置”在復(fù)選框上方。


在 App.js 中 - 將您的應(yīng)用程序返回更改為:


?return (

? ? <View>

? ? ? <View style={{ flexDirection: 'row', alignItems: 'center', marginLeft:100}}>

? ? ? ? ? <Text style={styles.labelStyles}>1hr</Text>

? ? ? ? ? <Text style={styles.labelStyles}>2hr</Text>

? ? ? ? ? <Text style={styles.labelStyles}>3hr</Text>

? ? ? ? ? <Text style={styles.labelStyles}>4hr</Text>

? ? ? </View>

? ? ? <Choice data={data} onValueChange={handleValueChange} />

? ? </View>

? );

然后只需在您的 中添加一些樣式const styles,如下所示:


labelStyles: {

? ? margin:10,

? ? fontSize:13,

? ? color:'#afafb2'

? },

查看完整回答
反對 回復(fù) 2023-08-24
  • 2 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)