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

@pcs/react-form-creator-core

v0.1.4

Published

Core library for react-form-creator

Downloads

8

Readme

React Form Creator Core

The core logic for creating FormEditor and FormRenderer UI components. This library contains no styling or pre-build tools, just the core logic for allowing the placement of tools on a form via drag and drop.

Installation

NPM

npm install @pcs/react-form-creator-core

Yarn

yarn add @pcs/react-form-creator-core

Usage

Creating tools

Forms are made up of instances of tools and your FormEditor/FormRenderer components need to know how to render them.

Here is an example of a basic text field tool:

import { Tool } from '@pcs/react-form-creator-core'
import TextField, { TextFieldProps } from './TextField'

const TextFieldTool: Tool<TextFieldProps> = {
  toolType: 'text_field', // a unique tool type value
  title: 'Text Field', // The human readable name of the tool
  icon: <i>TEXT</i>, // A React element that renders an icon for the tool
  options: { // The options object represents the default options for the tool, on tool render each option is passed as a prop to the tool component
    label: 'Default Label' 
  },
  component: TextField // The component to render for an instance of this tool
}

An example TextField component could then look like:

import { useRegisterField } from '@pcs/react-form-creator-core'

export interface TextFieldProps {
  name: string // Name will always be passed to all tool components
  label: string // Passed from the options object on the tool instance
}

const TextField = (props: TextFieldProps) => {
  const fieldProps = useRegisterField(props.name) // The useRegisterField hook generates the props needed to register a field of the given name against the form

  return (
    <div>
      <label>{props.label}</label>
      <input type="text" {...fieldProps} />
    </div>
  )
}

export default TextField

Setting up an Editor

Once you have some tools in place you can create an editor that uses them, first you need to wrap your editor component in the CoreContextProvider, which you can do like so:

import { CoreContextProvider } from '@pcs/react-form-creator-core'
import { TextFieldTool, NumberFieldTool } from './tools'
import FormEditor from './FormEditor'
import PendingToolDialog from './PendingToolDialog'

const tools = [TextFieldTool, NumberFieldTool]

const initialValue = { 
  items: [{ 
    toolType: 'text_field', 
    name: 'first_field', 
    options: { label: 'First Field'}
  }, { 
    toolType: 'text_field', 
    name: 'second_field', 
    options: { label: 'Second Field'}
  }] 
}

const MyApp = () => {
  return (
    <CoreContextProvider 
      tools={tools}
      initialValue={initialValue}
      pendingToolDialog={PendingToolDialog}
    >
      <FormEditor />
    </CoreContextProvider>
  )
}

export default MyApp

The context provider holds:

  • The tools which we set up in the previous section.
  • The initial value which is the value for the form as a whole, at a minimum must contain an items array, which is a flat array of data representing the initial tool instances
  • A "pendingToolDialog" component that has the following props
interface PendingToolDialogProps {
  pendingTool: {
    tool: Tool<any>
    index?: number
    parent?: string
  } | null
  onClose: () => void
  onAdd: (
    tool: ToolInstance<any>,
    index?: number,
    parent?: string
  ) => true | string
} 

This component should display whenever there is a pending tool passed as a prop and call onAdd whenever the user has specified a name for that pending tool. If the onAdd function returns a string then that should be displayed as an error message in the dialog. This component has been left out of the core library as it leaves it up to the consumer how it is implemented / styled.

Your FormEditor component itself should look something like this:

import { 
  Form, 
  EditorWrapper, 
  ToolInstanceDraggableWrapper, 
  ToolInstanceRenderer 
} from '@pcs/react-form-creator-core'
import Toolbox from './Toolbox'

interface FormEditorProps {
  onSubmit: (value: Record<string, unknown>) => void
  onSave: (formStructure: FormStructure) => void
}

const FormEditor = (props: FormEditorProps) => {
  const { toolInstances }

  return (
    <EditorWrapper>
      <div>
        <Form onSubmit={props.onSubmit}>
          {toolInstances.map((toolInstance) => (
            <ToolInstanceDraggableWrapper toolInstance={toolInstance} key={toolInstance.name}>
              <ToolInstanceRenderer
                editMode
                toolInstance={toolInstance}
              />
            </ToolInstanceDraggableWrapper>
          ))}
        </Form>
        <ToolBox />
      </div>
    </EditorWrapper>
  )
}

export default FormEditor

You can customise this how you see fit, but the core two things you need are the form area where the tool instances are rendered and a toolbox where tools can be added to the form.

The ToolBox component can then look something like this:

import { useTools, ToolboxItemWrapper } from '@pcs/react-form-creator-core'

const Toolbox = () => {
  const { tools } = useTools()
  return <div>
    {tools.map((tool) => (
      <ToolboxItemWrapper key={tool.toolType} tool={tool}>
        <div
          {tool.icon}
          <h1>{tool.title}</h1>
        </div>
      </ToolboxItemWrapper>
    ))}
  </div>
}

export default Toolbox

Wrapping your tool item's in ToolboxItemWrapper will make each tool item draggable, then once dropped on to your form it will start the creation of a tool instance in the position it was dropped. If the tool requires a name then the previously mentioned configured PendingToolDialog will come into play, once the user has confirmed a valid name for the tool then the tool instance will be created and added to the form.

Setting up a Renderer

A Form Renderer component is the component that is used after the form has been created and is ready for data to be entered into it, setting one up can be done like so:

import { CoreContextProvider, Form, ToolInstanceRenderer, FormStructure, Tool, useTools } from '@pcs/react-form-creator-core'

interface FormRendererProps {
  tools: Tool<any>[]
  items: FormStructure['items']
  onSubmit: (data: Record<string, unknown>) => void
}

const FormRendererWrapped = (props: FormRendererProps) => {
  const { tools, items, className, onSubmit } = props
  return (
    <CoreContextProvider tools={tools} initialValue={{ items }}>
      <FormRenderer
        onSubmit={onSubmit}
      />
    </ContextProvider>
  )
}

const FormRenderer = (props: Pick<FormRendererProps, 'onSubmit'>) => {
  return (
    <Form onSubmit={props.onSubmit}>
      {toolInstances.map((toolInstance) => (
        <ToolInstanceRenderer
          key={toolInstance.name}
          toolInstance={toolInstance}
        />
      ))}
      <input type="submit">Submit</button>
    </Form>
  )
}

export default FormRendererWrapped