joplin/ReactNativeClient/lib/components/action-button.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-05-16 21:05:53 +00:00
import React, { Component } from 'react';
2017-07-13 23:35:37 +00:00
import { StyleSheet, Text } from 'react-native';
2017-05-16 21:05:53 +00:00
import Icon from 'react-native-vector-icons/Ionicons';
import ReactNativeActionButton from 'react-native-action-button';
import { connect } from 'react-redux'
2017-07-30 21:04:26 +00:00
import { globalStyle } from 'lib/components/global-style.js'
2017-06-24 18:06:28 +00:00
import { Log } from 'lib/log.js'
2017-07-13 23:35:37 +00:00
import { _ } from 'lib/locale.js'
2017-05-16 21:05:53 +00:00
const styles = StyleSheet.create({
actionButtonIcon: {
fontSize: 20,
height: 22,
color: 'white',
},
2017-07-30 21:04:26 +00:00
itemText: {
// fontSize: 14, // Cannot currently set fontsize since the bow surrounding the label has a fixed size
}
2017-05-16 21:05:53 +00:00
});
class ActionButtonComponent extends React.Component {
2017-07-13 23:35:37 +00:00
constructor() {
super();
this.state = {
2017-07-15 18:21:39 +00:00
buttonIndex: 0,
2017-07-13 23:35:37 +00:00
};
}
2017-07-14 23:12:32 +00:00
componentWillReceiveProps(newProps) {
2017-07-15 18:21:39 +00:00
if ('buttonIndex' in newProps) {
this.setState({ buttonIndex: newProps.buttonIndex });
2017-07-14 23:12:32 +00:00
}
}
2017-05-24 20:51:50 +00:00
newTodo_press() {
this.props.dispatch({
type: 'NAV_GO',
2017-05-24 20:51:50 +00:00
routeName: 'Note',
noteId: null,
folderId: this.props.parentFolderId,
itemType: 'todo',
});
}
2017-05-16 21:05:53 +00:00
newNote_press() {
this.props.dispatch({
type: 'NAV_GO',
2017-05-16 21:05:53 +00:00
routeName: 'Note',
noteId: null,
folderId: this.props.parentFolderId,
2017-05-24 20:51:50 +00:00
itemType: 'note',
2017-05-16 21:05:53 +00:00
});
}
newFolder_press() {
this.props.dispatch({
type: 'NAV_GO',
2017-05-16 21:05:53 +00:00
routeName: 'Folder',
folderId: null,
});
}
render() {
2017-07-13 23:35:37 +00:00
let buttons = this.props.buttons ? this.props.buttons : [];
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
if (this.props.addFolderNoteButtons) {
if (this.props.folders.length) {
buttons.push({
title: _('New todo'),
onPress: () => { this.newTodo_press() },
color: '#9b59b6',
icon: 'md-checkbox-outline',
});
buttons.push({
title: _('New note'),
onPress: () => { this.newNote_press() },
color: '#9b59b6',
icon: 'md-document',
});
}
buttons.push({
2017-07-21 22:42:24 +00:00
title: _('New notebook'),
2017-07-13 23:35:37 +00:00
onPress: () => { this.newFolder_press() },
color: '#3498db',
icon: 'md-folder',
});
}
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
let buttonComps = [];
for (let i = 0; i < buttons.length; i++) {
let button = buttons[i];
let buttonTitle = button.title ? button.title : '';
let key = buttonTitle.replace(/\s/g, '_') + '_' + button.icon;
buttonComps.push(
<ReactNativeActionButton.Item key={key} buttonColor={button.color} title={buttonTitle} onPress={button.onPress}>
<Icon name={button.icon} style={styles.actionButtonIcon} />
2017-05-16 21:05:53 +00:00
</ReactNativeActionButton.Item>
2017-07-08 22:57:09 +00:00
);
}
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
if (!buttonComps.length && !this.props.mainButton) {
return <ReactNativeActionButton style={{ display: 'none' }}/>
}
let mainButton = this.props.mainButton ? this.props.mainButton : {};
2017-07-21 21:40:02 +00:00
let mainIcon = mainButton.icon ? <Icon name={mainButton.icon} style={styles.actionButtonIcon} /> : <Icon name="md-add" style={styles.actionButtonIcon} />
2017-07-13 23:35:37 +00:00
2017-07-15 18:21:39 +00:00
if (this.props.multiStates) {
if (!this.props.buttons || !this.props.buttons.length) throw new Error('Multi-state button requires at least one state');
2017-07-15 18:37:17 +00:00
if (this.state.buttonIndex < 0 || this.state.buttonIndex >= this.props.buttons.length) throw new Error('Button index out of bounds: ' + this.state.buttonIndex + '/' + this.props.buttons.length);
2017-07-15 18:21:39 +00:00
let button = this.props.buttons[this.state.buttonIndex];
2017-07-13 23:35:37 +00:00
let mainIcon = <Icon name={button.icon} style={styles.actionButtonIcon} />
return (
<ReactNativeActionButton
icon={mainIcon}
buttonColor="rgba(231,76,60,1)"
2017-07-15 18:21:39 +00:00
onPress={() => { button.onPress() }}
2017-07-13 23:35:37 +00:00
/>
);
} else {
return (
2017-07-30 21:04:26 +00:00
<ReactNativeActionButton textStyle={styles.itemText} icon={mainIcon} buttonColor="rgba(231,76,60,1)" onPress={ function() { } }>
2017-07-13 23:35:37 +00:00
{ buttonComps }
</ReactNativeActionButton>
);
}
2017-05-16 21:05:53 +00:00
}
}
const ActionButton = connect(
(state) => {
2017-07-08 22:57:09 +00:00
return {
folders: state.folders,
};
2017-05-16 21:05:53 +00:00
}
)(ActionButtonComponent)
export { ActionButton };