utils

v0.9.6arrow_drop_down
v0.9.6
STATUS
Passing
DOWNLOADS
2,749
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
5 months ago
subject
Property types for general properties.
Like
Use utils in your project ?
Copied
npm i @bit/digiaonline.react-foundation.internal.utils
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
utils.js
199 Lines(112 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Breakpoints, FloatTypes, HorizontalAlignments, VerticalAlignments, SpaceControls, ExtendedBreakpoints } from './enums';

/**
 * Property types for general properties.
 *
 * @returns {Object}
 */
export const GeneralPropTypes = {
  showFor: PropTypes.oneOf([Breakpoints.MEDIUM, Breakpoints.LARGE]),
  showOnlyFor: PropTypes.oneOf(objectValues(Breakpoints)),
  hideFor: PropTypes.oneOf([Breakpoints.MEDIUM, Breakpoints.LARGE]),
  hideOnlyFor: PropTypes.oneOf(objectValues(Breakpoints)),
  isHidden: PropTypes.bool,
  isInvisible: PropTypes.bool,
  showForLandscape: PropTypes.bool,
  showForPortrait: PropTypes.bool,
  showForSr: PropTypes.bool,
  showOnFocus: PropTypes.bool,
  isClearfix: PropTypes.bool,
  float: PropTypes.oneOf(objectValues(FloatTypes))
};

/**
 * Creates class names from the given arguments.
 *
 * @param {*} args
 * @returns {string}
 */
export function createClassName(...args) {
  return classNames(...args);
}

/**
 * Parses the general class names from the given properties.
 *
 * @param {Object} props
 * @returns {Object}
 */
export function generalClassNames(props) {
  return {
    'show-for-medium': props.showFor === Breakpoints.MEDIUM,
    'show-for-large': props.showFor === Breakpoints.LARGE,
    'show-for-small-only': props.showOnlyFor === Breakpoints.SMALL,
    'show-for-medium-only': props.showOnlyFor === Breakpoints.MEDIUM,
    'show-for-large-only': props.showOnlyFor === Breakpoints.LARGE,
    'hide-for-medium': props.hideFor === Breakpoints.MEDIUM,
    'hide-for-large': props.hideFor === Breakpoints.LARGE,
    'hide-for-small-only': props.hideOnlyFor === Breakpoints.SMALL,
    'hide-for-medium-only': props.hideOnlyFor === Breakpoints.MEDIUM,
    'hide-for-large-only': props.hideOnlyFor === Breakpoints.LARGE,
    'hide': props.isHidden,
    'invisible': props.isInvisible,
    'show-for-landscape': props.showForLandscape,
    'show-for-portrait': props.showForPortrait,
    'show-for-sr': props.showForSr,
    'show-on-focus': props.showOnFocus,
    'clearfix': props.isClearfix,
    'float-left': props.float === FloatTypes.LEFT,
    'float-center': props.float === FloatTypes.CENTER,
    'float-right': props.float === FloatTypes.RIGHT
  };
}

/**
 * Returns the keys for the given object.
 * This method is used for getting the keys for prop types.
 *
 * @param {Object} object
 * @returns {Array}
 */
export function objectKeys(object) {
  return Object.keys(object);
}

/**
 * Returns the values for the given object.
 * This method is used for getting the values for enumerables.
 *
 * @param {Object} object
 * @returns {Array}
 */
export function objectValues(object) {
  const values = [];

  for (const property in object) {
    if (object.hasOwnProperty(property)) {
      values.push(object[property]);
    }
  }

  return values;
}

/**
 * Removes properties from the given object.
 * This method is used for removing valid attributes from component props prior to rendering.
 *
 * @param {Object} object
 * @param {Array} remove
 * @returns {Object}
 */
export function removeProps(object, remove) {
  const result = {};

  for (const property in object) {
    if (object.hasOwnProperty(property) && remove.indexOf(property) === -1) {
      result[property] = object[property];
    }
  }

  return result;
}

/**
 * Returns whether or not the given value is defined.
 *
 * @param {*} value
 * @returns {boolean}
 */
export function isDefined(value) {
  return typeof value !== 'undefined';
}

/**
 * Adds a breakpoint to a class if breakpoint is specified.
 *
 * @param {String} prop
 * @param {String} size
 * @returns {string}
 */
export function addBreakpoint(prop, size) {
  return size === 'all' ? prop : `${size}-${prop}`;
}

/**
 * Sets direction for grid and gutters (horizontal or vertical).
 *
 * @param {boolean} isVertical
 * @param {String} gutters
 * @returns {string}
 */
export function setDirection(isVertical, gutters = null) {
  if (gutters) {
    return isVertical === true ? `grid-${gutters}-y` : `grid-${gutters}-x`;
  } else {
    return isVertical === true ? 'grid-y' : 'grid-x';
  }
}

// Flexbox Utilities

/**
 * Property types for flexbox utilities.
 *
 * @returns {Object}
 */
export const FlexboxPropTypes = {
  alignX: PropTypes.oneOf(objectValues(HorizontalAlignments)),
  alignY: PropTypes.oneOf(objectValues(VerticalAlignments)),
  selfAlignX: PropTypes.oneOf(objectValues(HorizontalAlignments)),
  selfAlignY: PropTypes.oneOf(objectValues(VerticalAlignments)),
  centerAlign: PropTypes.bool,
  flexContainer: PropTypes.bool,
  flexDirRow: PropTypes.oneOf(objectValues(ExtendedBreakpoints)),
  flexDirRowRev: PropTypes.oneOf(objectValues(ExtendedBreakpoints)),
  flexDirCol: PropTypes.oneOf(objectValues(ExtendedBreakpoints)),
  flexDirColRev: PropTypes.oneOf(objectValues(ExtendedBreakpoints)),
  flexChild: PropTypes.oneOf(objectValues(SpaceControls)),
  flexOrder: PropTypes.number,
  flexOrderSmall: PropTypes.number,
  flexOrderMedium: PropTypes.number,
  flexOrderLarge: PropTypes.number,
};

/**
 * Parses the flexbox class names from the given properties.
 *
 * @param {Object} props
 * @returns {Object}
 */
export function flexboxClassNames(props) {
  return {
    'flex-container': props.flexContainer,
    'align-center-middle': props.centerAlign,
    [`align-${props.alignX}`]: props.alignX,
    [`align-${props.alignY}`]: props.alignY,
    [addBreakpoint('flex-dir-row', props.flexDirRow)]: props.flexDirRow,
    [addBreakpoint('flex-dir-row-reverse', props.flexDirRowRev)]: props.flexDirRowRev,
    [addBreakpoint('flex-dir-column', props.flexDirCol)]: props.flexDirCol,
    [addBreakpoint('flex-dir-column-reverse', props.flexDirColRev)]: props.flexDirColRev,
    [`flex-child-${props.flexChild}`]: props.flexChild,
    [`order-${props.flexOrder}`]: props.flexOrder,
    [`small-order-${props.flexOrder}`]: props.flexOrderSmall,
    [`medium-order-${props.flexOrder}`]: props.flexOrderMedium,
    [`large-order-${props.flexOrder}`]: props.flexOrderLarge
  };
}