Skip to content

Commit

Permalink
Fixed number inputs being janky
Browse files Browse the repository at this point in the history
  • Loading branch information
Soryyyn committed Feb 27, 2023
1 parent 5546e0e commit 2cd8613
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
18 changes: 7 additions & 11 deletions src/renderer/components/NewFarm/BasicInfoTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,13 @@ export default function BasicInfoTab({ data, onChange }: Props) {
/>
<Select
label="Farming location (stream site)"
value={basicInfo.type}
value={
api.selections.FarmingLocation.find(
(select) => select.value === basicInfo.type
)?.display ?? ''
}
fullWidth
options={[
{
display: 'Twitch',
value: 'twitch'
},
{
display: 'YouTube',
value: 'youtube'
}
]}
options={api.selections.FarmingLocation}
onChange={(changed) => {
setBasicInfo({
...basicInfo,
Expand All @@ -66,6 +61,7 @@ export default function BasicInfoTab({ data, onChange }: Props) {
}}
/>
<NumberInput
key="schedule-input"
label="Farming schedule (in minutes)"
value={basicInfo.schedule}
withButtons
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/components/NewFarm/ConditionsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export default function ConditionsTab({ data, onChange }: Props) {
<div className="flex flex-col gap-4">
<Select
label="Type of condition"
value={conditionsInfo.type}
value={
api.selections.FarmConditionSelect.find(
(select) => select.value === conditionsInfo.type
)?.display ?? ''
}
fullWidth
options={api.selections.FarmConditionSelect}
onChange={(changed) =>
Expand All @@ -48,6 +52,7 @@ export default function ConditionsTab({ data, onChange }: Props) {
<span className="bg-pepper-400 h-0.5 w-[98%] rounded-full self-center" />

<NumberInput
key="amount-to-fulfill-input"
label="Amount to fulfill in timespan (in hours)"
value={conditionsInfo.amountToFulFill ?? 4}
min={1}
Expand All @@ -63,6 +68,7 @@ export default function ConditionsTab({ data, onChange }: Props) {
/>

<NumberInput
key="buffer-input"
label="Buffer for fulfillment (in minutes)"
value={conditionsInfo.buffer ?? 30}
min={0}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/Settings/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default function Setting({ setting, onChange }: Props) {
) {
return (
<NumberInput
key={`${setting.id}-number-input`}
value={
((setting.value as SettingValueWithSpecial)
.value as number) ?? setting.value
Expand Down
28 changes: 13 additions & 15 deletions src/renderer/components/global/Inputs/NumberInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { faMinus, faPlus } from '@fortawesome/free-solid-svg-icons';
import clsx from 'clsx';
import { uniqueId } from 'lodash';
import React, { useEffect, useState } from 'react';
import Icon from '../Icon';

Expand All @@ -24,25 +23,16 @@ export default function NumberInput({
fullWidth,
disabled,
withButtons,
inputWidth,
onChange
}: Props) {
const [inputValue, setInputValue] = useState<number>(value);

useEffect(() => {
if (value < 0) setInputValue(0);

if (value < min) {
setInputValue(min);
} else if (value > max) {
setInputValue(max);
} else {
onChange(inputValue);
}
onChange(inputValue);
}, [inputValue]);

return (
<div className="flex flex-col gap-2 grow" key={uniqueId()}>
<div className="flex flex-col gap-2 grow">
{label && (
<div className="flex flex-row leading-none gap-1">
<span className="text-snow-300">{label}</span>
Expand Down Expand Up @@ -77,12 +67,20 @@ export default function NumberInput({
'hover:bg-pepper-800 focus:bg-pepper-800': !disabled
}
)}
type="number"
disabled={disabled}
value={value}
type="number"
onInput={(event) => {
setInputValue(parseInt(event.currentTarget.value));
onChange={(event) => {
const { value, min, max } = event.target;
const adjustedValue = Math.max(
Number(min),
Math.min(Number(max), Number(value))
);

setInputValue(adjustedValue);
}}
min={min}
max={max}
/>
{withButtons && (
<button
Expand Down

0 comments on commit 2cd8613

Please sign in to comment.