Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
Adds Native Base for Styling Components & Styles List View
Browse files Browse the repository at this point in the history
  • Loading branch information
gkemp94 committed Jun 13, 2018
1 parent c0abc4a commit 4bb3036
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 73 deletions.
159 changes: 88 additions & 71 deletions components/TwListView.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,115 @@
import React from 'react';
import PropTypes from 'prop-types';
import { StyleSheet, ScrollView, Text, TextInput, View } from 'react-native';
import { StyleSheet, SectionList, StatusBar, View } from 'react-native';
import { Header, ListItem, Body, Item, Input, Right, Icon, Text } from 'native-base';

/*
* StyleSheet
* textInput for search bar style
* textBar between Title, Notice and Location
*/
const styles = StyleSheet.create({
container: {
borderWidth: 1.0,
backgroundColor: 'rgba(247,247,247,1.0)',
padding: 40,
height: 44,
flexDirection: 'column',

},
textInput: {
height: 30,
borderWidth: 1,
borderRadius: 8,
backgroundColor: 'transparent',
borderColor: '#cecece',
marginBottom: 5,
marginTop: 15,
fontWeight: 'bold',
marginHorizontal: 4,

},
top: {
height: 25,
backgroundColor: 'transparent',
borderRadius: 4,
justifyContent: 'space-between',
flex: 2,
flexDirection: 'row',
},
title: {
height: 25,
backgroundColor: 'steelblue',
fontWeight: 'bold',

},
text1: {
height: 28,
backgroundColor: 'skyblue',

},
text2: {
backgroundColor: 'powderblue',
import { getNoticeType, groupBy, formatDate } from '../utils/helpers';

const styles = StyleSheet.create({
header: {
paddingTop: StatusBar.currentHeight,
backgroundColor: 'white',
height: StatusBar.currentHeight + 60,
},

});

// disabled for now, at least until we figure out if this
// will have state or not
/* eslint-disable react/prefer-stateless-function */
/**
* @function
* Renders each List Item
*/
const TwListItem = (item) => {
const type = getNoticeType(item.NOTICETYPE[0]);
return (
<ListItem>
<Body>
<Text>{item.TITLE.split(' - ')[0]}</Text>
<Text style={type.getStyle()}>{type.getIcon()} {type.name} </Text>
<Text note numberOfLines={1}>{item.DESCRIPTION.replace(/<\/?[^>]+(>|$)/g, '')}</Text>
</Body>
<Right>
<Text note>{formatDate(new Date(item.STARTDATE))}</Text>
</Right>
</ListItem>
);
};

/**
* @class TwListView
* display of the data in a list
* the title is in bold
* the View has a bar
*/
export class TwListView extends React.Component {
class TwListView extends React.Component {
static navigationOptions = () => ({
header: null,
});

state = {
filterString: '',
}

/**
* @function _filter
* filters items
*/
_filter = (data) => {
const { filterString } = this.state;
if (!filterString) {
return data;
}
const filterRegex = new RegExp(filterString.toLowerCase());
return data.filter(item => item.COUNTY.toLowerCase().match(filterRegex));
}

/**
* @function _prepareData
* apply filter and group
*/
_prepareData = (data) => {
const filteredData = this._filter(data);
const groupedData = groupBy(filteredData, 'COUNTY');
return Object.keys(groupedData).map(key => ({
title: key,
data: groupedData[key],
}));
}

/**
* @function _keyExtractor
* used to extract key for List
*/
_keyExtractor = item => item.OBJECTID.toString();


/**
* @function render
*/
render() {
const props = this.props.screenProps;
const { data } = this.props.screenProps;
if (!data) return (<View><Text>Loading Data...</Text></View>);
return (
<ScrollView style={styles.container}>
<TextInput
style={styles.textInput}
// onChangeText={(text) => this.filterSearch(text)}
<View style={{ flex: 1 }}>
<Header searchBar rounded style={styles.header}>
<Item>
<Icon name="ios-search" />
<Input
placeholder="Search By County"
onChangeText={value => this.setState({ filterString: value })}
/>
</Item>
</Header>
<SectionList
renderItem={({ item }) => TwListItem(item)}
renderSectionHeader={({ section: { title } }) => (
<ListItem itemDivider><Text>{ title }</Text></ListItem>
)}
sections={this._prepareData(data)}
keyExtractor={this._keyExtractor}
/>
{props.data.map(item =>
// console.log(item.TITLE);
(
<View key={item.OBJECTID}>
<View style={styles.top} />
<Text style={styles.title} >{item.TITLE} </Text>
<Text style={styles.text1} > {item.LOCATION} </Text>
<Text style={styles.text1} > {item.NOTICETYPE} </Text>
<Text style={styles.text2} > {item.DESCRIPTION} </Text>
</View>
))}
</ScrollView>
</View>
);
}
}
/* eslint-enable react/prefer-stateless-function */

TwListView.propTypes = {
screenProps: PropTypes.shape({
Expand Down Expand Up @@ -123,3 +138,5 @@ TwListView.propTypes = {
})).isRequired,
}).isRequired,
};

export { TwListView };
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
"partial-lint": "git diff --name-only master... | grep js$ | xargs ./node_modules/.bin/eslint",
"check": "npm run lint && npm run test",
"tempcheck": "npm run partial-lint && npm run test",
"test": "jest"
"test": "jest",
"watch": "jest --watch"
},
"author": "",
"license": "ISC",
"jest": {
"preset": "jest-expo"
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!native-base-shoutem-theme|react-native-easy-grid|native-base|react-native|react-navigation|expo|@expo)"
]
},
"dependencies": {
"@expo/vector-icons": "^6.3.1",
"expo": "^26.0.0",
"native-base": "^2.5.1",
"npm": "^5.8.0",
"prop-types": "^15.6.1",
"react": "16.3.0",
Expand Down
87 changes: 87 additions & 0 deletions utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';
import { StyleSheet } from 'react-native';
import { Icon } from 'native-base';

const styles = StyleSheet.create({
icon: {
fontSize: 16,
},
});

export const alertTypes = {
WATEROUTAGE: {
name: 'Water Outage',
color: 'orange',
iconName: 'water-off',
iconType: 'MaterialCommunityIcons',
},
TRAFFICDISRUPTIONS: {
name: 'Traffic Distruptions',
color: '#BF5700',
iconName: 'traffic-cone',
iconType: 'Entypo',
},
DONOTDRINK: {
name: 'Do Not Drink',
color: 'red',
iconName: 'circle-slash',
iconType: 'Octicons',
},
BOILWATERNOTICE: {
name: 'Boil Water Notice',
color: 'red',
iconName: 'kettle',
iconType: 'MaterialCommunityIcons',
},
};

/**
* @function groupBy
* groups Array by Key into object
*/
export function groupBy(xs, key) {
return xs.reduce((rvIn, x) => {
const rv = rvIn;
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}

/**
* @function getNoticeType
* return object with notice name, style and icon
*/
export function getNoticeType(type) {
if (alertTypes[type]) {
return {
name: alertTypes[type].name,
getIcon: () => (
<Icon
name={alertTypes[type].iconName}
type={alertTypes[type].iconType}
style={[styles.icon, { color: alertTypes[type].color }]}
/>
),
getStyle: () => ({
color: alertTypes[type].color,
}),
};
}
return {
name: type && type.length ? type : 'Unknown Notice Type',
getIcon: () => (
<Icon name="alert" style={[styles.icon, { color: 'black' }]} />
),
getStyle: () => ({
color: 'black',
}),
};
}

/**
* @function formatDate
* formatsDate in irish format
*/
export function formatDate(date) {
return `${date.getDate()}/${date.getMonth() + 1}/${date.getYear() - 100}`;
}

0 comments on commit 4bb3036

Please sign in to comment.