Skip to content

Instantly share code, notes, and snippets.

@AlanGreyjoy
Created August 17, 2024 02:24
Show Gist options
  • Save AlanGreyjoy/3a870eef6a65a4c2b9a2cd959289376a to your computer and use it in GitHub Desktop.
Save AlanGreyjoy/3a870eef6a65a4c2b9a2cd959289376a to your computer and use it in GitHub Desktop.
Dead simple React Native Expandable View (RNUILIB & ICONIFY)
import { useState } from 'react'
import { Iconify } from 'react-native-iconify'
import { Colors, Text, TouchableOpacity, View } from 'react-native-ui-lib'
export default function ExpandableView(props) {
const title = props.title || 'Expandable View'
const defaultExpanded = props.defaultExpanded || false
const divider = props.divider || false
const [expanded, setExpanded] = useState(defaultExpanded || false)
const renderIcon = () => {
if (expanded) {
return (
<Iconify
icon='fluent:chevron-up-12-filled'
size={25}
color={Colors.$iconPrimary}
/>
)
} else {
return (
<Iconify
icon='fluent:chevron-down-12-filled'
size={25}
color={Colors.$iconPrimary}
/>
)
}
}
const handleToggleExpand = () => {
setExpanded(!expanded)
}
return (
<View flex>
{/* Title */}
<TouchableOpacity onPress={handleToggleExpand}>
<View
row
justifyContent={'space-between'}
>
<View>
<Text
text60BO
grey20
>
{title}
</Text>
</View>
<View>{renderIcon()}</View>
</View>
</TouchableOpacity>
{/* Divider */}
{divider && (
<View
marginT-10
style={{
borderBottomWidth: 1,
borderBottomColor: Colors.grey50
}}
/>
)}
{/* Children */}
{expanded && props.children}
</View>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment