@panneau/core 1.0.1-alpha.0 → 1.0.3-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/contexts.js +1 -1
  2. package/es/contexts.js +1740 -0
  3. package/es/hooks.js +1116 -0
  4. package/es/index.js +991 -9
  5. package/es/utils.js +329 -1
  6. package/hooks.js +1 -0
  7. package/lib/contexts.js +1871 -0
  8. package/lib/hooks.js +1167 -0
  9. package/lib/index.js +1003 -44
  10. package/lib/utils.js +371 -12
  11. package/package.json +79 -49
  12. package/utils.js +1 -1
  13. package/components.js +0 -1
  14. package/es/components/Button.js +0 -157
  15. package/es/components/ButtonGroup.js +0 -87
  16. package/es/components/Errors.js +0 -33
  17. package/es/components/Label.js +0 -32
  18. package/es/components/Loading.js +0 -56
  19. package/es/components/index.js +0 -6
  20. package/es/contexts/Components.js +0 -52
  21. package/es/contexts/Definition.js +0 -21
  22. package/es/contexts/UrlGenerator.js +0 -33
  23. package/es/contexts/index.js +0 -3
  24. package/es/lib/ComponentsCollection.js +0 -107
  25. package/es/lib/Definition.js +0 -134
  26. package/es/lib/PropTypes.js +0 -75
  27. package/es/lib/Resource.js +0 -93
  28. package/es/lib/ResourceApi.js +0 -139
  29. package/es/lib/UrlGenerator.js +0 -66
  30. package/es/lib/requests.js +0 -165
  31. package/es/lib/useResourceApi.js +0 -19
  32. package/es/lib/utils.js +0 -19
  33. package/es/requests.js +0 -1
  34. package/lib/components/Button.js +0 -182
  35. package/lib/components/ButtonGroup.js +0 -104
  36. package/lib/components/Errors.js +0 -49
  37. package/lib/components/Label.js +0 -47
  38. package/lib/components/Loading.js +0 -70
  39. package/lib/components/index.js +0 -47
  40. package/lib/contexts/Components.js +0 -78
  41. package/lib/contexts/Definition.js +0 -45
  42. package/lib/contexts/UrlGenerator.js +0 -53
  43. package/lib/contexts/index.js +0 -73
  44. package/lib/lib/ComponentsCollection.js +0 -117
  45. package/lib/lib/Definition.js +0 -145
  46. package/lib/lib/PropTypes.js +0 -152
  47. package/lib/lib/Resource.js +0 -104
  48. package/lib/lib/ResourceApi.js +0 -154
  49. package/lib/lib/UrlGenerator.js +0 -76
  50. package/lib/lib/requests.js +0 -206
  51. package/lib/lib/useResourceApi.js +0 -33
  52. package/lib/lib/utils.js +0 -35
  53. package/lib/requests.js +0 -17
  54. package/requests.js +0 -1
  55. package/src/components/Button.jsx +0 -186
  56. package/src/components/ButtonGroup.jsx +0 -98
  57. package/src/components/Errors.jsx +0 -46
  58. package/src/components/Label.jsx +0 -32
  59. package/src/components/Loading.jsx +0 -51
  60. package/src/components/index.js +0 -7
  61. package/src/contexts/Components.jsx +0 -49
  62. package/src/contexts/Definition.jsx +0 -21
  63. package/src/contexts/UrlGenerator.jsx +0 -35
  64. package/src/contexts/index.js +0 -12
  65. package/src/index.js +0 -8
  66. package/src/lib/ComponentsCollection.js +0 -81
  67. package/src/lib/Definition.js +0 -87
  68. package/src/lib/PropTypes.js +0 -117
  69. package/src/lib/Resource.js +0 -65
  70. package/src/lib/ResourceApi.js +0 -114
  71. package/src/lib/UrlGenerator.js +0 -55
  72. package/src/lib/requests.js +0 -120
  73. package/src/lib/useResourceApi.jsx +0 -20
  74. package/src/lib/utils.js +0 -13
  75. package/src/requests.js +0 -1
  76. package/src/styles/components/button-group.scss +0 -10
  77. package/src/styles/components/errors.scss +0 -5
  78. package/src/styles/components/loading.scss +0 -36
  79. package/src/styles/variables.scss +0 -5
  80. package/src/utils.js +0 -1
@@ -1,186 +0,0 @@
1
- /* eslint-disable jsx-a11y/anchor-is-valid, react/button-has-type, react/jsx-props-no-spreading */
2
- import React from 'react';
3
- import PropTypes from 'prop-types';
4
- import classNames from 'classnames';
5
- import isString from 'lodash/isString';
6
- import { Link } from 'react-router-dom';
7
- import { injectIntl } from 'react-intl';
8
-
9
- import Label from './Label';
10
- import * as PanneauPropTypes from '../lib/PropTypes';
11
- import { isMessage } from '../lib/utils';
12
-
13
- const propTypes = {
14
- intl: PanneauPropTypes.intl.isRequired,
15
- type: PropTypes.string,
16
- size: PanneauPropTypes.buttonSize,
17
- style: PanneauPropTypes.buttonStyle,
18
- href: PropTypes.string,
19
- target: PropTypes.string,
20
- external: PropTypes.bool,
21
- dropdown: PanneauPropTypes.dropdownItems,
22
- icon: PropTypes.string,
23
- disabled: PropTypes.bool,
24
- className: PropTypes.string,
25
- title: PanneauPropTypes.message,
26
- label: PanneauPropTypes.label,
27
- children: PanneauPropTypes.label,
28
- onClick: PropTypes.func,
29
- onItemClick: PropTypes.func,
30
- };
31
-
32
- const defaultProps = {
33
- type: 'button',
34
- size: null,
35
- style: 'primary',
36
- href: null,
37
- target: null,
38
- external: false,
39
- dropdown: null,
40
- icon: null,
41
- disabled: false,
42
- className: null,
43
- title: null,
44
- label: null,
45
- children: null,
46
- onClick: null,
47
- onItemClick: null,
48
- };
49
-
50
- const Button = ({
51
- intl,
52
- type,
53
- size,
54
- style,
55
- href,
56
- external,
57
- target,
58
- icon,
59
- dropdown,
60
- label,
61
- title,
62
- disabled,
63
- children,
64
- className,
65
- onClick,
66
- onItemClick,
67
- ...props
68
- }) => {
69
- const hasDropdown = dropdown !== null && dropdown.length > 0;
70
- const buttonProps = {
71
- className: classNames(['btn'], {
72
- [`btn-${style}`]: style !== null,
73
- [`btn-${size}`]: !hasDropdown && size !== null,
74
- 'dropdown-toggle': hasDropdown,
75
- disabled,
76
- [className]: className !== null,
77
- }),
78
- title: isMessage(title) ? intl.formatMessage(title) : title,
79
- ...props,
80
- };
81
- if (hasDropdown) {
82
- buttonProps['data-toggle'] = 'dropdown';
83
- buttonProps['aria-haspopup'] = 'true';
84
- buttonProps['aria-expanded'] = 'false';
85
- } else {
86
- buttonProps.onClick = onClick;
87
- }
88
-
89
- const finalLabel = children || label;
90
-
91
- const inner = (
92
- <>
93
- {isString(icon) ? (
94
- <span
95
- className={classNames([
96
- icon,
97
- {
98
- 'mr-2': finalLabel !== null,
99
- },
100
- ])}
101
- />
102
- ) : (
103
- icon
104
- )}
105
- {<Label>{finalLabel}</Label>}
106
- {hasDropdown ? <span className="caret" /> : null}
107
- </>
108
- );
109
-
110
- let button;
111
- if (!hasDropdown && href !== null) {
112
- button = external ? (
113
- <a
114
- href={href}
115
- {...buttonProps}
116
- target={target}
117
- aria-disabled={disabled ? 'true' : 'false'}
118
- >
119
- {inner}
120
- </a>
121
- ) : (
122
- <Link to={href} {...buttonProps} aria-disabled={disabled ? 'true' : 'false'}>
123
- {inner}
124
- </Link>
125
- );
126
- } else {
127
- button = (
128
- <button type={type} {...buttonProps} disabled={disabled}>
129
- {inner}
130
- </button>
131
- );
132
- }
133
-
134
- return hasDropdown ? (
135
- <div
136
- className={classNames([
137
- 'btn-group',
138
- {
139
- [`btn-group-${size}`]: hasDropdown && size !== null,
140
- },
141
- ])}
142
- >
143
- {button}
144
- <ul className="dropdown-menu">
145
- {dropdown.map((it, index) => {
146
- const {
147
- href: itemHref,
148
- label: itemLabel,
149
- external: itemExternal = false,
150
- target: itemTarget = null,
151
- onClick: itemOnClick = null,
152
- } = it;
153
- const itemProps = {
154
- className: 'dropdown-item',
155
- key: `dropdown-${index}`,
156
- onClick: e => {
157
- if (itemOnClick !== null) {
158
- itemOnClick(e, it, index);
159
- }
160
- if (onItemClick !== null) {
161
- onItemClick(e, it, index);
162
- }
163
- },
164
- };
165
-
166
- return itemExternal ? (
167
- <a {...itemProps} href={itemHref} target={itemTarget}>
168
- <Label>{itemLabel}</Label>
169
- </a>
170
- ) : (
171
- <Link {...itemProps} to={itemHref}>
172
- <Label>{itemLabel}</Label>
173
- </Link>
174
- );
175
- })}
176
- </ul>
177
- </div>
178
- ) : (
179
- button
180
- );
181
- };
182
-
183
- Button.propTypes = propTypes;
184
- Button.defaultProps = defaultProps;
185
-
186
- export default injectIntl(Button);
@@ -1,98 +0,0 @@
1
- /* eslint-disable react/jsx-props-no-spreading */
2
- import React from 'react';
3
- import PropTypes from 'prop-types';
4
- import classNames from 'classnames';
5
-
6
- import * as PanneauPropTypes from '../lib/PropTypes';
7
- import Button from './Button';
8
-
9
- import styles from '../styles/components/button-group.scss';
10
-
11
- const propTypes = {
12
- buttons: PanneauPropTypes.buttons,
13
- size: PanneauPropTypes.buttonSize,
14
- style: PanneauPropTypes.buttonStyle,
15
- href: PropTypes.string,
16
- onClick: PropTypes.func,
17
- noWrap: PropTypes.bool,
18
- toggle: PropTypes.bool,
19
- className: PropTypes.string,
20
- buttonClassName: PropTypes.string,
21
- };
22
-
23
- const defaultProps = {
24
- buttons: [],
25
- size: null,
26
- style: 'outline-secondary',
27
- href: null,
28
- noWrap: false,
29
- toggle: false,
30
- onClick: null,
31
- className: null,
32
- buttonClassName: null,
33
- };
34
-
35
- const ButtonGroup = ({
36
- buttons,
37
- style,
38
- size,
39
- noWrap,
40
- toggle,
41
- className,
42
- buttonClassName,
43
- onClick,
44
- ...props
45
- }) => (
46
- <div
47
- className={classNames([
48
- 'btn-group',
49
- styles.container,
50
- {
51
- [`btn-group-${size}`] : size !== null,
52
- [styles.noWrap]: noWrap,
53
- [className]: className !== null,
54
- },
55
- ])}
56
- {...(toggle
57
- ? {
58
- 'data-toggle': 'buttons',
59
- }
60
- : null)}
61
- {...props}
62
- >
63
- {buttons.map((button, index) => {
64
- const key = `button-${button.name || button.id || index}`;
65
- if (React.isValidElement(button)) {
66
- return React.cloneElement(button, {
67
- key,
68
- });
69
- }
70
- const {
71
- className: customClassName = null,
72
- style: buttonStyle = style,
73
- active = false,
74
- ...buttonProps
75
- } = button;
76
- return (
77
- <Button
78
- {...buttonProps}
79
- style={buttonStyle}
80
- key={key}
81
- className={classNames([
82
- styles.button,
83
- {
84
- 'active': active,
85
- [buttonClassName]: buttonClassName !== null,
86
- [customClassName]: customClassName !== null,
87
- },
88
- ])}
89
- />
90
- );
91
- })}
92
- </div>
93
- );
94
-
95
- ButtonGroup.propTypes = propTypes;
96
- ButtonGroup.defaultProps = defaultProps;
97
-
98
- export default ButtonGroup;
@@ -1,46 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import classNames from 'classnames';
4
- import isArray from 'lodash/isArray';
5
-
6
- import { errors as errorsPropTypes } from '../lib/PropTypes';
7
- import styles from '../styles/components/errors.scss';
8
-
9
- const propTypes = {
10
- errors: errorsPropTypes,
11
- className: PropTypes.string,
12
- };
13
-
14
- const defaultProps = {
15
- errors: [],
16
- className: null,
17
- };
18
-
19
- const Errors = ({ errors, className }) =>
20
- errors !== null && errors.length > 0 ? (
21
- <div
22
- className={classNames([
23
- 'alert',
24
- 'alert-danger',
25
- styles.container,
26
- {
27
- [className]: className !== null,
28
- },
29
- ])}
30
- >
31
- {isArray(errors) ? (
32
- <ul>
33
- {errors.map(error => (
34
- <li key={`error-${error}`}>{error}</li>
35
- ))}
36
- </ul>
37
- ) : (
38
- errors
39
- )}
40
- </div>
41
- ) : null;
42
-
43
- Errors.propTypes = propTypes;
44
- Errors.defaultProps = defaultProps;
45
-
46
- export default Errors;
@@ -1,32 +0,0 @@
1
- /* eslint-disable react/jsx-props-no-spreading */
2
- import React from 'react';
3
- import PropTypes from 'prop-types';
4
- import { FormattedMessage, FormattedHTMLMessage } from 'react-intl';
5
-
6
- import { label as labelPropTypes } from '../lib/PropTypes';
7
- import { isMessage } from '../lib/utils';
8
-
9
- const propTypes = {
10
- children: labelPropTypes.isRequired,
11
- isHtml: PropTypes.bool,
12
- values: PropTypes.object, // eslint-disable-line react/forbid-prop-types
13
- };
14
-
15
- const defaultProps = {
16
- isHtml: false,
17
- values: {},
18
- };
19
-
20
- const Label = ({ children, isHtml, values }) => {
21
- const Message = isHtml ? FormattedHTMLMessage : FormattedMessage;
22
- return isMessage(children) ? (
23
- <Message values={values} {...children} />
24
- ) : (
25
- children
26
- )
27
- };
28
-
29
- Label.propTypes = propTypes;
30
- Label.defaultProps = defaultProps;
31
-
32
- export default Label;
@@ -1,51 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import classNames from 'classnames';
4
-
5
- import styles from '../styles/components/loading.scss';
6
-
7
- const propTypes = {
8
- loading: PropTypes.bool,
9
- color: PropTypes.string,
10
- size: PropTypes.number,
11
- margin: PropTypes.string,
12
- sizeUnit: PropTypes.string,
13
- className: PropTypes.string,
14
- };
15
-
16
- const defaultProps = {
17
- loading: true,
18
- color: '#000000',
19
- size: 15,
20
- margin: '2px',
21
- sizeUnit: 'px',
22
- className: null,
23
- };
24
-
25
- const Loader = ({ loading, color, size, sizeUnit, margin, className }) => {
26
- const dotStyle = {
27
- backgroundColor: color,
28
- width: `${size}${sizeUnit}`,
29
- height: `${size}${sizeUnit}`,
30
- margin,
31
- };
32
- return loading ? (
33
- <div
34
- className={classNames([
35
- styles.component,
36
- {
37
- [className]: className !== null,
38
- },
39
- ])}
40
- >
41
- <div className={styles.dot} style={dotStyle} />
42
- <div className={styles.dot} style={dotStyle} />
43
- <div className={styles.dot} style={dotStyle} />
44
- </div>
45
- ) : null;
46
- };
47
-
48
- Loader.propTypes = propTypes;
49
- Loader.defaultProps = defaultProps;
50
-
51
- export default Loader;
@@ -1,7 +0,0 @@
1
- import Button from './Button';
2
- import ButtonGroup from './ButtonGroup';
3
- import Errors from './Errors';
4
- import Label from './Label';
5
- import Loading from './Loading';
6
-
7
- export { Button, ButtonGroup, Errors, Label, Loading };
@@ -1,49 +0,0 @@
1
- import React, { useContext, useMemo } from 'react';
2
- import PropTypes from 'prop-types';
3
- import ComponentsCollection from '../lib/ComponentsCollection';
4
-
5
- export const ComponentsContext = React.createContext(new ComponentsCollection());
6
-
7
- export const useComponents = (namespace = null, customComponents = null) => {
8
- const contextCollection = useContext(ComponentsContext);
9
- const collection = useMemo(() => {
10
- if (customComponents !== null) {
11
- return ComponentsCollection.make(customComponents);
12
- }
13
- return namespace !== null ? contextCollection.getCollection(namespace) : contextCollection;
14
- }, [namespace, customComponents, contextCollection]);
15
- return collection;
16
- };
17
-
18
- export const useComponent = (name = null, namespace = null, customComponents = null) => {
19
- const collection = useComponents(namespace, customComponents);
20
- const component = useMemo(() => collection.getComponent(
21
- name,
22
- ), [collection, name]);
23
- return component;
24
- };
25
-
26
- const propTypes = {
27
- children: PropTypes.node.isRequired,
28
- components: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object])),
29
- collection: PropTypes.instanceOf(ComponentsCollection),
30
- };
31
-
32
- const defaultProps = {
33
- components: null,
34
- collection: null,
35
- };
36
-
37
- export const ComponentsProvider = ({ children, components, collection }) => {
38
- // const value = useMemo(
39
- // () => (collection !== null ? collection : new ComponentsCollection(components)),
40
- // [components, collection],
41
- // );
42
- const value = collection !== null ? collection : new ComponentsCollection(components);
43
- return <ComponentsContext.Provider value={value}>{children}</ComponentsContext.Provider>;
44
- };
45
-
46
- ComponentsProvider.propTypes = propTypes;
47
- ComponentsProvider.defaultProps = defaultProps;
48
-
49
- export default ComponentsContext;
@@ -1,21 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import PropTypes from 'prop-types';
3
- import * as PanneauPropTypes from '../lib/PropTypes';
4
- import Definition from '../lib/Definition';
5
-
6
- export const DefinitionContext = React.createContext(new Definition());
7
-
8
- export const useDefinition = () => useContext(DefinitionContext);
9
-
10
- const propTypes = {
11
- children: PropTypes.node.isRequired,
12
- definition: PanneauPropTypes.definition.isRequired,
13
- };
14
-
15
- export const DefinitionProvider = ({ children, definition }) => (
16
- <DefinitionContext.Provider value={definition}>{children}</DefinitionContext.Provider>
17
- );
18
-
19
- DefinitionProvider.propTypes = propTypes;
20
-
21
- export default DefinitionContext;
@@ -1,35 +0,0 @@
1
- import React, { useContext, useMemo } from 'react';
2
- import PropTypes from 'prop-types';
3
- import UrlGenerator from '../lib/UrlGenerator';
4
-
5
- export const UrlGeneratorContext = React.createContext(null);
6
-
7
- export const useUrlGenerator = () => useContext(UrlGeneratorContext);
8
-
9
- const propTypes = {
10
- children: PropTypes.node.isRequired,
11
- routes: PropTypes.objectOf(PropTypes.string),
12
- urlGenerator: PropTypes.instanceOf(UrlGenerator),
13
- };
14
-
15
- const defaultProps = {
16
- routes: null,
17
- urlGenerator: null,
18
- };
19
-
20
- const UrlGeneratorProvider = ({ children, routes, urlGenerator }) => {
21
- const value = useMemo(() => (urlGenerator !== null ? urlGenerator : new UrlGenerator(routes)), [
22
- routes,
23
- urlGenerator,
24
- ]);
25
- return (
26
- <UrlGeneratorContext.Provider value={value}>{children}</UrlGeneratorContext.Provider>
27
- );
28
- };
29
-
30
- UrlGeneratorProvider.propTypes = propTypes;
31
- UrlGeneratorProvider.defaultProps = defaultProps;
32
-
33
- export { UrlGeneratorProvider };
34
-
35
- export default UrlGeneratorContext;
@@ -1,12 +0,0 @@
1
- export {
2
- default as ComponentsContext,
3
- useComponents,
4
- useComponent,
5
- ComponentsProvider,
6
- } from './Components';
7
- export { default as DefinitionContext, useDefinition, DefinitionProvider } from './Definition';
8
- export {
9
- default as UrlGeneratorContext,
10
- useUrlGenerator,
11
- UrlGeneratorProvider,
12
- } from './UrlGenerator';
package/src/index.js DELETED
@@ -1,8 +0,0 @@
1
- /** @module panneau/core */
2
- export * as PropTypes from './lib/PropTypes';
3
- export { default as ComponentsCollection } from './lib/ComponentsCollection';
4
- export { default as UrlGenerator } from './lib/UrlGenerator';
5
- export { default as Definition } from './lib/Definition';
6
- export { default as Resource } from './lib/Resource';
7
- export { default as ResourceApi } from './lib/ResourceApi';
8
- export { default as useResourceApi } from './lib/useResourceApi';
@@ -1,81 +0,0 @@
1
- class ComponentsCollection {
2
- static make(components) {
3
- return components instanceof ComponentsCollection ? components : new ComponentsCollection(components);
4
- }
5
-
6
- static normalizeKey(key) {
7
- return key.replace(/[^A-Za-z0-9.]/gi, '').toLowerCase();
8
- }
9
-
10
- constructor(components) {
11
- this.components = {};
12
- this.addComponents(components || {});
13
- }
14
-
15
- addComponent(key, Component, namespace) {
16
- const componentKey = ComponentsCollection.normalizeKey(
17
- `${typeof namespace !== 'undefined' ? `${namespace}.` : ''}${key}`,
18
- );
19
- this.components[componentKey] = Component;
20
- }
21
-
22
- addComponents(components, namespace) {
23
- const items =
24
- components instanceof ComponentsCollection ? components.getComponents() : components;
25
- Object.keys(items).forEach(key => {
26
- this.addComponent(key, items[key], namespace);
27
- });
28
- }
29
-
30
- getCollection(namespace) {
31
- const components = this.getComponents(namespace);
32
- const collection = new ComponentsCollection(components);
33
- return collection;
34
- }
35
-
36
- getComponents(namespace) {
37
- return Object.keys(this.components).reduce((components, key) => {
38
- if (typeof namespace !== 'undefined') {
39
- const namespaceKey = ComponentsCollection.normalizeKey(namespace);
40
- // eslint-disable-next-line no-useless-escape
41
- const matches = key.match(new RegExp(`^${namespaceKey}\.(.*)$`, 'i'));
42
- return matches
43
- ? {
44
- ...components,
45
- [matches[1]]: this.components[key],
46
- }
47
- : components;
48
- }
49
- return {
50
- ...components,
51
- [key]: this.components[key],
52
- };
53
- }, {});
54
- }
55
-
56
- setComponents(components, namespace) {
57
- const items =
58
- components instanceof ComponentsCollection ? components.getComponents() : components;
59
- if (typeof namespace !== 'undefined') {
60
- Object.keys(items).forEach(key => {
61
- this.components[`${namespace}.${key}`] = items[key];
62
- });
63
- } else {
64
- this.components = items;
65
- }
66
- return this;
67
- }
68
-
69
- getComponent(key = null) {
70
- if (key === null) {
71
- return null;
72
- }
73
- const normalizedKey = ComponentsCollection.normalizeKey(key);
74
- const foundKey = Object.keys(this.components).find(fieldKey => fieldKey === normalizedKey);
75
- return typeof foundKey !== 'undefined' && foundKey !== null
76
- ? this.components[foundKey]
77
- : null;
78
- }
79
- }
80
-
81
- export default ComponentsCollection;