0.1.0 • Published 6 years ago

custom-hoc v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

custom-hoc

Higher-order components:

compose()

compose(
  ...functions: Array<Function>: Function,
): HigherOrderComponent

Combines multiple Higher-order components into one Higher-order component

Returns props:
  • No props
Ussage example:
const hoc = compose(
  withToggle(),
  withInputs({ email: 1, name: 1  }),
);
 
export default hoc(BaseComponent);

withInputs()

withInputs(
  fields: Object | (props: Object) => Object
): HigherOrderComponent

Accepts object which has key (controlled input field name) and value (config) Config can contains:

{
  type: string, // allowed types: string | number | date | array
  validate: (value: string) => boolean,
  defaultValue: any, // default values: string: '', number: 0, date: new Date()
} 
Returns props:
  • List of fields values (firstName, lastName, email, etc.)
  • errors: ObjectErrors list
  • submitReady: boolean - Returns true when no errors
  • onChange: Function - Returns onChange event handler. Receives field name and callback
  • onClear: Function - Sets all values to initial state. Receives callback
Ussage example:
const hoc = withInputs({
  firstName: {
    validate: value => value.length > 0 && value.length < 25,
    defaultValue: 'Bob',
  },
  birthday: {
    type: 'date',
    validate: value => (new Date().getFullYear() - value.getFullYear()) > 18, // // greater than 15 year
  },
  age: {
    type: 'number',
    validate: value => value >= 21, // greater than 21 year
    defaultValue: 18,
  },
  hobbies: {
    type: 'array',
    validate: value => value.length > 1, // more than one hobby
  },
});
 
export default hoc(BaseComponent);

withToggle()

withToggle(              // By default
  propName: string,      // 'isOpened'
  toggleName: string,    // 'toggle'
  defaultValue: boolean, // false
): HigherOrderComponent

Passes two additional props to the base component: a state value, and a function to toggle that Boolean state value. Also you get 2 functions: show and hide for handle state.

Returns props:
  • [propName]: Boolean State of value
  • [toggleName]: Function Sets inverted [propName] state
  • show: Function Sets [propName] to true
  • hide: Function Sets [propName] to false
Ussage example:
const hoc = withToggle('isOpened', 'toggle', false);
 
export default hoc(BaseComponent);

shouldRender()

shouldRender(
  shouldRenderFunction: (props: Object) => boolean
): HigherOrderComponent

Determines the cases in which the component should render

Returns props:
  • No props
Ussage example:
const hoc = shouldRender(props => props.shouldRenderProperty);
 
export default hoc(BaseComponent);

withOffset()

withOffset(
  getAnchor: (props: Object) => string, // CSS selector or Node
  transition: number, // optional
): HigherOrderComponent

Calculates position of component relative to anchor (CSS selector or Node). Also recalculate new position after resizing window

Returns props:
  • offset: Object Object with position - { top: Number, left: Number }
Ussage example:
const hoc = withOffset(
  props => props.anchor, // anchor: '.btn' | Node
  200,                   // Time for animation in ms 
);
 
export default hoc(BaseComponent);

appendToBody()

appendToBody(
  className: string
): HigherOrderComponent

Creates portal for your element and append it to body

Returns props:
  • No props
Ussage example:
const hoc = appendToBody(
  'opacity-50',
);
 
export default hoc(BaseComponent);

withOutsideClick()

withOutsideClick(
  getOnClick: (props: Object) => Function,
  useEscape: boolean,
  additionalKeyCodes: Array<number>,
): HigherOrderComponent

Adds Event Listeners for your wrapped Component. When you click outside of Component you can fire callback. Also youcan pass additional keyCodes for firing or include "Escape" - useEscape

Returns props:
  • No props
Ussage example:
const hoc = withOutsideClick(
  props => props.onHide, // Returns null (by default)
  false,                 // true (by default)
  [27, 13, 65],
);

export default hoc(BaseComponent);

setRoles()

setRoles(
  gerRoles: (props: Object) => Array<string> | (props: Object) => string,
): HigherOrderComponent

Sets user roles to context. You can pass String or Array of Strings

Returns props:
  • No props
Ussage example:
const hoc = setRoles(
  props => props.currentUser.roles, // Array or String
);
 
export default hoc(BaseComponent);

withRoles()

withRoles(
  roles: Array<string> | string,
): HigherOrderComponent

Compares passed roles with roles from context and checks cases when component should render. It should be used with setRoles

Returns props:
  • roles Returns roles value from context
Ussage example:
const hoc = withRoles(
  ['admin', 'member'], // Roles for comparing - String or Array of String
);
 
export default hoc(BaseComponent);
0.1.0

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago