npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-keyrus-formol

v0.7.1

Published

A reusable form package with Zod, Tailwind, and React Hook Form

Downloads

2

Readme

react-keyrus-formol

Table of Contents


Introduction

react-keyrus-formol is a React library designed to simplify form management in web applications. It provides a variety of components that make it easier to create powerful and customizable forms. One of the key advantages of this library is its flexibility in styling. You can choose to use CSS frameworks like Tailwind or go for traditional CSS depending on your project requirements.


Installation

npm install react-keyrus-formol

Or if you're using Yarn:

```bash
yarn add react-keyrus-formol
```

Usage

Form

The Form component is the wrapper for your form elements.

import { Form } from 'react-keyrus-formol';

const onSubmit=(data)=> console.log(data)

// need Button component same function in props

<Form onSubmit={onSubmit}>{/* Your form components */}</Form>;


<Button
type="submit"
name='submit'
onClick={onSubmit}
/>

FormContext

The FormContext provides the required context for form components.

import { FormContext } from 'react-keyrus-formol';

<FormContext.Provider value={{ ...methods }}>
	<form onSubmit={methods.handleSubmit(handleSubmit)}>
		{/* Your form components */}
	</form>
</FormContext.Provider>;

Input

The Input component is used for input fields.

Props

  • name: String (required)
  • label: String (required)
  • id: String (required)
  • type: String
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Input } from 'react-keyrus-formol';

<Input
	name='age'
	label='Age'
	id='age'
	type='number'
	validationOptions={{
		required: { value: true, message: 'Age is required' },
	}}
	styleClasses={{
		width: 'w-1/2',
		size: 'text-lg',
		color: 'text-blue-700',
	}}
/>;

Select

The Select component is used for dropdown selection.

Props

  • name: String (required)
  • options: Array of Strings (required)
  • label: String (required)
  • id: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Select } from 'react-keyrus-formol';

<Select
	name='choice'
	label='Choice'
	id='choice'
	options={['Option 1', 'Option 2', 'Option 3']}
	validationOptions={{
		required: { value: true, message: 'A choice is required' },
	}}
	styleClasses={{
		width: 'w-1/3',
		size: 'text-lg',
		color: 'text-red-700',
	}}
/>;

Button

The Button component is used for buttons.

Props

  • type: String (required)
  • name: String (required)
  • onClick: Function (required)
  • onSuccess: Function
  • onError: Function
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Button } from 'react-keyrus-formol';

<Button
	type='submit'
	name='Submit'
	onClick={onSubmit}
	onSuccess={onSuccess}
	onError={onError}
	styleClasses={{
		button: 'bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 rounded',
	}}
/>;

ErrorMessage

The ErrorMessage component is used for displaying error messages.

Props

  • name: String (required)
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { ErrorMessage } from 'react-keyrus-formol';

<ErrorMessage
	name='age'
	styleClasses={{
		paragraph: 'text-red-600 text-sm bold',
	}}
/>;

RadioGroup

The RadioGroup component is used for grouping radio buttons.

Props

  • name: String (required)
  • options: Array of Objects (required)
  • label: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { RadioGroup } from 'react-keyrus-formol';

<RadioGroup
	name='gender'
	label='Gender'
	options={[
		{ label: 'Male', value: 'male' },
		{ label: 'Female', value: 'female' },
	]}
	validationOptions={{
		required: { value: true, message: 'Gender is required' },
	}}
	styleClasses={{
		input: 'h-4 w-4',
		label: 'text-xs',
		fieldset: 'border p-2',
		legend: 'text-lg',
	}}
/>;

Radio

The Radio component is used for individual radio buttons.

Props

  • name: String (required)
  • label: String (required)
  • value: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Radio } from 'react-keyrus-formol';

<Radio
	name='gender'
	label='Male'
	value='male'
	validationOptions={{
		required: { value: true, message: 'Gender is required' },
	}}
	styleClasses={{
		input: 'text-red-500 h-6 w-6',
		label: 'text-xl',
		div: 'border p-2',
	}}
/>;

DatePicker

DatePicker component is used for date picking.

Props

  • name: String (required)
  • label: String (required)
  • id: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { DatePicker } from 'react-keyrus-formol';

<DatePicker
	name='birthdate'
	label='Birthdate'
	id='birthdate'
	validationOptions={{
		required: { value: true, message: 'Date is required' },
	}}
	styleClasses={{
		input: 'text-sm',
		label: 'text-lg',
		div: 'border p-4',
	}}
/>;

Checkbox

The Checkbox component is used for checkboxes.

Props

  • name: String (required)
  • label: String (required)
  • id: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Checkbox } from 'react-keyrus-formol';

<Checkbox
	name='acceptTerms'
	id='acceptTerms'
	label='Accept Terms and Conditions'
	validationOptions={{
		required: { value: true, message: 'Agreement is required' },
	}}
	styleClasses={{
		input: 'h-4 w-4',
		label: 'text-xs',
		div: 'border p-2',
	}}
/>;

Textarea

The Textarea component is used for multiline text input.

Props

  • name: String (required)
  • label: String (required)
  • id: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { Textarea } from 'react-keyrus-formol';

<Textarea
	name='description'
	label='Description'
	id='description'
	validationOptions={{
		required: { value: true, message: 'Description is required' },
	}}
	styleClasses={{
		input: 'text-sm',
		label: 'text-lg',
		div: 'border p-4',
	}}
/>;

FileUpload

The FileUpload component is used for file uploads.

Props

  • name: String (required)
  • label: String (required)
  • id: String (required)
  • validationOptions: Object
  • styleClasses: Object (optional) - Custom CSS classes for styling.
import { FileUpload } from 'react-keyrus-formol';

<FileUpload
	name='file'
	label='Upload File'
	id='file'
	validationOptions={{
		required: { value: true, message: 'File is required' },
	}}
	styleClasses={{
		input: 'text-sm',
		label: 'text-lg',
		div: 'border p-4',
	}}
/>;

Examples

Here's a complete example using all of the components:

import {
	Input,
	TextArea,
	Form,
	Select,
	Button,
	RadioGroup,
	Radio,
	Checkbox,
	FileUpload,
	DatePicker,
	ErrorMessage,
} from './components';

function App() {
	const initialData = {
		age: 0,
		name: '',
		choice: 'Option 1',
		description: '',
		gender: '',
		acceptTerms: false,
		file: null,
	};

	const onSubmit = (data) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log(data);
				resolve();
			}, 2000);
		});
	};

	const onSuccess = () => {
		console.log('Données soumises avec succès');
	};

	const onError = (error) => {
		console.log('Erreur lors de la soumission des données', error);
	};

	return (
		<Form
			onSubmit={onSubmit}
			defaultValues={initialData}
			defaultErrorStyle='text-red-600 text-sm'>
			<Input
				name='name'
				label='Name'
				id='name'
				type='text'
				validationOptions={{
					required: { value: true, message: 'Le nom est requis' },
				}}
				styleClasses={{
					width: 'w-1/2',
					size: 'text-lg',
					color: 'text-blue-700',
					other: 'focus:border-blue-500',
				}}
			/>

			<Input
				name='age'
				label='Age'
				id='age'
				type='number'
				validationOptions={{
					required: { value: true, message: "L'âge est requis" },
					min: { value: 1, message: "L'âge doit être au moins de 1" },
					max: {
						value: 150,
						message: "L'âge doit être au maximum de 150",
					},
				}}
				styleClasses={{
					other: 'focus:border-green-500',
				}}
			/>
			<Input
				name='name'
				label='Name'
				id='name'
				type='text'
				validationOptions={{
					required: { value: true, message: 'Le nom est requis' },
				}}
			/>

			<Select
				styleClasses={{
					width: 'w-1/3',
					size: 'text-lg',
					color: 'text-red-700',
					other: 'focus:border-red-500',
				}}
				name='gender'
				options={['Male', 'Female']}
				label='Gender'
				id='gender'
				validationOptions={{ required: true }}
			/>
			<TextArea
				styleClasses={{
					width: 'w-1/2',
					size: 'text-sm',
					color: 'text-blue-700',
					other: 'focus:border-blue-500',
				}}
				name='message'
				label='Message'
				id='message'
				rows={4}
				cols={50}
				validationOptions={{
					required: { value: true, message: 'Le message est requis' },
				}}
			/>

			<RadioGroup
				styleClasses={{
					input: 'h-4 w-4',
					label: 'text-xs',
					fieldset: 'border p-2',
					legend: 'text-lg',
				}}
				name='choice'
				label='Your choice'
				options={[
					{ value: 'yes', label: 'Yes' },
					{ value: 'no', label: 'No' },
				]}
				validationOptions={{
					required: { value: true, message: 'A choice is required' },
				}}
			/>

			<Checkbox
				styleClasses={{
					input: 'h-4 w-4',
					label: 'text-xs',
					div: 'border p-2',
				}}
				name='subscribe'
				label='Subscribe to newsletter'
				id='subscribe'
				validationOptions={{
					required: {
						value: true,
						message: 'Subscription is required',
					},
				}}
			/>

			<FileUpload
				styleClasses={{
					input: 'text-sm',
					label: 'text-lg',
					div: 'border p-4',
				}}
				name='file'
				label='Upload your file'
				id='file'
				validationOptions={{
					required: { value: true, message: 'File is required' },
				}}
			/>
			<DatePicker
				styleClasses={{
					input: 'text-sm',
					label: 'text-lg',
					div: 'border p-4',
				}}
				name='date'
				label='Select a date'
				id='date'
				validationOptions={{
					required: { value: true, message: 'Date is required' },
				}}
			/>
			<Radio
				styleClasses={{
					input: 'text-red-500 h-6 w-6',
					label: 'text-xl',
					div: 'border p-2',
				}}
				name='gender'
				label='Male'
				id='male'
				value='male'
				validationOptions={{
					required: { value: true, message: 'Gender is required' },
				}}
			/>
			<ErrorMessage
				name='username'
				styleClasses={{
					paragraph: 'text-blue-600 text-sm bold',
				}}
			/>

			<Button
				type='submit'
				name='Envoyer'
				onClick={onSubmit}
				onSuccess={onSuccess}
				onError={onError}
				styleClasses={{
					button: 'bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 rounded',
				}}
			/>
		</Form>
	);
}

export default App;

For more details on changes for each version, see the CHANGELOG.

Contributors

If you would like to contribute, please follow the Contribution Guidelines.


License

MIT License. See LICENSE for more information.