custom-hoc v0.1.0
custom-hoc
Higher-order components:
compose()
compose(
...functions: Array<Function>: Function,
): HigherOrderComponentCombines 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
): HigherOrderComponentAccepts 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 listsubmitReady: boolean- Returns true when no errorsonChange: Function- ReturnsonChangeevent handler. Receives field name and callbackonClear: 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
): HigherOrderComponentPasses 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]: BooleanState of value[toggleName]: FunctionSets inverted[propName]stateshow: FunctionSets[propName]totruehide: FunctionSets[propName]tofalse
Ussage example:
const hoc = withToggle('isOpened', 'toggle', false);
export default hoc(BaseComponent);shouldRender()
shouldRender(
shouldRenderFunction: (props: Object) => boolean
): HigherOrderComponentDetermines 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
): HigherOrderComponentCalculates position of component relative to anchor (CSS selector or Node). Also recalculate new position after resizing window
Returns props:
offset: ObjectObject 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
): HigherOrderComponentCreates 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>,
): HigherOrderComponentAdds 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,
): HigherOrderComponentSets 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,
): HigherOrderComponentCompares passed roles with roles from context and checks cases when component should render.
It should be used with setRoles
Returns props:
rolesReturnsrolesvalue from context
Ussage example:
const hoc = withRoles(
['admin', 'member'], // Roles for comparing - String or Array of String
);
export default hoc(BaseComponent);8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago