aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/DocCreatorMenu/Menu/ConditionalsTextarea.tsx
blob: 89c2e44ff5a6be7123c74a7e4bde82ad870343d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { observer } from "mobx-react";
import { ObservableReactComponent } from "../../../../ObservableReactComponent";
import { Conditional } from "../Backend/TemplateManager";
import { action, makeObservable, observable, runInAction } from "mobx";
import React from "react";

interface ConditionalsTextAreaProps {
    conditional: Conditional;
    property: keyof Conditional;
}

@observer
export class ConditionalsTextArea extends ObservableReactComponent<ConditionalsTextAreaProps> {

    private mirrorRef: HTMLSpanElement | null = null;

    @observable private inputWidth: string = '60px';

    constructor(props: ConditionalsTextAreaProps) {
        super(props);
        makeObservable(this);
    }

    setMirrorRef: React.LegacyRef<HTMLSpanElement> = (node) => { this.mirrorRef = node }

    @action updateInputWidth() {
        const mirror = this.mirrorRef;
        if (mirror) {
            const width = mirror.offsetWidth;
            if ( width + 8 > 60) this.inputWidth = `${width + 8}px`;
        }
    }

    render() {
        return (
            <div style={{ display: 'inline-block', position: 'relative' }}>
                <span
                    ref={this.setMirrorRef}
                    style={{
                        position: 'absolute',
                        visibility: 'hidden',
                        whiteSpace: 'pre',
                        font: 'inherit',
                        padding: 0,
                    }}
                >
                    {this._props.conditional[this._props.property] || ' '}
                </span>
                <input
                    className="form-row-input"
                    value={this.props.conditional[this.props.property] ?? ''}
                    onChange={e => {
                        runInAction(() => {
                        this.props.conditional[this.props.property] = e.target.value as "=" | ">" | "<" | "contains";
                        });
                        this.updateInputWidth();
                    }}
                    style={{ width: this.inputWidth }}
                    placeholder={this.props.property}
                />
            </div>
        );
    }

}