Skip to content

Commit

Permalink
Reset confirmation keyword field whenever the dialog is open
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Feb 6, 2024
1 parent 361b150 commit 0f27e18
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "material-ui-confirm",
"version": "3.0.10",
"description": "Higher order component for straightforward use of @material-ui/core confirmation dialogs.",
"description": "Simple confirmation dialogs built on top of @mui/material",
"keywords": [
"Material UI",
"confirmation",
Expand Down
3 changes: 3 additions & 0 deletions src/ConfirmProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ let confirmGlobal;
const ConfirmProvider = ({ children, defaultOptions = {} }) => {
const [options, setOptions] = useState({});
const [resolveReject, setResolveReject] = useState([]);
const [key, setKey] = useState(0);
const [resolve, reject] = resolveReject;

const confirm = useCallback((options = {}) => {
return new Promise((resolve, reject) => {
setKey((key) => key + 1);
setOptions(options);
setResolveReject([resolve, reject]);
});
Expand Down Expand Up @@ -108,6 +110,7 @@ const ConfirmProvider = ({ children, defaultOptions = {} }) => {
{children}
</ConfirmContext.Provider>
<ConfirmationDialog
key={key}
open={resolveReject.length === 2}
options={buildOptions(defaultOptions, options)}
onClose={handleClose}
Expand Down
30 changes: 30 additions & 0 deletions test/useConfirm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,37 @@ describe("useConfirm", () => {

expect(confirmationButton.disabled).toBe(false);
});

test("resets the input value on every open", () => {
const { getByText, getAllByText } = render(
<TestComponent
confirmOptions={{
confirmationKeyword: "DELETE",
}}
/>
);

fireEvent.click(getByText("Delete"));

let textfield = getAllByText(
(content, element) => element.tagName.toLowerCase() === "input"
)[0];

expect(textfield).toBeTruthy();
fireEvent.change(textfield, { target: { value: "DELETE" } });

fireEvent.click(getByText("Ok"));

fireEvent.click(getByText("Delete"));

textfield = getAllByText(
(content, element) => element.tagName.toLowerCase() === "input"
)[0];

expect(textfield.value).toEqual("");
});
});

test("renders textfield with custom props", () => {
const { getByText, queryByPlaceholderText } = render(
<TestComponent
Expand Down

0 comments on commit 0f27e18

Please sign in to comment.