Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 3x 31x 31x 31x 31x 31x 31x 31x 31x 29x 1x 31x 31x 1x 31x 31x 29x 8x 8x 8x 7x 7x 8x 29x 29x 29x 29x 29x 14x 15x 29x 27x 74x 74x 74x 27x 74x 8x 27x 27x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 1x 2x 3x 3x | import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import type { Tab, TabsProps, TabsRef } from '../Tabs.types';
import { Button } from '../../../atoms/Button/mobile/Button.native';
import { Card } from '../../../atoms/Card/mobile/Card.native';
import { useTheme } from '@sb/ui/components/Themes/ThemeProvider';
import { colors } from '@sb/styles/colors';
import { cn } from '@sb/libs';
export const Tabs = forwardRef<TabsRef, TabsProps>((props, ref) => {
const {
id,
className,
style,
tabLabelStyle,
tabs,
activeTab,
onTabChange,
onTabStatusChange,
tabPosition = 'top',
variant = 'primary',
padding = 'small',
bordered = false,
shadow = 'none',
accessibilityHint,
} = props;
const theme = useTheme();
const themeColors = colors[theme.theme];
const cardColor = { color: themeColors.colorLoader_Text };
const [activeIndexInternal, setActiveIndexInternal] = useState(activeTab ?? 0);
const isControlled = typeof activeTab === 'number';
const activeIndex = isControlled ? activeTab! : activeIndexInternal;
useEffect(() => {
if (isControlled && activeTab !== activeIndexInternal) {
setActiveIndexInternal(activeTab!);
}
}, [activeTab, activeIndexInternal, isControlled]);
const scrollRef = useRef<ScrollView>(null);
useImperativeHandle(ref, () => ({
scrollToActiveTab() {
scrollRef.current?.scrollTo({ x: 0, y: 0, animated: true });
},
}));
const [buttonsWidth, setButtonsWidth] = useState<number>(0);
if (!tabs || tabs.length === 0) return null;
const handleTabClick = (newIndex: number, disabled?: boolean) => {
Iif (disabled) return;
const oldIndex = activeIndex;
if (!isControlled) {
setActiveIndexInternal(newIndex);
onTabStatusChange?.(newIndex, oldIndex);
}
onTabChange?.(newIndex);
};
const activeButtonVariant = variant;
const inactiveButtonVariant = variant === 'primary' ? 'cyan' : 'primary';
const renderSectionContent = (item: Tab) => {
const content = item.body ?? item.content;
if (typeof content === 'string' || typeof content === 'number') {
return <Text style={cardColor}>{content}</Text>;
}
return content;
};
if (tabPosition === 'top' || tabPosition === 'bottom') {
const buttonsContainer = (
<View
style={styles.buttonRow}
onLayout={(e) => setButtonsWidth(e.nativeEvent.layout.width)}
accessibilityRole="tablist"
>
{tabs.map((tab, i) => {
const isActive = i === activeIndex;
let accessibilityLabelText = tab.label;
if (isActive) {
accessibilityLabelText += ', Active';
}
return (
<View key={i} style={styles.buttonWrapper}>
<Button
label={tab.label}
textStyle={tabLabelStyle}
onPress={() => handleTabClick(i, tab.disabled)}
variant={isActive ? activeButtonVariant : inactiveButtonVariant}
size="large"
rounded={false}
disabled={tab.disabled}
className={cn({
'opacity-50': tab.disabled,
'!mx-0 !px-0': padding === 'none',
// "py-[0.75em]": true,
})}
accessibilityRole="tab"
accessibilityLabel={accessibilityLabelText}
accessibilityState={{ selected: isActive, disabled: !!tab.disabled }}
accessibilityHint={tab.accessibilityHint}
/>
</View>
);
})}
</View>
);
const activeTabItem = tabs[activeIndex];
return (
<View
testID={id}
className={className}
style={[styles.container, style]}
accessibilityHint={accessibilityHint}
>
{tabPosition === 'top' && buttonsContainer}
<Card
id={`tab-content-${activeIndex}`}
width={buttonsWidth > 0 ? buttonsWidth : '100%'}
shadow={shadow}
padding={padding}
bordered={bordered}
className={`${className ? className + ' ' : ''}bg-transparent`}
accessible={false}
>
{activeTabItem.header != null && (
<View style={[styles.sectionHeader, { borderColor: cardColor.color }]}>
<Text className="font-normal" style={cardColor}>
{activeTabItem.header}
</Text>
</View>
)}
<View className={cn(activeTabItem.bodyClassName)}>
{renderSectionContent(activeTabItem)}
</View>
{activeTabItem.footer != null && (
<View style={[styles.sectionFooter, { borderColor: cardColor.color }]}>
<Text className="font-normal" style={cardColor}>
{activeTabItem.footer}
</Text>
</View>
)}
</Card>
{tabPosition === 'bottom' && buttonsContainer}
</View>
);
}
Eif (tabPosition === 'left' || tabPosition === 'right') {
const activeTabItem = tabs[activeIndex];
return (
<View className="flex-row" accessibilityHint={accessibilityHint}>
{tabPosition === 'left' && (
<View className="flex-col" accessibilityRole="tablist">
{tabs.map((tab, i) => {
const isActive = i === activeIndex;
let accessibilityLabelText = tab.label;
if (isActive) {
accessibilityLabelText += ', Active';
}
return (
<Button
key={i}
label={tab.label}
onPress={() => handleTabClick(i, tab.disabled)}
variant={isActive ? activeButtonVariant : inactiveButtonVariant}
size="medium"
rounded={false}
disabled={tab.disabled}
className="flex-1"
accessibilityRole="tab"
accessibilityLabel={accessibilityLabelText}
accessibilityState={{ selected: isActive, disabled: !!tab.disabled }}
accessibilityHint={tab.accessibilityHint}
/>
);
})}
</View>
)}
<Card
id={`tab-content-${activeIndex}`}
accessible={false}
className={`${className ? className + ' ' : ''}bg-transparent flex-1`}
shadow={shadow}
padding={padding}
bordered={bordered}
>
{activeTabItem.header != null && (
<View style={[styles.sectionHeader, { borderColor: cardColor.color }]}>
<Text className="font-normal" style={cardColor}>
{activeTabItem.header}
</Text>
</View>
)}
<View>{renderSectionContent(activeTabItem)}</View>
{activeTabItem.footer != null && (
<View style={[styles.sectionFooter, { borderColor: cardColor.color }]}>
<Text className="font-normal" style={cardColor}>
{activeTabItem.footer}
</Text>
</View>
)}
</Card>
{tabPosition === 'right' && (
<View className="flex-col" accessibilityRole="tablist">
{tabs.map((tab, i) => {
const isActive = i === activeIndex;
let accessibilityLabelText = tab.label;
if (isActive) {
accessibilityLabelText += ', Active';
}
return (
<Button
key={i}
label={tab.label}
onPress={() => handleTabClick(i, tab.disabled)}
variant={isActive ? activeButtonVariant : inactiveButtonVariant}
size="medium"
rounded={false}
disabled={tab.disabled}
className="flex-1"
accessibilityRole="tab"
accessibilityLabel={accessibilityLabelText}
accessibilityState={{ selected: isActive, disabled: !!tab.disabled }}
accessibilityHint={tab.accessibilityHint}
/>
);
})}
</View>
)}
</View>
);
}
return null;
});
Tabs.displayName = 'Tabs';
const styles = StyleSheet.create({
container: {
flex: 1,
},
rowContainer: {
flex: 1,
flexDirection: 'row',
},
buttonRow: {
flexDirection: 'row',
width: '100%',
},
buttonColumn: {
flexDirection: 'column',
width: 'auto',
},
buttonWrapper: {
flex: 1,
},
sectionHeader: {
borderBottomWidth: 1,
paddingBottom: 8,
marginBottom: 8,
},
sectionFooter: {
borderTopWidth: 1,
paddingTop: 8,
marginTop: 8,
},
contentColumn: {
flex: 1,
},
cardContainer: {
flex: 1,
},
});
|