Created
April 30, 2024 00:07
-
-
Save porfidev/50a110cd3e778185ae4f7fcfb04364bd to your computer and use it in GitHub Desktop.
Chechboxes React Native by porfi.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* | |
* @format | |
*/ | |
import React, {useState} from 'react'; | |
import { | |
SafeAreaView, | |
ScrollView, | |
StatusBar, | |
useColorScheme, | |
View, | |
} from 'react-native'; | |
import {Colors} from 'react-native/Libraries/NewAppScreen'; | |
import Checkbox from './components/Checkbox/Checkbox.tsx'; | |
import CheckboxList from './components/CheckboxList.tsx'; | |
function App(): React.JSX.Element { | |
const isDarkMode = useColorScheme() === 'dark'; | |
const backgroundStyle = { | |
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, | |
}; | |
const availableOptions = [ | |
{ | |
id: '1', | |
label: 'Opción 1', | |
}, | |
{ | |
id: '2', | |
label: 'Opción 2', | |
}, | |
{ | |
id: '3', | |
label: 'Opción 3', | |
}, | |
{ | |
id: '4', | |
label: 'Opción 4', | |
}, | |
]; | |
const [selected, setSelected] = useState<string[]>([]); | |
const onPressCheckbox = (id: string) => { | |
if (selected.includes(id)) { | |
const newSelected = selected.filter(item => item !== id); | |
return setSelected(newSelected); | |
} | |
const result = [...selected, id]; | |
setSelected(result); | |
}; | |
return ( | |
<SafeAreaView style={backgroundStyle}> | |
<StatusBar | |
barStyle={isDarkMode ? 'light-content' : 'dark-content'} | |
backgroundColor={backgroundStyle.backgroundColor} | |
/> | |
<ScrollView | |
contentInsetAdjustmentBehavior="automatic" | |
style={backgroundStyle}> | |
<View | |
style={{ | |
backgroundColor: isDarkMode ? Colors.black : Colors.white, | |
}}> | |
<CheckboxList | |
options={availableOptions} | |
selectedOption={selected} | |
onPressCheckbox={onPressCheckbox} | |
/> | |
</View> | |
</ScrollView> | |
</SafeAreaView> | |
); | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {StyleSheet} from 'react-native'; | |
const checkboxStyles = StyleSheet.create({ | |
wrapper: { | |
flexDirection: 'row', | |
alignItems: 'center', | |
gap: 15, | |
}, | |
check: { | |
width: 30, | |
height: 30, | |
borderRadius: 6, | |
backgroundColor: '#F7F7F7', | |
borderColor: '#656565', | |
borderWidth: 2, | |
justifyContent: 'center', | |
alignItems: 'center', | |
}, | |
checked: { | |
backgroundColor: '#0BB37B', | |
borderColor: '#06744F', | |
}, | |
label: { | |
fontSize: 16, | |
color: '#656565', | |
}, | |
labelChecked: { | |
color: '#06744F', | |
}, | |
}); | |
export default checkboxStyles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Image, Text, View} from 'react-native'; | |
import React from 'react'; | |
import CheckboxStyles from './Checkbox.styles.ts'; | |
const CheckIcon = require('./img/checkMark.png'); | |
type CheckboxProps = { | |
isChecked: boolean; | |
label: string; | |
}; | |
function Checkbox({isChecked, label}: CheckboxProps) { | |
return ( | |
<View style={CheckboxStyles.wrapper}> | |
<View style={[CheckboxStyles.check, isChecked && CheckboxStyles.checked]}> | |
{isChecked ? <Image source={CheckIcon} /> : null} | |
</View> | |
<Text | |
style={[ | |
CheckboxStyles.label, | |
isChecked && CheckboxStyles.labelChecked, | |
]}> | |
{label} | |
</Text> | |
</View> | |
); | |
} | |
export default Checkbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {Pressable, View} from 'react-native'; | |
import Checkbox from './Checkbox/Checkbox.tsx'; | |
type CheckboxListProps = { | |
options: { | |
id: string; | |
label: string; | |
}[]; | |
selectedOption?: string[]; | |
onPressCheckbox: (id: string) => void; | |
}; | |
function CheckboxList({ | |
options, | |
selectedOption, | |
onPressCheckbox, | |
}: CheckboxListProps) { | |
if (!options || options.length === 0) { | |
return null; | |
} | |
return ( | |
<View> | |
{options.map(option => ( | |
<Pressable | |
onPress={() => onPressCheckbox(option.id)} | |
style={{marginBottom: 10}}> | |
<Checkbox | |
key={option.id} | |
label={option.label} | |
isChecked={!!selectedOption?.includes(option.id)} | |
/> | |
</Pressable> | |
))} | |
</View> | |
); | |
} | |
export default CheckboxList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment