@dtdot/lego 0.17.4 → 0.17.10

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.
@@ -6,12 +6,13 @@ export declare type DragVariant = Status;
6
6
  export interface InlineCardProps extends React.HTMLAttributes<HTMLDivElement> {
7
7
  children: React.ReactNode;
8
8
  size?: InlineCardSize;
9
+ value?: string;
9
10
  gestureLeftIcon?: IconProp;
10
11
  gestureLeftVariant?: DragVariant;
11
12
  onGestureLeft?: () => void;
12
13
  }
13
14
  declare const InlineCard: {
14
- ({ children, size, onClick, gestureLeftIcon, gestureLeftVariant, onGestureLeft, }: InlineCardProps): JSX.Element;
15
+ ({ children, size, value, onClick, gestureLeftIcon, gestureLeftVariant, onGestureLeft, }: InlineCardProps): JSX.Element;
15
16
  Media: ({ children }: import("./_InlineCardMedia.component").InlineCardMediaProps) => JSX.Element;
16
17
  Content: ({ children, center }: import("./_InlineCardContent.component").InlineCardContentProps) => JSX.Element;
17
18
  Meta: ({ children }: import("./_InlineCardMeta.component").InlineCardMetaProps) => JSX.Element;
@@ -1,5 +1,5 @@
1
1
  import { motion, useMotionValue, useTransform } from 'framer-motion';
2
- import React, { useState } from 'react';
2
+ import React, { useContext, useState } from 'react';
3
3
  import styled, { useTheme } from 'styled-components';
4
4
  import useMeasure from 'react-use-measure';
5
5
  import { responsive } from '../..';
@@ -8,6 +8,8 @@ import InlineCardContent from './_InlineCardContent.component';
8
8
  import InlineCardMedia from './_InlineCardMedia.component';
9
9
  import InlineCardMeta from './_InlineCardMeta.component';
10
10
  import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
11
+ import InlineCardSelectionContext from './_InlineCardSelection.context';
12
+ import { faCheck } from '@fortawesome/free-solid-svg-icons';
11
13
  const CardWrapper = styled.div `
12
14
  position: relative;
13
15
  height: 64px;
@@ -75,7 +77,38 @@ const CardActionBackground = styled(motion.div) `
75
77
  left: 0;
76
78
  pointer-events: none;
77
79
  `;
78
- const InlineCard = ({ children, size, onClick, gestureLeftIcon, gestureLeftVariant, onGestureLeft, }) => {
80
+ const SelectChecked = styled.div `
81
+ position: absolute;
82
+ top: 0;
83
+ right: 0;
84
+
85
+ width: 0;
86
+ height: 0;
87
+
88
+ border-bottom: 48px solid transparent;
89
+ border-right: 48px solid ${(props) => props.theme.colours.statusInfo.main};
90
+ `;
91
+ const SelectIconContainer = styled.div `
92
+ position: absolute;
93
+ left: 0;
94
+ right: 0;
95
+
96
+ width: 48px;
97
+ height: 48px;
98
+
99
+ display: flex;
100
+ justify-content: center;
101
+ align-items: center;
102
+
103
+ padding-left: 16px;
104
+ padding-bottom: 16px;
105
+
106
+ color: ${(props) => props.theme.colours.statusInfo.contrast};
107
+ `;
108
+ const InlineCard = ({ children, size, value, onClick, gestureLeftIcon, gestureLeftVariant, onGestureLeft, }) => {
109
+ const { value: selectedValues, onToggle } = useContext(InlineCardSelectionContext);
110
+ const isSelectable = !!onToggle;
111
+ const isSelected = selectedValues?.length && value && selectedValues.includes(value);
79
112
  const theme = useTheme();
80
113
  const x = useMotionValue(0);
81
114
  const [ref, bounds] = useMeasure();
@@ -90,9 +123,20 @@ const InlineCard = ({ children, size, onClick, gestureLeftIcon, gestureLeftVaria
90
123
  onGestureLeft();
91
124
  }
92
125
  };
126
+ const handleClick = (e) => {
127
+ if (onClick) {
128
+ onClick(e);
129
+ }
130
+ if (onToggle && value) {
131
+ onToggle(value);
132
+ }
133
+ };
93
134
  return (React.createElement(CardWrapper, { size: size },
94
135
  React.createElement(CardActionBackground, { style: { opacity, backgroundColor: gestureLeftTheme.main } }, gestureLeftIcon && (React.createElement(FontAwesomeIcon, { style: { fontSize: '20px', color: gestureLeftTheme.contrast }, icon: gestureLeftIcon }))),
95
- React.createElement(CardOuter, { drag: onGestureLeft ? 'x' : undefined, onDragEnd: handleDragEnd, ref: ref, style: { x }, animate: { x: gestureLeftActivated ? -bounds.width : undefined, opacity: gestureLeftActivated ? 0 : undefined }, dragConstraints: { left: 0, right: 0 }, usePointer: !!onClick, onClick: onClick }, children)));
136
+ React.createElement(CardOuter, { drag: onGestureLeft ? 'x' : undefined, onDragEnd: handleDragEnd, ref: ref, style: { x }, animate: { x: gestureLeftActivated ? -bounds.width : undefined, opacity: gestureLeftActivated ? 0 : undefined }, dragConstraints: { left: 0, right: 0 }, usePointer: !!onClick || isSelectable, onClick: handleClick }, children),
137
+ isSelectable && isSelected ? (React.createElement(SelectChecked, null,
138
+ React.createElement(SelectIconContainer, null,
139
+ React.createElement(FontAwesomeIcon, { icon: faCheck })))) : null));
96
140
  };
97
141
  InlineCard.Media = InlineCardMedia;
98
142
  InlineCard.Content = InlineCardContent;
@@ -2,5 +2,6 @@
2
2
  import { Meta } from '@storybook/react/types-6-0';
3
3
  export declare const Standard: () => JSX.Element;
4
4
  export declare const WithDrag: () => JSX.Element;
5
+ export declare const WithSelection: () => JSX.Element;
5
6
  declare const _default: Meta<import("@storybook/react/types-6-0").Args>;
6
7
  export default _default;
@@ -1,6 +1,7 @@
1
- import React from 'react';
1
+ import React, { useState } from 'react';
2
2
  import { faCheckCircle, faExclamationTriangle, faInfoCircle, faTimes } from '@fortawesome/free-solid-svg-icons';
3
- import { Badge, InlineCard, InlineCardGroup } from '../..';
3
+ import { Badge, FocusLayout, Form, InlineCard, InlineCardGroup } from '../..';
4
+ import InlineCardSelection from './InlineCardSelection.component';
4
5
  export const Standard = () => (React.createElement(InlineCardGroup, null,
5
6
  React.createElement(InlineCard, null,
6
7
  React.createElement(InlineCard.Content, null, "Some content")),
@@ -14,7 +15,7 @@ export const Standard = () => (React.createElement(InlineCardGroup, null,
14
15
  React.createElement(InlineCard.Content, null, "Clickable card"),
15
16
  React.createElement(InlineCard.Meta, null,
16
17
  React.createElement(Badge, { variant: 'info' }, "Clickable")))));
17
- export const WithDrag = () => (React.createElement("div", { style: { maxWidth: '400px' } },
18
+ export const WithDrag = () => (React.createElement(FocusLayout, null,
18
19
  React.createElement(InlineCardGroup, null,
19
20
  React.createElement(InlineCard, { onGestureLeft: () => console.log('Gesture left triggered'), gestureLeftIcon: faCheckCircle, gestureLeftVariant: 'success' },
20
21
  React.createElement(InlineCard.Content, null, "A success gesture")),
@@ -24,6 +25,27 @@ export const WithDrag = () => (React.createElement("div", { style: { maxWidth: '
24
25
  React.createElement(InlineCard.Content, null, "A warning gesture")),
25
26
  React.createElement(InlineCard, { onGestureLeft: () => console.log('Gesture left triggered'), gestureLeftIcon: faTimes, gestureLeftVariant: 'danger' },
26
27
  React.createElement(InlineCard.Content, null, "A danger gesture")))));
28
+ export const WithSelection = () => {
29
+ const [value, setValue] = useState({ animals: ['lama'] });
30
+ return (React.createElement(FocusLayout, null,
31
+ React.createElement(Form, { value: value, onChange: setValue },
32
+ React.createElement(InlineCardSelection, { name: 'animals' },
33
+ React.createElement(InlineCardGroup, null,
34
+ React.createElement(InlineCard, { value: 'camel' },
35
+ React.createElement(InlineCard.Media, null,
36
+ React.createElement("img", { src: 'http://3.bp.blogspot.com/-m2w1bFhTLMs/Uyh0xlVCluI/AAAAAAAACjw/G4IhU8b0RhY/w1200-h630-p-k-nu/368692.jpg' })),
37
+ React.createElement(InlineCard.Content, null, "A cranky camel")),
38
+ React.createElement(InlineCard, { value: 'eagle' },
39
+ React.createElement(InlineCard.Media, null,
40
+ React.createElement("img", { src: 'https://www.hakaimagazine.com/wp-content/uploads/header-bald-eagle-nests.jpg' })),
41
+ React.createElement(InlineCard.Content, null, "A eager eagle")),
42
+ React.createElement(InlineCard, { value: 'lama' },
43
+ React.createElement(InlineCard.Media, null,
44
+ React.createElement("img", { src: 'https://external-preview.redd.it/3KAYF01HBUX-QKwcpo89kHPzScRoZPXZN7o02JepX8E.jpg?auto=webp&s=08bbddec975a8af76de43a33001604099fb0168d' })),
45
+ React.createElement(InlineCard.Content, null, "A lazy lama")),
46
+ React.createElement(InlineCard, { value: 'iguana' },
47
+ React.createElement(InlineCard.Content, null, "An invisible iguana")))))));
48
+ };
27
49
  export default {
28
50
  title: 'Components/InlineCard',
29
51
  component: InlineCard,
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ export interface InlineCardSelectionProps {
3
+ children: React.ReactNode;
4
+ name: string;
5
+ value?: string[];
6
+ onChange?: (value: string[]) => void;
7
+ }
8
+ declare const InlineCardSelection: ({ children, name, value: propsValue, onChange: propsOnChange, }: InlineCardSelectionProps) => JSX.Element;
9
+ export default InlineCardSelection;
@@ -0,0 +1,28 @@
1
+ import React, { useCallback } from 'react';
2
+ import useFormNode from '../Form/useFormNode.hook';
3
+ import InlineCardSelectionContext from './_InlineCardSelection.context';
4
+ const InlineCardSelection = ({ children, name, value: propsValue, onChange: propsOnChange, }) => {
5
+ const { value: contextValue, onChange: contextOnChange } = useFormNode(name);
6
+ const value = contextValue || propsValue || [];
7
+ const wrappedOnChange = useCallback((val) => {
8
+ if (propsOnChange) {
9
+ propsOnChange(val);
10
+ }
11
+ if (contextOnChange) {
12
+ contextOnChange(val);
13
+ }
14
+ }, [propsOnChange, contextOnChange]);
15
+ const handleToggle = (_value) => {
16
+ if (!value) {
17
+ return;
18
+ }
19
+ if (value.includes(_value)) {
20
+ wrappedOnChange(value.filter((val) => val !== _value));
21
+ }
22
+ else {
23
+ wrappedOnChange([...value, _value]);
24
+ }
25
+ };
26
+ return (React.createElement(InlineCardSelectionContext.Provider, { value: { value, onToggle: handleToggle } }, children));
27
+ };
28
+ export default InlineCardSelection;
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ export declare type CardSize = 'fill' | 'xs' | 'sm' | 'md' | 'lg';
3
+ interface InlineCardSelectionContextProps {
4
+ value?: string[];
5
+ onToggle?: (value: string) => void;
6
+ }
7
+ declare const InlineCardSelectionContext: import("react").Context<InlineCardSelectionContextProps>;
8
+ export default InlineCardSelectionContext;
@@ -0,0 +1,6 @@
1
+ import { createContext } from 'react';
2
+ const InlineCardSelectionContext = createContext({
3
+ value: undefined,
4
+ onToggle: undefined,
5
+ });
6
+ export default InlineCardSelectionContext;
@@ -35,7 +35,7 @@ const addVariants = {
35
35
  const defaultValue = [{ id: uuidv4(), value: '' }];
36
36
  const LiveList = ({ value: inputValue, name, onChange: propsOnChange }) => {
37
37
  const { getFocused, requestFocus } = useContext(FocusContext);
38
- const { value: contextValue, error: contextError, onChange: contextOnChange } = useFormNode(name);
38
+ const { value: contextValue, error: contextError, onChange: contextOnChange, } = useFormNode(name);
39
39
  const value = contextValue || inputValue || defaultValue;
40
40
  const wrappedOnChange = useCallback((val) => {
41
41
  if (propsOnChange) {
@@ -46,7 +46,7 @@ const LiveList = ({ value: inputValue, name, onChange: propsOnChange }) => {
46
46
  }
47
47
  }, [propsOnChange, contextOnChange]);
48
48
  useEffect(() => {
49
- if (!(value === null || value === void 0 ? void 0 : value.length)) {
49
+ if (!value?.length) {
50
50
  wrappedOnChange([{ id: uuidv4(), value: '' }]);
51
51
  }
52
52
  }, [value, wrappedOnChange]);
@@ -65,7 +65,7 @@ const LiveList = ({ value: inputValue, name, onChange: propsOnChange }) => {
65
65
  return;
66
66
  }
67
67
  const focusedValue = value.find((val) => val.id === focusedId);
68
- if (focusedValue === null || focusedValue === void 0 ? void 0 : focusedValue.value) {
68
+ if (focusedValue?.value) {
69
69
  return;
70
70
  }
71
71
  const focusedIndex = value.findIndex((val) => val.id === focusedId);
@@ -97,7 +97,7 @@ const LiveList = ({ value: inputValue, name, onChange: propsOnChange }) => {
97
97
  const newItem = { id: uuidv4(), value: '' };
98
98
  wrappedOnChange([...value, newItem]);
99
99
  };
100
- if (!(value === null || value === void 0 ? void 0 : value.length)) {
100
+ if (!value?.length) {
101
101
  return null;
102
102
  }
103
103
  return (React.createElement("div", null,
@@ -57,7 +57,7 @@ const FocusContextProvider = ({ children }) => {
57
57
  function FocusContextProviderHOC(WrappedComponent) {
58
58
  const HocComponent = (props) => {
59
59
  return (React.createElement(FocusContextProvider, null,
60
- React.createElement(WrappedComponent, Object.assign({}, props))));
60
+ React.createElement(WrappedComponent, { ...props })));
61
61
  };
62
62
  return HocComponent;
63
63
  }
@@ -11,5 +11,5 @@ const NotificationContainer = styled.div `
11
11
  }
12
12
  `;
13
13
  const Notification = (props) => (React.createElement(NotificationContainer, null,
14
- React.createElement(Alert, Object.assign({}, props))));
14
+ React.createElement(Alert, { ...props })));
15
15
  export default Notification;
@@ -30,7 +30,7 @@ const Identicon = ({ value }) => {
30
30
  const hexColours = [colours.red, colours.green, colours.blue, colours.yellow];
31
31
  const convertedColours = hexColours.map((hex) => {
32
32
  const rgb = hexToRgb(hex);
33
- return [rgb === null || rgb === void 0 ? void 0 : rgb.r, rgb === null || rgb === void 0 ? void 0 : rgb.g, rgb === null || rgb === void 0 ? void 0 : rgb.b, 255];
33
+ return [rgb?.r, rgb?.g, rgb?.b, 255];
34
34
  });
35
35
  const hash = SparkMD5.hash(value || 'unknown user');
36
36
  const colourIndex = Math.floor(Math.random() * 4);
@@ -45,7 +45,7 @@ const QrCode = ({ value }) => {
45
45
  if (hintTimeoutRef.current) {
46
46
  window.clearTimeout(hintTimeoutRef.current);
47
47
  }
48
- if (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) {
48
+ if (inputRef?.current) {
49
49
  inputRef.current.select();
50
50
  document.execCommand('copy');
51
51
  }
@@ -4,14 +4,13 @@ const useKeypress = (key, handler) => {
4
4
  useEffect(() => {
5
5
  eventListenerRef.current = (event) => {
6
6
  if (key === event.key) {
7
- handler === null || handler === void 0 ? void 0 : handler();
7
+ handler?.();
8
8
  }
9
9
  };
10
10
  }, [key, handler]);
11
11
  useEffect(() => {
12
12
  const eventListener = (event) => {
13
- var _a;
14
- (_a = eventListenerRef.current) === null || _a === void 0 ? void 0 : _a.call(eventListenerRef, event);
13
+ eventListenerRef.current?.(event);
15
14
  };
16
15
  window.addEventListener('keydown', eventListener);
17
16
  return () => {
package/build/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export { default as Notifications } from './components/Notifications/Notificatio
14
14
  export { default as ImageUpload } from './components/ImageUpload/ImageUpload.component';
15
15
  export { default as InlineCard } from './components/InlineCard/InlineCard.component';
16
16
  export { default as InlineCardGroup } from './components/InlineCard/InlineCardGroup.component';
17
+ export { default as InlineCardSelection } from './components/InlineCard/InlineCardSelection.component';
17
18
  export { default as Input } from './components/Input/Input.component';
18
19
  export { default as Heading } from './components/Heading/Heading.component';
19
20
  export { default as LiveInput } from './components/LiveInput/LiveInput.component';
package/build/index.js CHANGED
@@ -14,6 +14,7 @@ export { default as Notifications } from './components/Notifications/Notificatio
14
14
  export { default as ImageUpload } from './components/ImageUpload/ImageUpload.component';
15
15
  export { default as InlineCard } from './components/InlineCard/InlineCard.component';
16
16
  export { default as InlineCardGroup } from './components/InlineCard/InlineCardGroup.component';
17
+ export { default as InlineCardSelection } from './components/InlineCard/InlineCardSelection.component';
17
18
  export { default as Input } from './components/Input/Input.component';
18
19
  export { default as Heading } from './components/Heading/Heading.component';
19
20
  export { default as LiveInput } from './components/LiveInput/LiveInput.component';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dtdot/lego",
3
- "version": "0.17.4",
3
+ "version": "0.17.10",
4
4
  "description": "Some reusable components for building my applications",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -17,51 +17,52 @@
17
17
  "build"
18
18
  ],
19
19
  "devDependencies": {
20
- "@babel/cli": "^7.12.8",
21
- "@babel/core": "^7.12.9",
22
- "@babel/preset-react": "^7.12.7",
23
- "@babel/preset-typescript": "^7.12.7",
24
- "@storybook/addon-actions": "^6.1.10",
25
- "@storybook/addon-essentials": "^6.1.10",
26
- "@storybook/addon-links": "^6.1.10",
27
- "@storybook/react": "^6.1.10",
28
- "@types/identicon.js": "^2.3.0",
29
- "@types/qrcode": "^1.4.0",
30
- "@types/react": "^17.0.0",
31
- "@types/react-dom": "^17.0.9",
20
+ "@babel/cli": "^7.17.0",
21
+ "@babel/core": "^7.17.2",
22
+ "@babel/preset-react": "^7.16.7",
23
+ "@babel/preset-typescript": "^7.16.7",
24
+ "@storybook/addon-actions": "^6.4.19",
25
+ "@storybook/addon-essentials": "^6.4.19",
26
+ "@storybook/addon-links": "^6.4.19",
27
+ "@storybook/react": "^6.4.19",
28
+ "@types/identicon.js": "^2.3.1",
29
+ "@types/qrcode": "^1.4.2",
30
+ "@types/react": "^17.0.39",
31
+ "@types/react-dom": "^17.0.11",
32
32
  "@types/spark-md5": "^3.0.2",
33
- "@types/styled-components": "^5.1.4",
33
+ "@types/styled-components": "^5.1.22",
34
34
  "@types/uuid": "^8.3.4",
35
- "@typescript-eslint/eslint-plugin": "^4.9.1",
36
- "@typescript-eslint/parser": "^4.9.1",
37
- "babel-loader": "^8.2.2",
38
- "eslint": "^7.15.0",
39
- "eslint-config-prettier": "^7.0.0",
40
- "eslint-plugin-prettier": "^3.2.0",
41
- "eslint-plugin-react": "^7.21.5",
42
- "eslint-plugin-react-hooks": "^4.2.0",
43
- "prettier": "^2.2.1",
35
+ "@typescript-eslint/eslint-plugin": "^4.33.0",
36
+ "@typescript-eslint/parser": "^4.33.0",
37
+ "babel-loader": "^8.2.3",
38
+ "eslint": "^7.32.0",
39
+ "eslint-config-prettier": "^7.2.0",
40
+ "eslint-plugin-prettier": "^3.4.1",
41
+ "eslint-plugin-react": "^7.28.0",
42
+ "eslint-plugin-react-hooks": "^4.3.0",
43
+ "framer-motion": "^6.2.6",
44
+ "prettier": "^2.5.1",
44
45
  "prettier-eslint": "^12.0.0",
45
- "react": "^17.0.1",
46
- "react-dom": "^17.0.1",
46
+ "react": "^17.0.2",
47
+ "react-dom": "^17.0.2",
47
48
  "storybook-addon-styled-component-theme": "^1.3.0",
48
- "styled-components": "^5.2.1",
49
- "typescript": "^4.1.2"
49
+ "styled-components": "^5.3.3",
50
+ "typescript": "^4.5.5"
50
51
  },
51
52
  "peerDependencies": {
53
+ "framer-motion": "^6.2.6",
52
54
  "react": "^17.0.1",
53
55
  "react-dom": "^17.0.1",
54
56
  "styled-components": "^5.2.1"
55
57
  },
56
58
  "dependencies": {
57
59
  "@fortawesome/fontawesome-svg-core": "^1.2.32",
58
- "@fortawesome/free-solid-svg-icons": "^5.15.1",
60
+ "@fortawesome/free-solid-svg-icons": "^5.15.4",
59
61
  "@fortawesome/react-fontawesome": "^0.1.13",
60
- "framer-motion": "^4.1.17",
61
62
  "identicon.js": "^2.3.3",
62
- "qrcode": "^1.4.4",
63
+ "qrcode": "^1.5.0",
63
64
  "react-use-measure": "^2.1.1",
64
- "spark-md5": "^3.0.1",
65
+ "spark-md5": "^3.0.2",
65
66
  "uuid": "^8.3.2"
66
67
  }
67
68
  }