Skip to content

Commit

Permalink
allow negative input on controlled components
Browse files Browse the repository at this point in the history
  • Loading branch information
mentalcaries committed Jun 26, 2022
1 parent 755a464 commit fff4cb9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/components/Inventory/Inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ProductList from '../ProductList/ProductList';
import ProductSearch from '../ProductSearch/ProductSearch';
import Locations from '../Locations/Locations';
import React from 'react';
import { Product, Props } from '../../utils/types';
import { Product } from '../../utils/types';

interface InventoryProps {
onLookup: (query:string) => void;
Expand Down
12 changes: 7 additions & 5 deletions src/components/Locations/Locations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import { Product } from '../../utils/types';
import './Locations.css';

function Locations({onUpdate, location}:{onUpdate: Function, location: Product}) {
const [newQuantity, setNewQuantity] = useState<number>(0);
const [newQuantity, setNewQuantity] = useState<string>('');
const [formValue, setFormValue] = useState()
const [inventoryError, setInventoryError] = useState(false);

const {Core, Location, Quantity, Warehouse} = location;

function handleQuantityChange(evt:React.FormEvent<HTMLInputElement>):void {
setNewQuantity(parseInt(evt.currentTarget.value));
setNewQuantity(evt.currentTarget.value);
}

// Submit new value, but reject number less than inventory
function handleQuantitySubmit(evt:React.SyntheticEvent) {
evt.preventDefault();
if (Quantity + newQuantity >= 0) {

if (Quantity + parseInt(newQuantity) >= 0) {
setInventoryError(false);
onUpdate({
quantity: newQuantity,
core: Core,
location: Location,
});
setNewQuantity(0);
setNewQuantity('');
}
else setInventoryError(true);
}
Expand Down
10 changes: 0 additions & 10 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,3 @@ export interface Product {
Notes: string;
}

export interface Props {
onLookup: Function;
lookupError?: boolean;
prodList?: Product[];
onCoreSearch: Function;
prodStats?: [];
isStocked?: boolean;
inventory?: Product[]
onUpdate?: Function;
}

0 comments on commit fff4cb9

Please sign in to comment.