Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/components/InputSwitch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface InputSwitchProps {
dataType: string;
description: string;
id: string;
defaultValue: string | number;
min?: number;
max?: number;
conversionFactor?: number;
Expand All @@ -26,7 +25,7 @@ interface InputSwitchProps {
}

const InputSwitch = (props: InputSwitchProps): JSX.Element => {
const { displayName, inputType, dataType, description, defaultValue, min, max, options, id, gradientOptions, conversionFactor, unit } = props;
const { displayName, inputType, dataType, description, min, max, options, id, gradientOptions, conversionFactor, unit } = props;

const selectedRecipeId = useSelectedRecipeId();
const updateRecipeObj = useUpdateRecipeObj();
Expand All @@ -37,15 +36,22 @@ const InputSwitch = (props: InputSwitchProps): JSX.Element => {
// different unit in the UI than is stored in the recipe
const conversion = conversionFactor ?? 1;

// Stable getter for current value, with default fallback
// Stable getter for current value
const getCurrentValueMemo = useCallback(() => {
const v = getCurrentValue(id);
let value = v ?? defaultValue;
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;
}
return value;
}, [getCurrentValue, id, defaultValue, conversion]);
}, [getCurrentValue, id, min, conversion, dataType]);

// Local controlled state for the input UI
const [value, setValue] = useState<string | number>(getCurrentValueMemo());
Expand Down
1 change: 0 additions & 1 deletion src/components/RecipeForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const RecipeForm = ({ onStartPacking }: RecipeFormProps) => {
inputType={field.input_type}
dataType={field.data_type}
description={field.description}
defaultValue={field.default}
min={field.min}
max={field.max}
options={field.options}
Expand Down
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export type EditableField = {
data_type: string;
input_type: string;
description: string;
default: string | number;
path: string;
min?: number;
max?: number;
Expand Down
1 change: 0 additions & 1 deletion src/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const getEditableFieldsList = async (editable_field_ids: string[]): Promise<Edit
data_type: doc.data().data_type,
input_type: doc.data().input_type,
description: doc.data().description,
default: doc.data().default,
min: doc.data().min,
max: doc.data().max,
options: doc.data().options,
Expand Down