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'
? },
});

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'
? },
添加回答
舉報(bào)