/* eslint-disable max-classes-per-file */ import React, { Component, useState } from 'react'; import { storiesOf } from '@storybook/react'; import { boolean } from '@storybook/addon-knobs'; import styled from '@emotion/styled'; import Input, { PinInput, SearchInput, EditableInput, IntegerInput, ControlledIntegerInput, } from './index'; import Button from '../Button'; import BaseApp from '../BaseApp'; const StyledForm = styled.form` display: flex; flex-direction: column; max-width: 400px; > :not(:first-child) { margin-top: 12px; } `; const StyledCustomError = styled.ul` margin: 0; padding-inline-start: 0px; > li { margin-inline-start: 20px; } `; class InputWrapper extends Component { state = { username: '', mobile: '', phone: '', password: '', address: 'Sample Address', companyName: '', industry: '', formUsername: '', formPassword: '', }; handleChange = e => { this.setState({ [e.target.name]: e.target.value }); }; input = React.createRef(); render() { const rtl = boolean('Right-to-left', false); return (

Basic

With focusPlaceholder

With prefix

Password

Non-masked password

With error message

With custom error message

Address must be:
  • between [number] - [number] digits.
  • without spacing or special characters.
  • ) } style={{ width: 'min(100%,300px)' }} />

    Select all when clicked

    Move cursor to end of input

    Required field

    Form

    e.preventDefault()}>
    ); } } class PinInputWrapper extends Component { state = { register: ['', '', '', ''], resetPassword: ['1', '3', '3', '4'], }; render() { const rtl = boolean('Right-to-left', false); return (

    Basic

    With error message

    Small size

    ); } } class SearchInputWrapper extends Component { state = { value: '', }; onChange = e => { this.setState({ value: e.target.value }); }; onDelete = () => { this.setState({ value: '' }); }; render() { return (

    Basic

    ); } } const IntegerInputWrapper = () => (

    Basic

    ); const isValidInt = value => /^[0-9]*$/gm.test(value); const toInt = str => (str === '' ? '' : parseInt(str, 10)); const ControlledIntegerInputWrapper = () => { const MAX = 999; const MIN = 100; const STEP = 5; const [value, setValue] = useState(MIN); const onAdd = () => setValue(Math.min((toInt(value) || 0) + STEP, MAX)); const onMin = () => setValue(Math.max((toInt(value) || 0) - STEP, MIN)); const onChange = val => { const trimmedVal = val.replace(/\s+/g, ''); if (isValidInt(trimmedVal)) { const trimInt = toInt(trimmedVal); if (value !== trimInt && trimInt <= MAX) { setValue(trimInt); } } }; const onBlur = () => { if (toInt(value) < MIN || value === '') { setValue(MIN); } }; return (

    Basic

    ); }; class EditableInputWrapper extends Component { state = { isLoading: false, isSuccess: false, error: null, controlledValue: '', }; componentDidUpdate(_, prevState) { if (!prevState.isLoading && this.state.isLoading) { this.timeout = setTimeout( () => this.setState({ isLoading: false, isSuccess: true }), 1000 ); } if (!prevState.isSuccess && this.state.isSuccess) { this.timeout = setTimeout( () => this.setState({ isSuccess: false }), 3000 ); } } handleSave = () => { this.setState({ isLoading: true }); }; handleCancel = () => { this.setState({ error: null }); }; handleChange = () => { this.setState({ error: null }); }; handleError = () => { this.setState({ error: 'Email is invalid' }); }; timeout; render() { const { isLoading, isSuccess, error } = this.state; const validate = value => { // e.g. validating email const regexp = new RegExp(/^[\w-+]+(\.[\w-+]+)*@[\w-]+(\.[\w-]+)+$/i); // email pattern return regexp.test(value); }; const rtl = boolean('Right-to-left', false); return (

    Basic

    With validate function

    With hideActions prop

    Controlled

    { this.setState({ controlledValue: target.value }); }} onCancel={this.handleCancel} validate={validate} error={error} isLoading={isLoading} isSuccess={isSuccess} hideActions />
    ); } } storiesOf('Components/Input', module) .add('Input', () => ) .add('PinInput', () => ) .add('SearchInput', () => ) .add('EditableInput', () => ) .add('IntegerInput', () => ) .add('ControlledIntegerInput', () => );