-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
179 lines (165 loc) · 6.32 KB
/
index.tsx
File metadata and controls
179 lines (165 loc) · 6.32 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { useState, useEffect, useCallback } from "react";
import { Input, InputNumber, Select, Slider } from "antd";
import { GradientOption } from "../../types";
import {
useSelectedRecipeId,
useGetCurrentValue,
useEditRecipe,
useRecipes,
} from "../../state/store";
import GradientInput from "../GradientInput";
import "./style.css";
interface InputSwitchProps {
displayName: string;
inputType: string;
dataType: string;
description: string;
id: string;
min?: number;
max?: number;
conversionFactor?: number;
unit?: string;
options?: string[];
gradientOptions?: GradientOption[];
}
const InputSwitch = (props: InputSwitchProps): JSX.Element => {
const { displayName, inputType, dataType, description, min, max, options, id, gradientOptions, conversionFactor, unit } = props;
const selectedRecipeId = useSelectedRecipeId();
const editRecipe = useEditRecipe();
const getCurrentValue = useGetCurrentValue();
const recipes = useRecipes();
// Conversion factor for numeric inputs where we want to display a
// different unit in the UI than is stored in the recipe
const conversion = conversionFactor ?? 1;
// Stable getter for current value
const getCurrentValueMemo = useCallback(() => {
let value = getCurrentValue(id);
if (!value) {
// Default to min or 0 if numeric type, othewise empty string
if (dataType === "integer" || dataType === "float") {
value = min ?? 0;
} else {
value = "";
}
}
if (typeof value == "number") {
value = value * conversion;
value = Number(value.toFixed(2));
}
return value;
}, [getCurrentValue, id, min, conversion, dataType]);
// Local controlled state for the input UI
const [value, setValue] = useState<string | number>(getCurrentValueMemo());
// Reset local state when store value (or recipe) changes
useEffect(() => {
setValue(getCurrentValueMemo());
}, [getCurrentValueMemo, recipes]);
const handleInputChange = (value: string | number | null) => {
if (value == null || !selectedRecipeId) return;
setValue(value);
if (typeof value === "number") {
// Convert back to original units for updating recipe object
value = value / conversion;
value = Number(value.toFixed(4));
}
editRecipe(selectedRecipeId, id, value);
};
switch (inputType) {
case "slider": {
const numericValue =
typeof value === "number" ? value : Number(value) || 0;
const step = dataType === "integer" ? 1 : 0.01;
let maxValue = (max ?? 1) * conversion;
maxValue = Number(maxValue.toFixed(4));
const minValue = min ?? 0;
return (
<div className="input-switch">
<div className="input-label">
<strong>
{displayName} {unit && <span>({unit})</span>}
</strong>
<small>{description}</small>
</div>
<div className="input-content">
<div className="slider-input-wrapper">
<Slider
min={minValue}
max={maxValue}
step={step}
onChange={handleInputChange}
value={numericValue}
style={{ marginBottom: 0 }}
/>
<div className="slider-labels">
<small className="slider-label-left">
{minValue}
</small>
<small className="slider-label-right">
{maxValue}
</small>
</div>
</div>
<InputNumber
min={minValue}
max={maxValue}
step={step}
style={{ margin: "0 0 10px 10px" }}
value={numericValue}
onChange={handleInputChange}
type="number"
/>
</div>
</div>
);
}
case "dropdown": {
const selectOptions = options?.map((option) => ({
label: option,
value: option,
})) || [];
return (
<div className="input-switch">
<div className="input-label">
<strong>{displayName}</strong>{" "}
<small>{description}</small>
</div>
<div className="input-content">
<Select
options={selectOptions}
value={String(value)}
onChange={handleInputChange}
/>
</div>
</div>
);
}
case "gradient": {
return gradientOptions && gradientOptions.length > 0 ? (
<GradientInput
displayName={displayName}
description={description}
gradientOptions={gradientOptions}
defaultValue={String(getCurrentValueMemo())}
/>
) : (
<div>Issue reading gradient options</div>
);
}
default: {
return (
<div className="input-switch">
<div className="input-label">
<strong>{displayName}</strong>{" "}
<small>{description}</small>
</div>
<Input
value={String(value)}
onChange={(e) => handleInputChange(e.target.value)}
style={{ width: 200, marginLeft: 10 }}
/>
</div>
);
}
}
};
export default InputSwitch;