compose(
...functions: Array<Function>: Function,
): HigherOrderComponent
Combines multiple Higher-order components into one Higher-order component
- No props
const hoc = compose(
withToggle(),
withInputs({ email: 1, name: 1 }),
);
export default hoc(BaseComponent);
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()
}
- List of fields values (
firstName
,lastName
,email
, etc.) errors: Object
Errors listsubmitReady: boolean
- Returns true when no errorsonChange: Function
- ReturnsonChange
event handler. Receives field name and callbackonClear: Function
- Sets all values to initial state. Receives callback
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( // 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.
[propName]: Boolean
State of value[toggleName]: Function
Sets inverted[propName]
stateshow: Function
Sets[propName]
totrue
hide: Function
Sets[propName]
tofalse
const hoc = withToggle('isOpened', 'toggle', false);
export default hoc(BaseComponent);
shouldRender(
shouldRenderFunction: (props: Object) => boolean
): HigherOrderComponent
Determines the cases in which the component should render
- No props
const hoc = shouldRender(props => props.shouldRenderProperty);
export default hoc(BaseComponent);
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
offset: Object
Object with position -{ top: Number, left: Number }
const hoc = withOffset(
props => props.anchor, // anchor: '.btn' | Node
200, // Time for animation in ms
);
export default hoc(BaseComponent);
appendToBody(
className: string
): HigherOrderComponent
Creates portal for your element and append it to body
- No props
const hoc = appendToBody(
'opacity-50',
);
export default hoc(BaseComponent);
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
- No props
const hoc = withOutsideClick(
props => props.onHide, // Returns null (by default)
false, // true (by default)
[27, 13, 65],
);
export default hoc(BaseComponent);
setRoles(
gerRoles: (props: Object) => Array<string> | (props: Object) => string,
): HigherOrderComponent
Sets user roles to context. You can pass String or Array of Strings
- No props
const hoc = setRoles(
props => props.currentUser.roles, // Array or String
);
export default hoc(BaseComponent);
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
roles
Returnsroles
value from context
const hoc = withRoles(
['admin', 'member'], // Roles for comparing - String or Array of String
);
export default hoc(BaseComponent);