@rpg-engine/long-bow 0.7.85 → 0.7.87

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.7.85",
3
+ "version": "0.7.87",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,13 +1,5 @@
1
1
  import React from 'react';
2
- import styled from 'styled-components';
3
- import ModalPortal from '../../Abstractions/ModalPortal';
4
- import { ItemQuantitySelector } from './ItemQuantitySelector';
5
-
6
- export interface IQuantitySelect {
7
- isOpen: boolean;
8
- maxQuantity: number;
9
- callback: (_quantity: number) => void;
10
- }
2
+ import { IQuantitySelect, QuantitySelectorModal } from '../../QuantitySelector/QuantitySelectorModal';
11
3
 
12
4
  interface IProps {
13
5
  quantitySelect: IQuantitySelect;
@@ -17,43 +9,13 @@ interface IProps {
17
9
  export const ItemQuantitySelectorModal = ({
18
10
  quantitySelect,
19
11
  setQuantitySelect,
20
- }: IProps) => {
12
+ }: IProps): JSX.Element => {
21
13
  return (
22
- <ModalPortal>
23
- <QuantitySelectorContainer>
24
- <ItemQuantitySelector
25
- quantity={quantitySelect.maxQuantity}
26
- onConfirm={quantity => {
27
- quantitySelect.callback(quantity);
28
- setQuantitySelect({
29
- isOpen: false,
30
- maxQuantity: 1,
31
- callback: () => {},
32
- });
33
- }}
34
- onClose={() => {
35
- quantitySelect.callback(-1);
36
- setQuantitySelect({
37
- isOpen: false,
38
- maxQuantity: 1,
39
- callback: () => {},
40
- });
41
- }}
42
- />
43
- </QuantitySelectorContainer>
44
- </ModalPortal>
14
+ <QuantitySelectorModal
15
+ quantitySelect={quantitySelect}
16
+ setQuantitySelect={setQuantitySelect}
17
+ />
45
18
  );
46
19
  };
47
20
 
48
- const QuantitySelectorContainer = styled.div`
49
- position: absolute;
50
- top: 0;
51
- left: 0;
52
- width: 100vw;
53
- height: 100vh;
54
- z-index: 100;
55
- display: flex;
56
- justify-content: center;
57
- align-items: center;
58
- background-color: rgba(0, 0, 0, 0.5);
59
- `;
21
+ export type { IQuantitySelect };
@@ -0,0 +1,143 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import styled from 'styled-components';
3
+ import { Button, ButtonTypes } from '../Button';
4
+ import { RPGUIContainer, RPGUIContainerTypes } from '../RPGUI/RPGUIContainer';
5
+ import { RangeSlider, RangeSliderType } from '../RangeSlider';
6
+
7
+ export interface IQuantitySelectorProps {
8
+ maxQuantity: number;
9
+ initialQuantity?: number;
10
+ title?: string;
11
+ onConfirm: (quantity: number) => void;
12
+ onClose: () => void;
13
+ }
14
+
15
+ export const QuantitySelector: React.FC<IQuantitySelectorProps> = ({
16
+ maxQuantity,
17
+ initialQuantity,
18
+ title = 'Select quantity',
19
+ onConfirm,
20
+ onClose,
21
+ }) => {
22
+ const [value, setValue] = useState(initialQuantity ?? maxQuantity);
23
+ const inputRef = useRef<HTMLInputElement>(null);
24
+
25
+ useEffect(() => {
26
+ const closeSelector = (e: KeyboardEvent) => {
27
+ if (e.key === 'Escape') {
28
+ onClose();
29
+ }
30
+ };
31
+
32
+ if (inputRef.current) {
33
+ inputRef.current.focus();
34
+ inputRef.current.select();
35
+ }
36
+
37
+ document.addEventListener('keydown', closeSelector);
38
+ return () => document.removeEventListener('keydown', closeSelector);
39
+ }, [onClose]);
40
+
41
+ return (
42
+ <StyledContainer type={RPGUIContainerTypes.Framed} width="25rem">
43
+ <CloseButton className="container-close" onPointerDown={onClose}>
44
+ X
45
+ </CloseButton>
46
+ <h2>{title}</h2>
47
+ <StyledForm
48
+ onSubmit={e => {
49
+ e.preventDefault();
50
+ const numberValue = Number(value);
51
+ if (!Number.isNaN(numberValue)) {
52
+ onConfirm(Math.max(1, Math.min(maxQuantity, numberValue)));
53
+ }
54
+ }}
55
+ noValidate
56
+ >
57
+ <StyledInput
58
+ ref={inputRef}
59
+ placeholder="Enter quantity"
60
+ type="number"
61
+ min={1}
62
+ max={maxQuantity}
63
+ value={value}
64
+ onChange={e => {
65
+ if (Number(e.target.value) >= maxQuantity) {
66
+ setValue(maxQuantity);
67
+ return;
68
+ }
69
+ setValue(Number(e.target.value));
70
+ }}
71
+ onBlur={e => {
72
+ setValue(
73
+ Math.max(1, Math.min(maxQuantity, Number(e.target.value)))
74
+ );
75
+ }}
76
+ />
77
+ <RangeSlider
78
+ type={RangeSliderType.Slider}
79
+ valueMin={1}
80
+ valueMax={maxQuantity}
81
+ width="100%"
82
+ onChange={setValue}
83
+ value={value}
84
+ />
85
+ <Button buttonType={ButtonTypes.RPGUIButton} type="submit">
86
+ Confirm
87
+ </Button>
88
+ </StyledForm>
89
+ </StyledContainer>
90
+ );
91
+ };
92
+
93
+ const StyledContainer = styled(RPGUIContainer)`
94
+ position: relative;
95
+ display: flex;
96
+ flex-direction: column;
97
+ align-items: center;
98
+ padding: 1rem;
99
+
100
+ h2 {
101
+ margin: 0;
102
+ margin-bottom: 1rem;
103
+ font-size: 1rem;
104
+ }
105
+ `;
106
+
107
+ const CloseButton = styled.div`
108
+ position: absolute;
109
+ top: 3px;
110
+ right: 0px;
111
+ color: white;
112
+ z-index: 22;
113
+ font-size: 1.5rem;
114
+ cursor: pointer;
115
+ `;
116
+
117
+ const StyledForm = styled.form`
118
+ display: flex;
119
+ flex-direction: column;
120
+ align-items: center;
121
+ gap: 1rem;
122
+ width: 100%;
123
+ `;
124
+
125
+ const StyledInput = styled.input`
126
+ width: 100%;
127
+ padding: 0.5rem;
128
+ background-color: rgba(0, 0, 0, 0.25);
129
+ border: none;
130
+ color: white;
131
+ font-size: 1rem;
132
+ text-align: center;
133
+
134
+ &::-webkit-inner-spin-button,
135
+ &::-webkit-outer-spin-button {
136
+ -webkit-appearance: none;
137
+ margin: 0;
138
+ }
139
+
140
+ &[type='number'] {
141
+ -moz-appearance: textfield;
142
+ }
143
+ `;
@@ -0,0 +1,79 @@
1
+ import React from 'react';
2
+ import styled from 'styled-components';
3
+ import ModalPortal from '../Abstractions/ModalPortal';
4
+ import { QuantitySelector } from './QuantitySelector';
5
+
6
+ export interface IQuantitySelect {
7
+ isOpen: boolean;
8
+ maxQuantity: number;
9
+ callback: (quantity: number) => void;
10
+ title?: string;
11
+ initialQuantity?: number;
12
+ }
13
+
14
+ interface IProps {
15
+ quantitySelect: IQuantitySelect;
16
+ setQuantitySelect: React.Dispatch<React.SetStateAction<IQuantitySelect>>;
17
+ }
18
+
19
+ const validateQuantitySelect = (
20
+ quantitySelect: IQuantitySelect
21
+ ): IQuantitySelect => ({
22
+ ...quantitySelect,
23
+ maxQuantity: Math.max(1, quantitySelect.maxQuantity),
24
+ initialQuantity: quantitySelect.initialQuantity
25
+ ? Math.max(
26
+ 1,
27
+ Math.min(quantitySelect.maxQuantity, quantitySelect.initialQuantity)
28
+ )
29
+ : undefined,
30
+ });
31
+
32
+ export const QuantitySelectorModal = ({
33
+ quantitySelect,
34
+ setQuantitySelect,
35
+ }: IProps): JSX.Element => {
36
+ const resetQuantitySelect = () => {
37
+ setQuantitySelect({
38
+ isOpen: false,
39
+ maxQuantity: 1,
40
+ callback: () => {},
41
+ });
42
+ };
43
+
44
+ // Validate the input values
45
+ const validatedQuantitySelect = validateQuantitySelect(quantitySelect);
46
+
47
+ return (
48
+ <ModalPortal>
49
+ <QuantitySelectorContainer>
50
+ <QuantitySelector
51
+ maxQuantity={validatedQuantitySelect.maxQuantity}
52
+ initialQuantity={validatedQuantitySelect.initialQuantity}
53
+ title={validatedQuantitySelect.title}
54
+ onConfirm={quantity => {
55
+ validatedQuantitySelect.callback(quantity);
56
+ resetQuantitySelect();
57
+ }}
58
+ onClose={() => {
59
+ validatedQuantitySelect.callback(-1);
60
+ resetQuantitySelect();
61
+ }}
62
+ />
63
+ </QuantitySelectorContainer>
64
+ </ModalPortal>
65
+ );
66
+ };
67
+
68
+ const QuantitySelectorContainer = styled.div`
69
+ position: absolute;
70
+ top: 0;
71
+ left: 0;
72
+ width: 100vw;
73
+ height: 100vh;
74
+ z-index: 100;
75
+ display: flex;
76
+ justify-content: center;
77
+ align-items: center;
78
+ background-color: rgba(0, 0, 0, 0.5);
79
+ `;