Coder Social home page Coder Social logo

Comments (1)

VitorLuizC avatar VitorLuizC commented on August 15, 2024

A TypeScript example

import React, { Component } from 'react';
import { Button, Container, Entry } from '../components';
import { observer } from 'mobx-react';
import FormStore, { Validators } from 'mobx-valite-form-store';

export type LoginEntries = {
  username: string;
  password: string;
};

export type LoginFormProps = {
  passwordRecoveryURL: string;
  onLogin: (entries: LoginEntries) => void;
};

const validators: Validators<LoginEntries> = {
  username: [
    (value: string) => !!value.trim() || 'Nome de usuário vazio.',
    (value: string) => value.length > 18 || 'Nome de usuário maior que o permitido.'
  ],
  password: [
    (value: string) => !!value.trim() || 'Senha vazia.',
    (value: string) => value !== '123456' || 'Senha de usuário muito fácil, use outra.'
  ]
};

@observer
export class LoginForm extends Component<LoginFormProps> {
  store: FormStore<LoginEntries> = new FormStore({}, validators);

  onPress = async () => {
    await this.store.validateEntries();
    if (this.store.isValid && !this.store.isLoading)
      return;
    this.props.onLogin(this.store.entries);
  };

  render () {
    return (
      <Container>
        <Entry
          label="Nome de Usuário"
          value={ this.store.entries.username }
          message={ this.store.errors.username }
          onChange={ (value) => this.store.setEntry('username', value) }
        />
      
        <Entry
          label="Senha"
          value={ this.store.entries.password }
          message={ this.store.errors.password }
          onChange={ (value) => this.store.setEntry('password', value) }
        />

        <Link to={ this.props.passwordRecoveryURL }>Esqueceu sua senha?</Link>

        <Button onPress={ this.onPress } isDisabled={ !this.store.isValid || this.store.isLoading }>
          Entrar
        </Button>
      </Container>
    );
  }
}

from mobx-valite-form-store.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.