1.8.6 • Published 11 days ago

@start-base/react-form-elements v1.8.6

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

@start-base/react-form-elements

npm npm npm bundle size npm

Demos

For live demos of these components in action, please visit our CodeSandbox and Storybook demo pages.

Introduction

This npm package provides a set of form-related components for React. You can use these components to build forms with ease.

  • CSS variables for theming are available for all components.
  • Classnames are available for all components.
  • Built-in dark mode support.

npm.io npm.io npm.io

Installation

To install, you can use npm or yarn:

    $ npm install --save @start-base/react-form-elements

or

    $ yarn add @start-base/react-form-elements

Make sure to add css file to your app root file

import '@start-base/react-form-elements/index.css';

Usage

Each form element can be use separately. You can import them like this:

import Input from '@start-base/react-form-elements/Input';

const Page = () => {
  const [value, setValue] = useState('');

  const onChange = (e) => {
    const { name, value, type } = e.target;
    setValue(value);
  };

  return <Input name="name" label="Input" value={value} onChange={onChange} />;
};

export default Page;

or you can import all components from @start-base/react-form-elements like this:

import React, { useState } from 'react';
import {
  Input,
  Form,
  Checkbox,
  DatePicker,
} from '@start-base/react-form-elements';

const Page = () => {
  const [inputs, setInputs] = useState({});
  const onChange = (e) => {
    const { name, value, type } = e.target;

    setInputs((prevState) => ({
      ...prevState,
      [name]: type === 'checkbox' ? !prevState[name] : value,
    }));
  };

  return (
    <Form>
      <Input
        label="Username"
        name="username"
        value={inputs.username}
        onChange={onChange}
      />
      <Checkbox
        checked={inputs.agree}
        label="I understand and accept the terms and conditions and privacy policy."
        name="agree"
        onChange={onChange}
      />
      <DatePicker
        label="Date"
        name="date"
        value={inputs.date}
        onChange={onChange}
      />
    </Form>
  );
};

export default Page;

Components

Form

Input

TextArea

Checkbox

Radio

Switch

RadioGroup

PasswordInput

AmountInput

NumberInput

Select

PhoneInput

Calendar

DatePicker

MultipleDatePicker

DateRangePicker

API

The following props are available for each component:

Common Props

NameTypeDefaultDescription
namestringName of the input
labelstringnullLabel of the input
placeholderstringnullPlaceholder of the input
disableShrinkbooleanfalsePosition the label outside of the input
valuestring or numberValue of the input
onChangefunctionCallback function that is fired when the value changes.
inputClassNamestringnullClassname for input element
labelClassNamestringnullClassname for label element
errorClassNamestringnullClassname for error element
disabledbooleanfalseDisable the input
errorstring or boolean or objectnullError message to display below the input. if value is a boolean input border will be change. Also you can pass schema validator error objects.

Form

NameTypeDefaultDescription
childrenReactNodeChildren of the form

Input

NameTypeDefaultDescription
prependstring or elementnullPrepend text to the input
appendstring or elementnullAppend text to the input
appendClassNamestringnullClassname for append element
prependClassNamestringnullClassname for prepend element

TextArea

NameTypeDefaultDescription
autoGrowbooleanfalseAuto grow textarea

Checkbox

NameTypeDefaultDescription
checkedbooleanfalseChecked state of the checkbox

Radio

NameTypeDefaultDescription
checkedbooleanfalseChecked state of the radio

Switch

NameTypeDefaultDescription
checkedbooleanfalseChecked state of the switch

RadioGroup

NameTypeDefaultDescription
optionsarray[]Array of options to display
optionLabelClassNamestringnullClassname for option label

PasswordInput

NameTypeDefaultDescription
prependstring or elementnullPrepend text to the input
appendClassNamestringnullClassname for append element
prependClassNamestringnullClassname for prepend element

AmountInput

This component is a wrapper around react-currency-input-field component. All props from react-currency-input-field can be passed to this component.

NameTypeDefaultDescription
prependstring or elementnullPrepend text to the input
appendstring or elementnullAppend text to the input
appendClassNamestringnullClassname for append element
prependClassNamestringnullClassname for prepend element

NumberInput

NameTypeDefaultDescription
prependstring or elementnullPrepend text to the input
appendClassNamestringnullClassname for append element
prependClassNamestringnullClassname for prepend element

Select

This component is a wrapper around react-select component. All props from react-select can be passed to this component.

NameTypeDefaultDescription
optionsarray[]Array of options to display
classNamesobjectnullClassNames object for select components
componentsobjectnullComponents object for select

PhoneInput

This component is a wrapper around react-international-phone component. All props from react-international-phone can be passed to this component.

NameTypeDefaultDescription
defaultCountrystringusDefault country code for phone starting code and flag

Calendar

This component is a wrapper around react-day-picker component. All props from react-day-picker can be passed to this component.

NameTypeDefaultDescription
calendarClassNamestringnullClassname for calendar element

DatePicker

This component combined with Calendar component and Input component. All props from Calendar component and Input can be passed to this component.

NameTypeDefaultDescription
localeobjectnulldate-fns locale object
formatstringMM/dd/yyyyDate format
calendarClassNamestringnullClassname for calendar element

MultipleDatePicker

This component combined with Calendar component and Select component. All props from Calendar component and Select can be passed to this component.

NameTypeDefaultDescription
valuearray[]value array of MultipleDatePicker
localeobjectnulldate-fns locale object
formatstringMM/dd/yyyyDate format
calendarClassNamestringnullClassname for calendar element

DateRangePicker

This component combined with Calendar component and Select component. All props from Calendar component and Select can be passed to this component.

NameTypeDefaultDescription
valueobject""to - from object
localeobjectnulldate-fns locale object
formatstringMM/dd/yyyyDate format
calendarClassNamestringnullClassname for calendar element
numberOfMonthsinteger2Calender count to show date selection popup
separatorstring" / "Separator for dates on showing input

OTPInput

This component is a wrapper around input-otp component. All props from input-otp can be passed to this component.

NameTypeDefaultDescription
valuestringValue of the input
lengthobjectLength of the code.
timerintegernullOTP code timer limit of seconds.
onCompletefunctionCallback function that is fired when the all values entered.
onResendfunctionCallback function that is fired when the time out and cliced resend button.
formatarraynullInputs Separation array.
separatorstring"-"Separator element between inputs
resendLabelstring"Resend"Resend button text.

Styling

With CSS variables

You can use CSS variables to customize the look and feel of the components. Here's a list of all available variables:

--rfe-transparent: transparent;
--rfe-white: #fff;
--rfe-white-rgb: 255 255 255;
--rfe-black: #000;
--rfe-black-rgb: 0 0 0;
--rfe-color: #000;
--rfe-color-placeholder: #646464;
--rfe-color-error: #ff383e;
--rfe-border: #e6e6e6;
--rfe-background: #fafafa;
--rfe-background-selected: #f0f0f0;
--rfe-background-disabled: #e1e1e1;
--rfe-border-radius: 6px;
--rfe-font-size: 16px;
--rfe-placeholder-font-size: 14px;
--rfe-focused-font-size: 12px;
--rfe-error-font-size: 12px;
--rfe-input-height: 56px;
--rfe-spacing: 16px;
--rfe-label-spacing: 10px;
--rfe-focus: 0 0 10px #dcdcdc;
--rfe-font-family: 'Arial', sans-serif;

With classnames

You can use classnames to customize the look and feel of the components. Here's an example of how to use classnames:

.input {
  &:focus {
  }
}

.label {
}

.errorLabel {
}
import { forwardRef } from 'react';

import Input from '@start-base/react-form-elements/Input';

import styles from './CustomInput.module.scss';

const CustomInput = forwardRef((props, ref) => (
  <Input
    ref={ref}
    {...props}
    inputClassName={styles.input}
    labelClassName={styles.label}
    errorClassName={styles.errorLabel}
  />
));

export default CustomInput;

Examples

For more examples, please visit our CodeSandbox and Storybook demo pages.

react-hook-form

Here's an example of using the form elements with react-hook-form:

import React, { useState } from 'react';
import { useForm, Controller } from 'react-hook-form';
import {
  Input,
  Form,
  Checkbox,
  DatePicker,
} from '@start-base/react-form-elements';

const Page = () => {
  const [inputs, setInputs] = useState({});
  const { control, register, handleSubmit, watch } = useForm();
  const onSubmit = (data) => setInputs(data);

  return (
    <Form>
      <Input
        label="Username"
        value={watch('username')}
        {...register('username')}
      />
      <Checkbox
        label="I understand and accept the terms and conditions and privacy policy."
        checked={watch('policy')}
        {...register('policy')}
      />
      <Controller
        name="date"
        control={control}
        render={({ field }) => (
          <DatePicker label="Date" value={watch('date')} {...field} />
        )}
      />
      <button onClick={handleSubmit(onSubmit)}>Submit</button>
    </Form>
  );
};

export default Page;

react-hook-form with yup

Here's an example of using the form elements with react-hook-form and yup:

import React, { useState } from 'react';
import { useForm, Controller } from 'react-hook-form';
import {
  Input,
  Form,
  Checkbox,
  DatePicker,
} from '@start-base/react-form-elements';
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from 'yup';

const validationSchema = yup.object({
  username: yup.string().required('Required').label('Username'),
  date: yup.date().required('Required').label('Date'),
  policy: yup.boolean().required('Required').label('Policy'),
});

const Page = () => {
  const [inputs, setInputs] = useState({});
  const {
    formState: { errors },
    control,
    register,
    handleSubmit,
    watch,
  } = useForm({
    resolver: yupResolver(validationSchema),
  });
  const onSubmit = (data) => setInputs(data);

  return (
    <Form>
      <Input
        label="Username"
        value={watch('username')}
        {...register('username')}
        error={errors.username}
      />
      <Checkbox
        label="I understand and accept the terms and conditions and privacy policy."
        checked={watch('policy')}
        {...register('policy')}
        error={errors.policy}
      />
      <Controller
        name="date"
        control={control}
        render={({ field }) => (
          <DatePicker
            label="Date"
            value={watch('date')}
            error={errors.date}
            {...field}
          />
        )}
      />
      <button onClick={handleSubmit(onSubmit)}>Submit</button>
    </Form>
  );
};

export default Page;

Demos

For live demos of these components in action, please visit our CodeSandbox and Storybook demo pages.

npm.io

1.8.6

11 days ago

1.8.5

13 days ago

1.8.2

1 month ago

1.8.4

1 month ago

1.8.3

1 month ago

1.8.1

1 month ago

1.8.0

1 month ago

1.7.18

3 months ago

1.7.17

3 months ago

1.7.16

3 months ago

1.7.15

4 months ago

1.7.14

4 months ago

1.7.13

4 months ago

1.7.12

4 months ago

1.7.11

4 months ago

1.7.10

4 months ago

1.7.9

4 months ago

1.7.8

4 months ago

1.7.7

4 months ago

1.7.6

4 months ago

1.7.5

4 months ago

1.7.4

4 months ago

1.7.3

4 months ago

1.7.2

4 months ago

1.7.1

4 months ago

1.7.0

4 months ago

1.6.13

5 months ago

1.6.12

5 months ago

1.6.11

6 months ago

1.6.10

6 months ago

1.6.9

6 months ago

1.6.8

6 months ago

1.6.7

6 months ago

1.6.6

6 months ago

1.6.5

6 months ago

1.6.4

6 months ago

1.6.3

6 months ago

1.6.2

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.5.0

6 months ago

1.4.0

6 months ago

1.3.9

6 months ago

1.3.8

6 months ago

1.3.7

6 months ago

1.3.6

6 months ago

1.3.5

6 months ago

1.3.4

6 months ago

1.3.3

7 months ago

1.3.2

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.0

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago