import * as React from 'react' import { StyleSheet, Platform } from 'react-native' import { Text } from 'native-base' import * as tinycolor from 'tinycolor2' import { AppHelper, TypeComponent } from '@ticmakers-react-native/core' import { Col, Grid, Row } from '@ticmakers-react-native/flexbox' import Image from '@ticmakers-react-native/image' import Icon from '@ticmakers-react-native/icon' import Input, { IInputProps } from '@ticmakers-react-native/input' import Button from '@ticmakers-react-native/button' import { ILoginProps, ILoginState, TypeLoginImageSource } from './index.d' import styles from './styles' /** * Class to define the Login component * @class Login * @extends {React.Component} */ export default class Login extends React.Component { /** * Reference of username input * @type {(Input | undefined)} */ public userInput: Input | undefined /** * Reference of password input * @type {(Input | undefined)} */ public passInput: Input | undefined /** * Default locale labels */ public labels = { en: { password: 'Password', username: 'Username', }, es: { password: 'Contraseña', username: 'Usuario', }, } constructor(props: ILoginProps) { super(props) this.state = { emailValue: undefined, } } /** * Method that renders the component * @returns {TypeComponent} */ public render(): TypeComponent { const { containerStyle, shadowBox } = this._processProps() const containerProps = { containerStyle: StyleSheet.flatten([styles.container, shadowBox && styles.boxShadow, containerStyle]), style: StyleSheet.flatten([styles.wrapper]), } return ( {/** Logo Container */} { this.Logo() } {/** Top Container */} { this.Top() } {/** Form Container */} { this.Form() } {/** Actions Container */} { this.Actions() } {/** Footer Container */} { this.Footer() } ) } /** * Method that renders the Logo component * @returns {TypeComponent} */ public Logo(): TypeComponent { const { LogoComponent, hideLogo, logoContainerStyle, logoSize, logoSource } = this._processProps() const containerProps = { style: StyleSheet.flatten([styles.logoContainer, logoContainerStyle]), } const imageProps = { containerStyle: StyleSheet.flatten([styles.logoImageContainer]), source: logoSource as TypeLoginImageSource, style: StyleSheet.flatten([styles.logoImage, logoSize && { height: logoSize } as any]), } if (!hideLogo) { if (AppHelper.isComponent(LogoComponent)) { return ( { LogoComponent } ) } return ( ) } } /** * Method that renders the Top component * @returns {TypeComponent} */ public Top(): TypeComponent { const { TopComponent, hideTop, topContainerStyle, title, titleStyle } = this._processProps() const containerProps = { style: StyleSheet.flatten([styles.topContainer, topContainerStyle]), } const titleProps = { style: StyleSheet.flatten([styles.topTitle, titleStyle]), } if (!hideTop) { let titleComponent let topComponent if (title) { if (AppHelper.isComponent(title) || AppHelper.isElement(title)) { titleComponent = React.cloneElement(title as any, titleProps) } else { titleComponent = title } } if (TopComponent && (AppHelper.isComponent(TopComponent) || AppHelper.isElement(TopComponent))) { topComponent = React.cloneElement(TopComponent as any) } return ( { titleComponent } { topComponent } ) } } /** * Method that renders the Form component * @returns {TypeComponent} */ public Form(): TypeComponent { const { emailValue } = this.state const { ForgotPasswordComponent, alignForgotPassword, forgotPasswordStyle, hideForm, hideForgotPassword, hideValidationIcons, lang, passwordProps, usernameProps } = this._processProps() const _lang = lang || 'en' const defaultUsernameLabel = (this.labels as any)[_lang] && (this.labels as any)[_lang].username || 'Username' const defaultPasswordLabel = (this.labels as any)[_lang] && (this.labels as any)[_lang].password || 'Password' if (usernameProps && typeof emailValue !== 'undefined') { usernameProps.value = emailValue } const inputUserProps: IInputProps = { hideValidationIcons, // tslint:disable-next-line: object-literal-sort-keys containerStyle: StyleSheet.flatten([styles.formInputContainer]), iconLeft: (usernameProps && usernameProps.icon) || { name: 'account-circle', type: 'material-community' }, label: (usernameProps && usernameProps.label) || defaultUsernameLabel, lang: _lang, onChangeText: this._onChangeEmail.bind(this), rules: (usernameProps && usernameProps.rules) || { required: true }, value: (usernameProps && usernameProps.value) || undefined, } const inputPassProps: IInputProps = { hideValidationIcons, // tslint:disable-next-line: object-literal-sort-keys containerStyle: StyleSheet.flatten([styles.formInputContainer]), iconLeft: (passwordProps && passwordProps.icon) || { name: 'key', type: 'material-community' }, label: (passwordProps && passwordProps.label) || defaultPasswordLabel, lang: _lang, password: true, rules: (passwordProps && passwordProps.rules) || { required: true }, } const forgotPasswordProps = { center: alignForgotPassword === 'center', left: alignForgotPassword === 'left', link: true, right: alignForgotPassword === 'right', style: StyleSheet.flatten([{ margin: 16 }, forgotPasswordStyle]), title: _lang === 'es' ? '¿Olvidaste la contraseña?' : 'Forgot password?', } if (!hideForm) { let forgotPassword = ) } } /** * Method that renders the Google button component * @returns {TypeComponent} */ public GoogleButton(): TypeComponent { const { GoogleComponent, google, googleIcon, googleLabel, googleStyle, lang } = this._processProps() const _lang = lang || 'en' const buttonProps = { block: true, light: true, onPress: () => this._onSubmitGoogle(), style: StyleSheet.flatten([styles.btn, googleStyle]), } const iconName = Platform.select({ android: 'google', ios: 'logo-google' }) const iconType = Platform.select({ android: 'font-awesome', ios: 'ionicon' }) let IconComponent = if (AppHelper.isComponent(googleIcon)) { IconComponent = React.cloneElement(googleIcon as any) } else if (googleIcon && Object.keys(googleIcon).length > 0) { IconComponent = } if (google) { if (AppHelper.isComponent(GoogleComponent)) { return React.cloneElement(GoogleComponent as any) } return ( ) } } /** * Method that renders the Footer component * @returns {TypeComponent} */ public Footer(): TypeComponent { const { FooterComponent, hideFooter, lang } = this._processProps() const _lang = lang || 'en' if (!hideFooter) { if (AppHelper.isComponent(FooterComponent)) { return ( { FooterComponent } ) } return ( { _lang === 'es' ? '¿No tienes una cuenta?' : 'Don\'t have an account?' }