Socket
Book a DemoInstallSign in
Socket

@josiahayres/react-hooks

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@josiahayres/react-hooks

React Hooks

latest
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

@josiahayres/react-hooks

TypeScript

react-hooks is a collection of React Hooks, written in Typescript.

Installation

Use the package manager npm to install @josiahayres/react-hooks.

npm install @josiahayres/react-hooks

Hook: useFormSectionControl()

When you have forms with multiple sections, this hook helps by enforcing the following rules:

  • Only one section can be active at a time.
  • Optionally, can only edit one section at a time.
  • Each section

You are responsible for ensuring each Section must control

  • validation of each section, when to let user go to next section.
  • Section State: Active vs Summary & what is displaye at each point
  • Data collection from each section & what to do with the data once submitted.
  • Showing user submit button
import { useFormSectionsControl } from 'josiahayres/react-hooks';

// Create a Union type with all valid section IDs
type ValidFormSectionIds =
  | 'sectionOne'
  | 'sectionTwo'
  | 'sectionThree'
  | 'sectionFour';

// This form uses three sections.
const sectionIds = ['sectionOne', 'sectionTwo', 'sectionThree'];

// Initialize hook in component
const formSectionsControl =
  useFormSectionControl<ValidFormSectionIds>(sectionIds);

formSectionControl

This is what the hook returns. Is a tuple with the following:

const [store, dispatch, canEditSection] = formSectionsControl;

formSectionControl.store

Also accessable as formSectionsControl[0].

// Store object can be destructured like this
const { activeSectionId, haveVisitedSummary, formSections, options } = store;
StoreNotes
activeSectionIdOne of provided formSections or null
haveVisitedSummaryTrue once dispatch({type:"gotToNextSection"}) called when activeSectionId is last id in provided list
formSectionsSame as provided list of formSections, see section below for more
optionsSame as provided options object, see section below for more

store.options

This is the options object as provided to the hook on setup. All options in this object are optional.

KeyWhen key has value:Notes
initialActiveSectionIdnullhaveVisitedSummary=true, activeSectionId=null
initialActiveSectionId"sectionOne"haveVisitedSummary=false, activeSectionId="sectionOne"
initialActiveSectionIdNot providedhaveVisitedSummary=false, activeSectionId=first section in provided list

formSectionControl.dispatch

Also accessable as formSectionsControl[1]({type:""}).

Action TypeAction ParametersNotes
resetNoneWill reset internal hook state to value at initial render
goToNextSectionNoneWill step through each section in list provided, until no more sections are active.
goTosectionIdSets a specific sectionId active

Examples:

// Typescript will help dispatching the correct actions
dispatch({ type: 'goToNextSection' });
dispatch({ type: 'goTo', sectionId: 'sectionTwo' });
dispatch({ type: 'reset' });

formSectionControl.canEditSection

Function takes a sectionId and returns if that sectionId can be edited. These checks are used internally, in this order.

  • When activeSectionId === sectionId return false
  • When activeSectionId is null return true
  • When haveVisitedSummary AND activeSectionId NOT null return false
  • When position of sectionId is before position of activeSectionId return true
  • When position of sectionId is equal to or after activeSectionId return true
const sectionOneEditable = canEditSection('sectionOne');

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Keywords

React

FAQs

Package last updated on 07 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.