|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | */ |
5 | 5 |
|
6 | | -/* eslint-disable prefer-arrow/prefer-arrow-functions, @typescript-eslint/naming-convention, jsdoc/require-jsdoc */ |
7 | | - |
8 | | -import { useState } from "preact/hooks"; |
9 | | -import type { JSX } from "preact/jsx-runtime"; |
| 6 | +import type { ComponentChild } from "preact"; |
10 | 7 | import type { Subscribable } from "../../core/types/general.js"; |
11 | | -import { useSubscription } from "../../ui/hooks/useSubscription.js"; |
| 8 | +import { ComponentBase, type IComponentProperties, type IComponentState } from "./ComponentBase/ComponentBase.js"; |
12 | 9 |
|
13 | | -export function NumberInput({ getValue, setValue, subscribable }: { |
14 | | - getValue: () => string, |
15 | | - setValue: (newValue: string) => void, |
| 10 | +export interface INumberInputProps extends IComponentProperties { |
| 11 | + getValue: () => string; |
| 12 | + setValue: (newValue: string) => void; |
16 | 13 | subscribable: Subscribable; |
17 | | -}): JSX.Element { |
18 | | - const [visibleValue, setVisibleValue] = useState(getValue()); |
| 14 | +} |
| 15 | + |
| 16 | +interface INumberInputState extends IComponentState { |
| 17 | + visibleValue: string; |
| 18 | +} |
| 19 | + |
| 20 | +export class NumberInput extends ComponentBase<INumberInputProps, INumberInputState> { |
| 21 | + |
| 22 | + public constructor(props: INumberInputProps) { |
| 23 | + super(props); |
| 24 | + |
| 25 | + this.state = { |
| 26 | + visibleValue: this.props.getValue() |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + public override componentDidMount(): void { |
| 31 | + const { subscribable } = this.props; |
19 | 32 |
|
20 | | - // If the model pushes a change to this value for some other reason, we'd better update |
21 | | - useSubscription(subscribable, () => { |
22 | | - setVisibleValue(getValue()); |
23 | | - }); |
| 33 | + subscribable.subscribe(this.handleChange); |
| 34 | + } |
| 35 | + |
| 36 | + public override componentWillUnmount(): void { |
| 37 | + const { subscribable } = this.props; |
| 38 | + |
| 39 | + subscribable.unsubscribe(this.handleChange); |
| 40 | + } |
| 41 | + |
| 42 | + public render(): ComponentChild { |
| 43 | + const { visibleValue } = this.state; |
| 44 | + |
| 45 | + return ( |
| 46 | + <input |
| 47 | + className="short" |
| 48 | + type="text" |
| 49 | + inputMode="numeric" |
| 50 | + pattern="[0-9]*" |
| 51 | + onChange={(event) => { |
| 52 | + this.attemptSetVisibleValue((event.target as HTMLInputElement).value); |
| 53 | + }} |
| 54 | + value={visibleValue} |
| 55 | + onBlur={() => { |
| 56 | + this.attemptSet(); |
| 57 | + }} |
| 58 | + onKeyPress={(event) => { |
| 59 | + if (event.key === "Enter") { |
| 60 | + this.attemptSet(); |
| 61 | + } |
| 62 | + }} |
| 63 | + /> |
| 64 | + ); |
| 65 | + } |
24 | 66 |
|
25 | 67 | // To update the input as you type, but not update the model |
26 | | - function attemptSetVisibleValue(inputValue: string) { |
| 68 | + private attemptSetVisibleValue(inputValue: string) { |
27 | 69 | if (inputValue.length === 0) { |
28 | | - setVisibleValue(""); |
| 70 | + this.setState({ visibleValue: "" }); |
29 | 71 |
|
30 | 72 | return; |
31 | 73 | } |
32 | 74 |
|
33 | 75 | if (!inputValue.charAt(inputValue.length - 1).match(/[0-9]/)) { |
34 | | - attemptSetVisibleValue(inputValue.substring(0, inputValue.length - 1)); |
| 76 | + this.attemptSetVisibleValue(inputValue.substring(0, inputValue.length - 1)); |
35 | 77 |
|
36 | 78 | return; |
37 | 79 | } |
38 | 80 |
|
39 | | - setVisibleValue(inputValue); |
| 81 | + this.setState({ visibleValue: inputValue }); |
40 | 82 | } |
41 | 83 |
|
42 | 84 | // Try to set the model value, which may fail due to validation |
43 | | - function attemptSet() { |
| 85 | + private attemptSet() { |
| 86 | + const { getValue, setValue } = this.props; |
| 87 | + const { visibleValue } = this.state; |
| 88 | + |
44 | 89 | if (visibleValue === getValue()) { |
45 | 90 | return; |
46 | 91 | } |
47 | 92 |
|
48 | 93 | try { |
49 | 94 | setValue(visibleValue); |
50 | 95 | } catch { |
51 | | - setVisibleValue(getValue()); |
| 96 | + this.setState({ visibleValue: getValue() }); |
52 | 97 | } |
53 | 98 | } |
54 | 99 |
|
55 | | - return ( |
56 | | - <input |
57 | | - className="short" |
58 | | - type="text" |
59 | | - inputMode="numeric" |
60 | | - pattern="[0-9]*" |
61 | | - onChange={event => { |
62 | | - attemptSetVisibleValue((event.target as HTMLInputElement).value); |
63 | | - }} |
64 | | - value={visibleValue} |
65 | | - onBlur={() => { |
66 | | - attemptSet(); |
67 | | - }} |
68 | | - onKeyPress={event => { |
69 | | - if (event.key === "Enter") { |
70 | | - attemptSet(); |
71 | | - } |
72 | | - }} |
73 | | - /> |
74 | | - ); |
| 100 | + private handleChange = () => { |
| 101 | + const { getValue } = this.props; |
| 102 | + |
| 103 | + this.setState({ visibleValue: getValue() }); |
| 104 | + }; |
75 | 105 | } |
0 commit comments