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

@paprika/form-element

v7.0.1

Published

FormElement component displays a user input field

Downloads

7,350

Readme

@paprika/form-element

Description

FormElement component displays a user input field

Installation

yarn add @paprika/form-element

or with npm:

npm install @paprika/form-element

Props

FormElement

| Prop | Type | required | default | Description | | ----------- | -------------------------------------------------------------------------------------------- | -------- | ----------------------------- | -------------------------------------------------------------------------------------- | | children | node | true | - | FormElement sub components and layout elements. | | hasFieldSet | bool | false | false | FormElement contains multiple children so Renders a legend element instead of label. | | id | string | false | "" | id attribute for the input field DOM element (will be auto-generated if not supplied). | | isDisabled | bool | false | false | Should be disabled or not, default is false. | | isOptional | bool | false | false | If input is an optional field and should be indicated. | | isRequired | bool | false | false | If input is a required field. | | size | [ FormElement.types.size.SMALL, FormElement.types.size.MEDIUM, FormElement.types.size.LARGE] | false | FormElement.types.size.MEDIUM | Size of the label, error, help and description (font size, min-height, padding, etc). |

FormElement.Content

| Prop | Type | required | default | Description | | -------- | ----------- | -------- | ------- | --------------------------------------------------------------------------------------------- | | children | [func,node] | true | - | Input field and layout elements. May be a render function with a11yProps object and refLabel. |

a11yProps includes: { id, disabled?, "aria-disabled"?, "aria-describedby"?, "aria-required"? } refLabel is a React ref for the <FormElement.Label />|

FormElement.Description

| Prop | Type | required | default | Description | | -------- | ---- | -------- | ------- | ---------------------------------------- | | children | node | false | null | Content for the form element description |

FormElement.Error

| Prop | Type | required | default | Description | | -------- | ------ | -------- | ----------------------- | ---------------------------- | | children | node | false | null | Content of the error message | | size | custom | false | Error.types.size.MEDIUM | |

FormElement.Instructions

| Prop | Type | required | default | Description | | -------- | ---- | -------- | ------- | ----------------------------------------- | | children | node | false | null | Content for the form element instructions |

FormElement.Label

| Prop | Type | required | default | Description | | ---------------- | ----------------------------------------------------------------------------------------------------------- | -------- | ----------------------- | ----------------------------------------------------------- | | children | node | true | - | content for the label | | help | node | false | null | Help indicator | | helpA11yText | string | false | null | Aria label for icon button that triggers help popover | | isDisabled | bool | false | null | If the label should be dimmed and the help popover disabled | | isVisuallyHidden | bool | false | false | Should label be hidden | | helpAlign | [ Popover.types.align.TOP, Popover.types.align.RIGHT, Popover.types.align.BOTTOM, Popover.types.align.LEFT] | false | Popover.types.align.TOP | change tooltip alignment | | helpZIndex | number | false | null | zIndex for help tooltip |

FormElement.Layout

| Prop | Type | required | default | Description | | -------- | ---- | -------- | ------- | -------------------------- | | children | node | false | null | Content of the flex parent |

FormElement.Layout.LeftCol

| Prop | Type | required | default | Description | | -------- | --------------- | -------- | ------- | --------------------------------------------------------------------------- | | children | node | false | null | Content for left column flex child (typically the Label) | | width | [number,string] | false | "auto" | Width of the left column as number (pixels) or string (any CSS width value) |

FormElement.Layout.RightCol

| Prop | Type | required | default | Description | | -------- | ---- | -------- | ------- | --------------------------------------------------------------------------- | | children | node | false | null | Content for right column flex child (typically everything except the Label) |

Fieldset

Props are the same as FormElement but hasFieldSet=true.

Usage

The <L10n /> component is a required context provider that must wrap the <FormElement /> (or your application) for proper localization.

Using with <Input />

import Input from "@paprika/input";
import FormElement from "@paprika/form-element";

const { Label, Content } = FormElement;

<FormElement>
  <Label>First Name</Label>
  <Content>
    {a11yProps => (
      <Input
        onChange={handleChange}
        value={value}
        {...a11yProps}
      />
    )}
  </Content>
</FormElement>

Using with help popover, <Instructions />, <Description />, and <Error />

Best practices for laying out supporting content for a form input is to follow this order:

  • <Label /> first.
  • <Instructions /> above the input field contained in <Content />.
  • <Description /> or <Error /> shown below input field (but do not render both at the same time).

Note: the <Error /> component will render null if its children is falsey.

import Input from "@paprika/input";
import FormElement from "@paprika/form-element";

const { Label, Content, Instructions, Description, Error } = FormElement;
const errorMsg = "There was an error.";

<FormElement>
  <Label help="Help text">Field label</Label>
  <Instructions>Instruction text</Instructions>
  <Content>
    {a11yProps => (
      <Input hasError={Boolean(errorMsg)} {...a11yProps} />
    )}
  </Content>
  {errorMsg 
    ? <Error>{errorMsg}</Error> 
    : <Description>Description text</Description>
  }
</FormElement>

Using <Fieldset /> with <Checkbox />

Since a group of <Checkbox /> or <Radio /> components consist of multiple inputs, each with their own <label>, then need to be wrapped in a <fieldset> element with a <legend> element as a common label. This is the same for any group of form inputs that need to be grouped under a common label.

For this purpose, the <Fieldset /> component can be used. It is provided as a named import from the same @paprika/form-element package.

The <Fieldset /> component has all the same subcomponents as the <FormElement /> – it actually is just a <FormElement /> with the hasFieldSet prop set to true.

import DatePicker from "@paprika/checkbox";
import { Fieldset } from "@paprika/form-element";

const { Label, Content, Error } = Fieldset;

<Fieldset>
  <Label>Select Options</Label>
  <Content>
    {a11yProps => (
      <>
        <Checkbox onChange={handleChange}>
          <Checkbox.Input {...a11yProps} />
          Cheese
        </Checkbox>
        <Checkbox onChange={handleChange}>
          <Checkbox.Input {...a11yProps} />
          Meat
        </Checkbox>
        <Checkbox onChange={handleChange}>
          <Checkbox.Input {...a11yProps} />
          Mushrooms
        </Checkbox>
      </>
    )}
  </Content>
  <Error>{errorMsg}</Error>
</Fieldset>

Links