Skip to content

Repository files navigation

candy_wrapper

candy_wrappers are lightweight wrapper components around popular UI libraries made to work with form_props. Easily use the power of Rails forms with any supported React UI library.

Caution

This project is in its early phases of development. Its interface, behavior, and name are likely to change drastically before a major version release.

Component status

Each component are meant to be copied from this repo to your own project and customized to your liking. There are no CLI tools to help. just copy and paste from github.

Legend: ✔️ library component | 🌐 native HTML input

form_props helper Component Vanilla Mantine React Aria
f.text_field TextField ✔️ ✔️ ✔️
f.email_field EmailField ✔️ ✔️ ✔️
f.password_field PasswordField ✔️ ✔️ ✔️
f.number_field NumberField ✔️ ✔️ ✔️
f.date_field DateField ✔️ ✔️ ✔️
f.datetime_local_field DateTimeLocalField ✔️ ✔️ ✔️
f.time_field TimeField ✔️ ✔️ ✔️
f.search_field SearchField ✔️ ✔️ ✔️
f.tel_field TelField ✔️ ✔️ ✔️
f.url_field UrlField ✔️ ✔️ ✔️
f.color_field ColorField 🌐 ✔️ ✔️
f.month_field MonthField 🌐 ✔️ 🌐
f.range_field RangeField 🌐 ✔️ ✔️
f.file_field FileField 🌐 ✔️ ✔️
f.check_box Checkbox ✔️ ✔️ ✔️
f.collection_check_boxes CollectionCheckboxes ✔️ ✔️ ✔️
f.collection_radio_buttons CollectionRadioButtons ✔️ ✔️ ✔️
f.select (multiple: true supported) Select ✔️ ✔️ ✔️
f.text_area TextArea ✔️ ✔️ ✔️
f.grouped_collection_select Select ✔️ ✔️ ✔️
f.weekday_select Select ✔️ ✔️ ✔️
f.time_zone_select Select ✔️ ✔️ ✔️
f.submit SubmitButton ✔️ ✔️ ✔️

Installation

There's nothing to install, but if you need types:

npm install -D candy_wrapper

Then go to the wrapper directory in this repo and copy the wrappers for the UI library of your choice into your project.

Usage

Once you've copied the components to your project. Use form_props to build your form:

json.newPostForm do
  form_props(@post) do |f|
    f.text_field :title
    f.submit
  end
end

This would create a payload that looks something this:

{
  someForm: {
    props: {
      id: "create-post",
      action: "/posts/123",
      acceptCharset: "UTF-8",
      method: "post"
    },
    extras: {
      method: {
        name: "_method",
        type: "hidden",
        defaultValue: "patch",
        autoComplete: "off"
      },
      utf8: {
        name: "utf8",
        type: "hidden",
        defaultValue: "\u0026#x2713;",
        autoComplete: "off"
      }
      csrf: {
        name: "utf8",
        type: "authenticity_token",
        defaultValue: "SomeTOken!23$",
        autoComplete: "off"
      }
    },
    inputs: {
      title: {name: "post[title]", id: "post_title", type: "text", defaultValue: "hello"},
      submit: {type: "submit", value: "Update a Post"}
    }
  }
}

Take the payload and pass it to the wrapper:

import {Form, TextField, SubmitButton} from './copied_components_for_mantine'

const {form, extras, inputs} = newPostForm

<Form {...form} extras={extras}>
  <TextField {...inputs.title} label="Post title" />
  <SubmitButton {...inputs.submit} />
</Form>

Server errors

Each wrapper comes with inline support for server errors.

import {Form, TextField} from './copied_components'

const validationErrors = {
  full_title: "Invalid length"
}

const {form, extras, inputs} = newPostForm

<Form {...form} extras={extras} validationErrors={validationErrors}>
  <TextField {...inputs.title} label="Post title" errorKey="full_title" />
  <SubmitButton {...inputs.submit} />
</Form>

Vanilla

Vanilla wrappers wrap around basic React HTML tags. If you want to build wrappers of your own, you can start here and use other UI wrappers as reference.

Mantine

To use the Mantine wrappers, add the following libraries before copying:

yarn add @mantine/core @mantine/dates

React Aria

To use the React Aria wrappers, add the following libraries before copying:

yarn add react-aria-components @internationalized/date

Contributors

Thank you, contributors!

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

11 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages