Show list of results

This commit is contained in:
Jake Howard 2016-10-03 21:58:00 +01:00
parent 1c9612b3a3
commit dc7f19fa37
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 98 additions and 16 deletions

View file

@ -0,0 +1,27 @@
import React from 'react';
import zxcvbn from 'zxcvbn';
import {
Text,
View,
StyleSheet
} from 'react-native';
import _ from 'underscore';
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
} as React.ViewStyle,
});
interface Props {
data: any;
}
export default function Result(props: Props) {
return (
<Text>{props.data.key}: {JSON.stringify(props.data.value)}</Text>
);
}

View file

@ -1,10 +1,13 @@
import React, { Component }from 'react'; import React from 'react';
import zxcvbn from 'zxcvbn'; import zxcvbn from 'zxcvbn';
import { import {
Text, Text,
View, View,
StyleSheet StyleSheet,
ListView
} from 'react-native'; } from 'react-native';
import _ from 'underscore';
import ResultItem from './ResultItem';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -12,21 +15,73 @@ const styles = StyleSheet.create({
alignItems: 'center', alignItems: 'center',
justifyContent: 'center' justifyContent: 'center'
} as React.ViewStyle, } as React.ViewStyle,
listView: {
flex: 1
} as React.ViewStyle,
listItem: {
borderBottomWidth: 1,
borderBottomColor: '#333',
paddingTop: 5,
paddingBottom: 8,
paddingHorizontal: 16
} as React.ViewStyle
}); });
interface Props { interface Props {
value: string; value: string;
} }
interface State { interface State {}
} export default class Result extends React.Component<Props, State> {
dataSource: any;
constructor() {
super();
this.dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
}
parseData(results: any) {
const newResults = _.clone(results);
const hashTimes = Object.keys(newResults.crack_times_display).map(function (key) {
const value = newResults.crack_times_display[key];
return {key, value};
});
delete newResults.crack_times_display;
delete newResults.crack_times_seconds;
const out = Object.keys(newResults).map(function (key) {
const value = newResults[key];
return {key, value};
});
return _.union(out, hashTimes);
}
renderRow(rowData: any, sectionId: string, rowId: string) {
let extraRowStyles = {};
if (rowId === '0') {
extraRowStyles = {
borderTopWidth: 1,
borderTopColor: '#333'
};
}
export default function Result(props : Props) {
const resultData = zxcvbn(props.value);
return ( return (
<View style={styles.container}> <View style={[styles.listItem, extraRowStyles]}>
<Text>{resultData.score}</Text> <ResultItem data={rowData} />
</View> </View>
); );
}
render() {
const results = this.parseData(zxcvbn(this.props.value));
return (
<View style={styles.container}>
<ListView
style={styles.listView}
dataSource={this.dataSource.cloneWithRows(results)}
renderRow={this.renderRow.bind(this)}
removeClippedSubviews />
</View>
);
}
} }