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

@merlin4/react-form-row

v0.0.4

Published

Simple React components to reduce code duplication when implementing forms.

Downloads

6

Readme

react-form-row

Simple React components to reduce code duplication when implementing forms.

Base classes pre-applied for Bootstrap 5 support.

Install

npm install @merlin4/react-form-row

InputRow component

InputRow is a stack of the following elements: <label>, <input>/<textarea>, and a <div> to display feedback.

Below is an example text input row.

// imports
import { useState } from 'react';
import { InputRow } from '@merlin4/react-form-row';

// hooks
const [submitted, setSubmitted] = useState(false);
const [title, setTitle] = useState('');

// local vars
const titleError = !title ? 'Title is required.' : '';

// render
<InputRow
  label="Title"
  id="BlogPost-Title"
  name="title"
  type="text"
  value={title}
  onChange={(evt) => setTitle(evt.currentTarget.value)}
  error={titleError}
  validated={submitted || title}
/>

Below is an example textarea row.

// imports
import { useState } from 'react';
import { InputRow } from '@merlin4/react-form-row';

// hooks
const [submitted, setSubmitted] = useState(false);
const [body, setBody] = useState('');

// local vars
const bodyError = !body ? 'Body is required.' : '';

// render
<InputRow
  label="Body"
  id="BlogPost-Body"
  name="body"
  type="textarea"
  cols="40"
  rows="7"
  value={body}
  onChange={(evt) => setBody(evt.currentTarget.value)}
  error={bodyError}
  validated={submitted || body}
/>

SelectRow

SelectRow is a stack of the following elements: <label>, <select>, and a <div> to display feedback.

Below is an example dropdown with fixed options.

// imports
import { useState } from 'react';
import { SelectRow } from '@merlin4/react-form-row';

// hooks
const [submitted, setSubmitted] = useState(false);
const [genre, setGenre] = useState('');

// local vars
const genreError = !genre ? 'Genre is required.' : '';

// render
<SelectRow
  label="Genre"
  id="NewMovie-Genre"
  name="genre"
  value={genre}
  onChange={(evt) => setGenre(evt.currentTarget.value)}
  error={genreError}
  validated={submitted || genre}
>
  <option value="">Select Genre</option>
  <option value="Action">Action</option>
  <option value="Comedy">Comedy</option>
  <option value="Romance">Romance</option>
</SelectRow>

Below is an example dropdown with dynamic options.

// imports
import { useState } from 'react';
import { map } from 'lodash';
import { SelectRow } from '@merlin4/react-form-row';

// hooks
const [submitted, setSubmitted] = useState(false);
const [userId, setUserId] = useState('');
const [users, setUsers] = useState([]);

// render
<SelectRow
  label="User"
  id="userId"
  name="userId"
  value={userId}
  onChange={(evt) => setUserId(evt.currentTarget.value)}
  error={!userId ? 'Please select a user.' : ''}
  validated={submitted || userId}
>
  <option key="" value="">
    Select User
  </option>
  {map(users, (user) => (
    <option key={user._id} value={user._id}>
      {user.fullName}
    </option>
  ))}
</SelectRow>

InputGroupRow

InputGroupRow is a simple wrapper for a Bootstrap Input Group.

Below is an example search bar.

// imports
import { useState } from 'react';
import { InputGroupRow } from '@merlin4/react-form-row';

// hooks
const [query, setQuery] = useState('');

// render
<InputGroupRow>
  <input
    className="form-control"
    aria-label="Title"
    id="query"
    name="query"
    type="search"
    value={query}
    onChange={(evt) => setQuery(evt.currentTarget.value)}
  />
  <button className="btn btn-outline-primary" type="submit" onClick={onSearch}>
    Search
  </button>
</InputGroupRow>

SubmitRow

SubmitRow is a combination of the following elements: <button>, spinner, and a success/error message. It helps support asynchronous operations by updating its from the provided promise.

Below is an example form submit row.

// imports
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import { SubmitRow } from '@merlin4/react-form-row';
import axios from 'axios';

// hooks
const [submitted, setSubmitted] = useState(false);
const [title, setTitle] = useState('');
const [body, setBody] = useState('');
const navigate = useNavigate();

// local vars
const canSubmit = auth?.payload?.permissions?.postBlog;

// handlers
async function onSubmit(evt) {
  setSubmitted(true);
  const res = await axios('http://localhost:5000/api/post/new', {
    method: 'put',
    data: { title, body },
  });
  toast.success(res.data.message);
  navigate(`/post/${res.data.postId}`);
  return res;
}

// render
<SubmitRow
  className="btn btn-primary"
  type="submit"
  disabled={!canSubmit}
  onClick={onSubmit}
/>