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

@stackup/inputs

v0.5.0

Published

React components for creating data-driven forms.

Downloads

9

Readme

Build Version Size License

A set of helpful input components that take the burden out of building forms with React.

This library depends on @stackup/form to manage the state of form values.

Components

Checkbox

Manages the state of a boolean value using a checkbox.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<Checkbox
  label="I agree"
  help="Did you read all 7,000 lines?"
  field={useField(form, "confirmation")}
/>

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | field | FormField<boolean> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

CheckboxItem

Manages an array of values, represented by checkboxes.

In addition to the props listed below, this component accepts all props for an HTML input.

Note that this component will not render error messages. For that, you'll want to wrap your checkboxes in a FieldSet.

Example

<FieldSet legend="Sport" field={useField(form, "sport")}>
  <CheckboxItem
    label="Soccer"
    value={{ name: "Soccer" }}
    field={useField(form, "sport")}
  />
  <CheckboxItem
    label="Baseball"
    value={{ name: "Baseball" }}
    field={useField(form, "sport")}
  />
</FieldSet>

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | value | Value | ✓ | Toggle the inclusion of this value in the array. | | field | FormField<Value[]> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

FieldSet

Fields that are composed of multiple components (e.g. Radio and CheckboxItem) should always be rendered within a fieldset.

If an error is associated with the field, it will be rendered within the fieldset, rather than on a Radio or CheckboxItem.

Example

<FieldSet legend="Sport" field={useField(form, "sport")}>
  <Radio
    label="Soccer"
    value={{ name: "Soccer" }}
    field={useField(form, "sport")}
  />
  <Radio
    label="Baseball"
    value={{ name: "Baseball" }}
    field={useField(form, "sport")}
  />
</FieldSet>

Props

| Name | Type | Required | Description | | --------------- | ------------------------------------- | -------- | -------------------------------------------------- | | field | FormField<any> | ✓ | See @stackup/form | | legend | ReactNode | ✓ | Content to appear in the legend | | help | ReactNode | ✗ | Extra help info | | children | ReactNode | ✗ | Content that should appear inside the fieldset | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | className | string | ✗ | An additional class name for the fieldset | | legendClassName | string | ✗ | An additional class name for the legend | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error |

FileInput

Renders an <input type="file" />.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<FileInput label="Image" field={useField(form, "image")} />

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | field | FormField<File> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

FileListInput

Renders an <input type="file" multiple />.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<FileListInput label="Images" field={useField(form, "images")} />

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | field | FormField<FileList> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

Input

Renders an <input />, which whatever type you provide.

The value of this input will always be a string, but you should use a validation library such as @stackup/validate to parse and validate the entry before submission.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<Input type="email" label="Email" field={useField(form, "email")} />

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | type | InputType | ✗ | | | field | FormField<string> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

Radio

Renders an <input type="radio" /> and manages the state of any type of value.

Note that this component will not render error messages. For that, you'll want to wrap your radio options in a FieldSet.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<FieldSet legend="Sport" field={useField(form, "sport")}>
  <Radio
    label="Soccer"
    value={{ name: "Soccer" }}
    field={useField(form, "sport")}
  />
  <Radio
    label="Baseball"
    value={{ name: "Baseball" }}
    field={useField(form, "sport")}
  />
</FieldSet>

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | value | Value | ✓ | The value of the option to be selected | | field | FormField<Value> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLInputElement> | ✗ | A ref to the input element |

Select

Renders a <select /> tag with whatever options you provide.

You can provide a placeholder, which will render a disabled option with a blank value.

In addition to the props listed below, this component accepts all props for an HTML select.

Example

<Select
  label="Sport"
  placeholder="Choose a sport"
  field={useField(form, "sport")}
>
  <option value="baseball">Baseball</option>
  <option value="soccer">Soccer</option>
</Select>

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | placeholder | string | ✗ | | | field | FormField<string> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLSelectElement> | ✗ | A ref to the input element |

Switch

Renders a <button /> to toggle the state of a boolean value.

You can style this element with CSS by selecting the [aria-checked] value.

In addition to the props listed below, this component accepts all props for an HTML button.

Example

<Switch type="enabled" label="Show preview" field={useField(form, "enabled")} />

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | field | FormField<boolean> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLButtonElement> | ✗ | A ref to the input element |

TextArea

Renders a <textarea />.

In addition to the props listed below, this component accepts all props for an HTML input.

Example

<TextArea type="comment" label="Comment" field={useField(form, "comment")} />

Props

| Name | Type | Required | Description | | -------------- | ------------------------------------- | -------- | ------------------------------------------------------ | | field | FormField<string> | ✓ | See @stackup/form | | label | ReactNode | ✓ | Content to appear in the label | | help | ReactNode | ✗ | Extra help info that will be rendered within the label | | variant | string \| boolean \| FieldVariant[] | ✗ | Variant class name to append to all elements | | prepend | ReactNode | ✗ | Content to render before the input | | append | ReactNode | ✗ | Content to render after the input | | className | string | ✗ | An additional class name for the field | | inputClassName | string | ✗ | An additional class name for the input | | labelClassName | string | ✗ | An additional class name for the label | | helpClassName | string | ✗ | An additional class name for the help | | errorClassName | string | ✗ | An additional class name for the error | | innerRef | Ref<HTMLTextAreaElement> | ✗ | A ref to the input element |

ThemeProvider

Render a ThemeProvider at the top of your component tree to provide a global theme.

See createTheme for an example of how to define your theme.

Example

<ThemeProvider value={theme}>
  <App />
</ThemeProvider>

Utilities

concat

Concatenate multiple class names.

concat("field", touched && error && "field--invalid");

createTheme

Create a new theme. This can be used in combination with the ThemeProvider to apply class names to fields.

export const theme = createTheme({
  field: props => {
    return concat(
      "field",
      props.touched && props.error && "field--invalid",
      ...props.types.map(type => `field--${type}`),
      ...props.variants.map(variant => variant && `field--${variant}`)
    );
  }
});

formatDateTime

Convert ISO-8601 to input[type=datetime-local]. This can be used to populate the initial value for your inputs.

When given an invalid value, this function will throw an error.

formatDateTime("2020-07-08T16:30:00.000Z");

formatTime

Convert ISO-8601 to input[type=time]. This can be used to populate the initial value for your inputs.

When given an invalid value, this function will throw an error.

formatTime("08:28:00Z");

formatNumber

Convert a number to input[type=number]. This can be used to populate the initial value for your inputs.

When given an invalid value, this function will throw an error.

formatNumber(0);

parseDateTime

Convert input[type=datetime-local] to ISO-8601. This can be used to parse and validate information entered by the user.

When given an invalid value, this function will return undefined.

parseDateTime("2020-07-08T12:30");

parseTime

Convert input[type=time] to ISO-8601. This can be used to parse and validate information entered by the user.

When given an invalid value, this function will return undefined.

parseTime("04:28");

parseNumber

Convert input[type=number] to a number. This can be used to parse and validate information entered by the user.

When given an invalid value, this function will return undefined.

parseNumber("1");