Skip to content

Commit

Permalink
fix: item value input initial selection
Browse files Browse the repository at this point in the history
  • Loading branch information
rare-magma committed Jun 10, 2023
1 parent e48ffa3 commit 4c4cfce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/components/ItemForm/ItemFormGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ describe("ItemFormGroup", () => {
expect(onChange).toBeCalledWith({
id: 1,
name: "name1change name",
value: 10123,
value: 123,
});
expect(screen.getByDisplayValue("$10,123")).toBeInTheDocument();
expect(screen.getByDisplayValue("$123")).toBeInTheDocument();
});

it("triggers onRemove when user clicks delete confirmation button", async () => {
Expand All @@ -64,12 +64,12 @@ describe("ItemFormGroup", () => {
expect(onRemove).toBeCalledWith({
id: 1,
name: "name1change name",
value: 10123,
value: 123,
});
});

it("shows tooltip when user hovers over", async () => {
fireEvent.mouseOver(screen.getByDisplayValue("$10,123"));
fireEvent.mouseOver(screen.getByDisplayValue("$123"));

expect(await screen.findByText("1% of revenue")).toBeInTheDocument();
});
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("ItemFormGroup", () => {
/>
);

await userEvent.clear(screen.getByDisplayValue("10123 €"));
await userEvent.clear(screen.getByDisplayValue("123 €"));
await userEvent.type(screen.getByLabelText("item 1 value"), "123,12");

expect(screen.getByDisplayValue("123,12 €")).toBeInTheDocument();
Expand Down
5 changes: 5 additions & 0 deletions src/components/ItemForm/ItemFormGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function ItemFormGroup({
const [itemForm, setItemForm] = useState(initialItemForm);
const [changed, setChanged] = useState(false);
const deleteButtonRef = useRef<HTMLButtonElement>(null);
const valueRef = useRef<HTMLInputElement>(null);

const handleRemove = (item: ItemForm) => {
onRemove(item);
Expand Down Expand Up @@ -130,6 +131,10 @@ function ItemFormGroup({
defaultValue={itemForm.value}
allowNegativeValue={false}
maxLength={14}
ref={valueRef}
onFocus={() => {
valueRef.current?.setSelectionRange(0, itemForm.value + 2);
}}
onValueChange={(value) => handleChange("value", value)}
/>
</OverlayTrigger>
Expand Down
10 changes: 5 additions & 5 deletions src/components/TableCard/TableCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ describe("TableCard", () => {
{
id: 1,
name: "expense1change name",
value: 10123,
value: 123,
},
],
total: 10123,
total: 123,
});
expect(screen.getByDisplayValue("$10,123")).toBeInTheDocument();
expect(screen.getByDisplayValue("$123")).toBeInTheDocument();
});

it("triggers onChange when user adds new item", async () => {
Expand All @@ -77,15 +77,15 @@ describe("TableCard", () => {
{
id: 1,
name: "expense1change name",
value: 10123,
value: 123,
},
{
id: 2,
name: "",
value: 0,
},
],
total: 10123,
total: 123,
});
});

Expand Down

0 comments on commit 4c4cfce

Please sign in to comment.