@rpg-engine/long-bow 0.5.65 → 0.5.67

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpg-engine/long-bow",
3
- "version": "0.5.65",
3
+ "version": "0.5.67",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -83,7 +83,7 @@
83
83
  },
84
84
  "dependencies": {
85
85
  "@rollup/plugin-image": "^2.1.1",
86
- "@rpg-engine/shared": "^0.9.14",
86
+ "@rpg-engine/shared": "^0.9.19",
87
87
  "dayjs": "^1.11.2",
88
88
  "font-awesome": "^4.7.0",
89
89
  "fs-extra": "^10.1.0",
@@ -94,6 +94,7 @@
94
94
  "react-draggable": "^4.4.5",
95
95
  "react-error-boundary": "^3.1.4",
96
96
  "react-icons": "^4.7.1",
97
+ "react-spinners": "^0.13.8",
97
98
  "rollup-plugin-image-files": "^1.4.2",
98
99
  "rpgui": "^1.0.3"
99
100
  }
Binary file
Binary file
@@ -0,0 +1,127 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { BeatLoader } from 'react-spinners';
3
+ import styled from 'styled-components';
4
+ import { v4 as uuidv4 } from 'uuid';
5
+ import { uiColors } from '../constants/uiColors';
6
+
7
+ export interface IAsyncDropdownOptionsProps {
8
+ id: number;
9
+ value: string;
10
+ option: string | JSX.Element;
11
+ }
12
+
13
+ export interface IAsyncDropdownProps {
14
+ options: IAsyncDropdownOptionsProps[];
15
+ width?: string;
16
+ onChange: (value: string) => void;
17
+ defaultValue?: string;
18
+ }
19
+
20
+ export const AsyncDropdown: React.FC<IAsyncDropdownProps> = ({
21
+ options,
22
+ width,
23
+ onChange,
24
+ defaultValue,
25
+ }) => {
26
+ const dropdownId = uuidv4();
27
+
28
+ const [selectedValue, setSelectedValue] = useState<string>(
29
+ defaultValue || options?.[0]?.value
30
+ );
31
+ const [selectedOption, setSelectedOption] = useState<string | JSX.Element>(
32
+ options?.find(option => option.value === defaultValue)?.option ||
33
+ options?.[0]?.option
34
+ );
35
+ const [opened, setOpened] = useState<boolean>(false);
36
+
37
+ useEffect(() => {
38
+ if (defaultValue) {
39
+ setSelectedValue(defaultValue);
40
+ setSelectedOption(
41
+ //@ts-ignore
42
+ options?.find(option => option.value === defaultValue)?.option
43
+ );
44
+ } else if (options?.length > 0) {
45
+ setSelectedValue(options[0].value);
46
+ setSelectedOption(options[0].option);
47
+ }
48
+ }, [options, defaultValue]);
49
+
50
+ const handleSelection = (value: string, option: string | JSX.Element) => {
51
+ if (value !== selectedValue) {
52
+ setSelectedValue(value);
53
+ setSelectedOption(option);
54
+ onChange(value);
55
+ }
56
+ };
57
+
58
+ return (
59
+ <Container onMouseLeave={() => setOpened(false)} width={width}>
60
+ <DropdownSelect
61
+ id={`dropdown-${dropdownId}`}
62
+ className="rpgui-dropdown-imp rpgui-dropdown-imp-header"
63
+ onPointerUp={() => setOpened(prev => !prev)}
64
+ >
65
+ <DropdownDisplay>
66
+ <DropdownLabel>▼</DropdownLabel> {selectedOption}
67
+ {!options.length && <BeatLoader size={4} color={uiColors.white} />}
68
+ </DropdownDisplay>
69
+ </DropdownSelect>
70
+
71
+ <DropdownOptions className="rpgui-dropdown-imp" opened={opened}>
72
+ {options?.map(option => (
73
+ <li
74
+ key={option.id}
75
+ onPointerUp={() => {
76
+ handleSelection(option.value, option.option);
77
+ setOpened(false);
78
+ }}
79
+ >
80
+ {option.option}
81
+ </li>
82
+ ))}
83
+ </DropdownOptions>
84
+ </Container>
85
+ );
86
+ };
87
+
88
+ const DropdownDisplay = styled.span`
89
+ display: flex;
90
+ flex-wrap: wrap;
91
+ justify-content: start;
92
+ align-items: center;
93
+ `;
94
+
95
+ const DropdownLabel = styled.label`
96
+ margin-right: 0.5rem;
97
+ `;
98
+
99
+ const Container = styled.div<{ width: string | undefined }>`
100
+ position: relative;
101
+ width: ${props => props.width || '100%'};
102
+ `;
103
+
104
+ const DropdownSelect = styled.p`
105
+ width: 100%;
106
+ box-sizing: border-box;
107
+
108
+ label {
109
+ display: inline-block;
110
+ transform: translateY(-2px);
111
+ }
112
+ `;
113
+
114
+ const DropdownOptions = styled.ul<{
115
+ opened: boolean;
116
+ }>`
117
+ position: absolute;
118
+ width: 100%;
119
+ max-height: 300px;
120
+ overflow-y: auto;
121
+ display: ${props => (props.opened ? 'block' : 'none')};
122
+ box-sizing: border-box;
123
+
124
+ @media (max-width: 768px) {
125
+ padding: 8px 0;
126
+ }
127
+ `;
@@ -12,22 +12,16 @@ export interface IDropdownProps {
12
12
  options: IOptionsProps[];
13
13
  width?: string;
14
14
  onChange: (value: string) => void;
15
- defaultValue?: string;
16
- triggerChangeOnMount?: boolean;
17
15
  }
18
16
 
19
17
  export const Dropdown: React.FC<IDropdownProps> = ({
20
18
  options,
21
19
  width,
22
20
  onChange,
23
- defaultValue,
24
- triggerChangeOnMount = true,
25
21
  }) => {
26
22
  const dropdownId = uuidv4();
27
23
 
28
- const [selectedValue, setSelectedValue] = useState<string>(
29
- defaultValue || ''
30
- );
24
+ const [selectedValue, setSelectedValue] = useState<string>('');
31
25
  const [selectedOption, setSelectedOption] = useState<string | JSX.Element>(
32
26
  ''
33
27
  );
@@ -54,17 +48,7 @@ export const Dropdown: React.FC<IDropdownProps> = ({
54
48
  }, [options]);
55
49
 
56
50
  useEffect(() => {
57
- if (defaultValue) {
58
- const option = options.find(o => o.value === defaultValue);
59
- if (option) {
60
- setSelectedValue(option.value);
61
- setSelectedOption(option.option);
62
- }
63
- }
64
- }, [defaultValue]);
65
-
66
- useEffect(() => {
67
- if (selectedValue && triggerChangeOnMount) {
51
+ if (selectedValue) {
68
52
  onChange(selectedValue);
69
53
  }
70
54
  }, [selectedValue]);
@@ -1,66 +1,59 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React, { useEffect, useState } from 'react';
2
2
  import styled, { keyframes } from 'styled-components';
3
3
  import SelectArrow from '../Arrow/SelectArrow';
4
4
 
5
5
  export interface ISimpleImageCarousel {
6
- basePath: string;
7
- imagesSrc: string[];
6
+ images: string[];
8
7
  styles?: React.CSSProperties;
9
8
  autoCycle?: boolean;
10
9
  autoCycleDelay?: number;
11
10
  stopAutoCyclingOnInteraction?: boolean;
12
11
  }
13
12
 
14
- export const SimpleImageCarousel = ({
15
- basePath,
16
- imagesSrc,
13
+ export const SimpleImageCarousel: React.FC<ISimpleImageCarousel> = ({
14
+ images,
17
15
  styles,
18
16
  autoCycle = false,
19
17
  autoCycleDelay = 3000,
20
18
  stopAutoCyclingOnInteraction = false,
21
- }: ISimpleImageCarousel) => {
19
+ }) => {
22
20
  const [currentImage, setCurrentImage] = useState(0);
23
- const isInteracted = useRef<boolean>(false);
21
+ const [autoCycleId, setAutoCycleId] = useState<NodeJS.Timeout | null>(null);
24
22
 
25
- useEffect(() => {
26
- let timer: NodeJS.Timeout | null = null;
23
+ const handleLeftClick = () => {
24
+ setCurrentImage(
25
+ prevImage => (prevImage - 1 + images.length) % images.length
26
+ );
27
+ if (stopAutoCyclingOnInteraction && autoCycleId) {
28
+ clearInterval(autoCycleId);
29
+ setAutoCycleId(null);
30
+ }
31
+ };
27
32
 
28
- if (autoCycle && !isInteracted.current) {
29
- timer = setInterval(() => {
30
- if (stopAutoCyclingOnInteraction && isInteracted.current) {
31
- clearInterval(timer!);
32
- return;
33
- }
33
+ const handleRightClick = () => {
34
+ setCurrentImage(prevImage => (prevImage + 1) % images.length);
35
+ if (stopAutoCyclingOnInteraction && autoCycleId) {
36
+ clearInterval(autoCycleId);
37
+ setAutoCycleId(null);
38
+ }
39
+ };
34
40
 
35
- handleRightClick(true);
41
+ useEffect(() => {
42
+ if (autoCycle) {
43
+ const id = setInterval(() => {
44
+ setCurrentImage(prevImage => (prevImage + 1) % images.length);
36
45
  }, autoCycleDelay);
46
+ setAutoCycleId(id);
37
47
  }
38
48
 
39
49
  return () => {
40
- if (timer) {
41
- clearInterval(timer);
50
+ if (autoCycleId) {
51
+ clearInterval(autoCycleId);
42
52
  }
43
53
  };
44
- }, [autoCycle, autoCycleDelay]);
45
-
46
- const handleLeftClick = () => {
47
- isInteracted.current = true;
48
- setCurrentImage(oldImage =>
49
- oldImage === 0 ? imagesSrc.length - 1 : oldImage - 1
50
- );
51
- };
52
-
53
- const handleRightClick = (isAutomatedClick: boolean = false) => {
54
- if (!isAutomatedClick) {
55
- isInteracted.current = true;
56
- }
57
-
58
- setCurrentImage(oldImage =>
59
- oldImage === imagesSrc.length - 1 ? 0 : oldImage + 1
60
- );
61
- };
54
+ }, [autoCycle, autoCycleDelay, images.length]);
62
55
 
63
- const hasMoreThanOneImage = imagesSrc.length > 1;
56
+ const hasMoreThanOneImage = images.length > 1;
64
57
 
65
58
  return (
66
59
  <ImageContainer style={styles}>
@@ -73,8 +66,8 @@ export const SimpleImageCarousel = ({
73
66
  <Carousel>
74
67
  <FadeInCarouselImg
75
68
  key={currentImage}
76
- src={`${basePath}/${imagesSrc[currentImage]}`}
77
- alt={imagesSrc[currentImage]}
69
+ src={images[currentImage]}
70
+ alt={`Image ${currentImage}`}
78
71
  />
79
72
  </Carousel>
80
73
  </ImageContainer>
package/src/index.tsx CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './components/AsyncDropdown';
1
2
  export * from './components/Button';
2
3
  export * from './components/Character/CharacterSelection';
3
4
  export * from './components/Chat/Chat';
@@ -14,9 +14,9 @@ const meta: Meta = {
14
14
 
15
15
  export default meta;
16
16
 
17
- const Template: Story<IDropdownProps> = args => (
17
+ const Template: Story<IDropdownProps> = (args) => (
18
18
  <RPGUIRoot>
19
- <Dropdown {...args} defaultValue="Elfo" />
19
+ <Dropdown {...args} />
20
20
  </RPGUIRoot>
21
21
  );
22
22
 
@@ -7,6 +7,8 @@ import {
7
7
  } from '../components/ImageCarousel/SimpleImageCarousel';
8
8
 
9
9
  import styled from 'styled-components';
10
+ import image1 from '../assets/images/backpack.png';
11
+ import image2 from '../assets/images/depot.png';
10
12
  import { RPGUIRoot } from '../components/RPGUI/RPGUIRoot';
11
13
 
12
14
  const meta: Meta = {
@@ -37,8 +39,7 @@ const CenteredContainer = styled.div`
37
39
  export const SimpleImageCarouselStory = Template.bind({});
38
40
 
39
41
  SimpleImageCarouselStory.args = {
40
- imagesSrc: ['./images/backpack.png', './images/depot.png'],
41
- basePath: 'http://localhost:6006',
42
+ images: [image1, image2],
42
43
  autoCycle: true,
43
44
  stopAutoCyclingOnInteraction: true,
44
45
  };