Files
react-native/Libraries/LogBox/UI/LogBoxInspectorHeader.js
T
Rick Hanlon 8cfa379503 LogBox - Update header to cycle around to begining
Summary:
This diff updates the header pagination logic so that it circles around to the beginning/end when reaching either end of the log list in the LogBox inspector.

It also changes the header message for a single log from "Logs" to "Log 1 of 1"

Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D18091621

fbshipit-source-id: 4d7e5e2cb0eb1a1ed09ca8ad318e6715d674648f
2019-10-28 10:10:55 -07:00

139 lines
3.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
import Image from '../../Image/Image';
import Platform from '../../Utilities/Platform';
import * as React from 'react';
import SafeAreaView from '../../Components/SafeAreaView/SafeAreaView';
import StyleSheet from '../../StyleSheet/StyleSheet';
import Text from '../../Text/Text';
import View from '../../Components/View/View';
import LogBoxImageSource from './LogBoxImageSource';
import LogBoxButton from './LogBoxButton';
import * as LogBoxStyle from './LogBoxStyle';
import type {LogLevel} from '../Data/LogBoxLog';
type Props = $ReadOnly<{|
onSelectIndex: (selectedIndex: number) => void,
selectedIndex: number,
total: number,
level: LogLevel,
|}>;
function LogBoxInspectorHeader(props: Props): React.Node {
const prevIndex =
props.selectedIndex - 1 < 0 ? props.total - 1 : props.selectedIndex - 1;
const nextIndex =
props.selectedIndex + 1 > props.total - 1 ? 0 : props.selectedIndex + 1;
const titleText = `Log ${props.selectedIndex + 1} of ${props.total}`;
return (
<SafeAreaView style={styles[props.level]}>
<View style={styles.header}>
<LogBoxInspectorHeaderButton
disabled={props.total <= 1}
level={props.level}
image={LogBoxImageSource.chevronLeft}
onPress={() => props.onSelectIndex(prevIndex)}
/>
<View style={styles.title}>
<Text style={styles.titleText}>{titleText}</Text>
</View>
<LogBoxInspectorHeaderButton
disabled={props.total <= 1}
level={props.level}
image={LogBoxImageSource.chevronRight}
onPress={() => props.onSelectIndex(nextIndex)}
/>
</View>
</SafeAreaView>
);
}
function LogBoxInspectorHeaderButton(
props: $ReadOnly<{|
disabled: boolean,
image: string,
level: LogLevel,
onPress?: ?() => void,
|}>,
): React.Node {
return (
<LogBoxButton
backgroundColor={{
default:
props.level === 'warn'
? LogBoxStyle.getWarningColor()
: LogBoxStyle.getErrorColor(),
pressed:
props.level === 'warn'
? LogBoxStyle.getWarningDarkColor()
: LogBoxStyle.getErrorDarkColor(),
}}
onPress={props.disabled ? null : props.onPress}
style={headerStyles.button}>
{props.disabled ? null : (
<Image
source={{height: 16, uri: props.image, width: 16}}
style={headerStyles.buttonImage}
/>
)}
</LogBoxButton>
);
}
const headerStyles = StyleSheet.create({
button: {
alignItems: 'center',
aspectRatio: 1,
justifyContent: 'center',
marginTop: 3,
marginRight: 6,
marginLeft: 6,
marginBottom: -8,
borderRadius: 3,
},
buttonImage: {
tintColor: LogBoxStyle.getTextColor(1),
},
});
const styles = StyleSheet.create({
warn: {
backgroundColor: LogBoxStyle.getWarningColor(),
},
error: {
backgroundColor: LogBoxStyle.getErrorColor(),
},
header: {
flexDirection: 'row',
height: Platform.select({
android: 48,
ios: 44,
}),
},
title: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
titleText: {
color: LogBoxStyle.getTextColor(1),
fontSize: 16,
fontWeight: '600',
includeFontPadding: false,
lineHeight: 20,
},
});
export default LogBoxInspectorHeader;