@rpg-engine/long-bow 0.5.65 → 0.5.66

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.66",
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
  }
@@ -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]);
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