Skip to content

Commit 262f87d

Browse files
committed
fix bug: clear input value every time dialog closed
1 parent e45649f commit 262f87d

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

lib/Dialog.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function DialogProvider(_ref) {
6565
setShow = _useState4[1];
6666

6767
var exitedRef = (0, _react.useRef)(true);
68-
var cancelRef = (0, _react.useRef)(); // unmount 的时候,如果还有 dialog 开着,启动它的正常 cancel 流程
68+
var cancelRef = (0, _react.useRef)(); // when unmount, if some dialog is still open, trigger its canceling process
6969

7070
(0, _react.useEffect)(function () {
7171
return function () {
@@ -176,8 +176,8 @@ function DialogProvider(_ref) {
176176
var mergedOptions = _objectSpread({}, options, {}, showOptions, {
177177
show: show,
178178
onExited: handleExited
179-
}); // animation === false 的时候,onExited 不会触发
180-
// 因此,当 show 变成 false 的时候,认为 exited 完成
179+
}); // if animation === false, the onExited will not get triggered
180+
// so, when show turned into false, it is considered as exited
181181

182182

183183
(0, _react.useEffect)(function () {
@@ -235,15 +235,25 @@ function DialogUI(_ref4) {
235235
refKey = _ref5$refKey === void 0 ? "ref" : _ref5$refKey,
236236
otherInputProps = _objectWithoutProperties(_ref5, ["defaultValue", "refKey"]);
237237

238-
var _useState5 = (0, _react.useState)(defaultValue),
238+
var _useState5 = (0, _react.useState)(""),
239239
_useState6 = _slicedToArray(_useState5, 2),
240240
inputValue = _useState6[0],
241241
setInputValue = _useState6[1];
242242

243-
var inputRef = (0, _react.useRef)();
243+
var inputRef = (0, _react.useRef)(); // only set input value to default value when it shows up
244+
244245
(0, _react.useEffect)(function () {
245-
setInputValue(defaultValue);
246-
}, [defaultValue]);
246+
if (show) {
247+
setInputValue(defaultValue);
248+
}
249+
}, [show, defaultValue]); // if it is closed without animation, clear the input directly
250+
251+
(0, _react.useEffect)(function () {
252+
if (!show && animation === false) {
253+
setInputValue("");
254+
}
255+
}, [show, animation]); // set autoFocus properly
256+
247257
(0, _react.useEffect)(function () {
248258
if (show && input && autoFocus) {
249259
inputRef.current.focus();
@@ -281,11 +291,17 @@ function DialogUI(_ref4) {
281291
setInputValue(e.target.value);
282292
}
283293

294+
function handleExited() {
295+
// clear the input value on finish of the animation
296+
setInputValue("");
297+
onExited();
298+
}
299+
284300
return _react["default"].createElement(_Modal["default"], {
285301
show: show,
286302
onHide: handleHide,
287303
centered: centered,
288-
onExited: onExited,
304+
onExited: handleExited,
289305
animation: animation,
290306
scrollable: scrollable,
291307
size: size

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-bootstrap-easy-dialog",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "React Bootstrap Dialog made easy",
55
"keywords": [
66
"react-bootstrap",

src/Dialog.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// 做的好拿去发布噢
2-
31
import React, {
42
createContext,
53
useContext,
@@ -20,7 +18,7 @@ export function DialogProvider({ children, ...options }) {
2018
const exitedRef = useRef(true);
2119
const cancelRef = useRef();
2220

23-
// unmount 的时候,如果还有 dialog 开着,启动它的正常 cancel 流程
21+
// when unmount, if some dialog is still open, trigger its canceling process
2422
useEffect(() => {
2523
return () => {
2624
cancelRef.current && cancelRef.current();
@@ -35,7 +33,7 @@ export function DialogProvider({ children, ...options }) {
3533
const dialog = useMemo(() => {
3634
function buildMethod(buildOptions, failValue) {
3735
return async (text, options) => {
38-
// 上一次对话尚未结束,调用失败
36+
// if the previous dialog is not exited, fails
3937
if (exitedRef.current === false) return failValue;
4038

4139
return new Promise(resolve => {
@@ -104,8 +102,8 @@ export function DialogProvider({ children, ...options }) {
104102
onExited: handleExited
105103
};
106104

107-
// animation === false 的时候,onExited 不会触发
108-
// 因此,当 show 变成 false 的时候,认为 exited 完成
105+
// if animation === false, the onExited will not get triggered
106+
// so, when show turned into false, it is considered as exited
109107
useEffect(() => {
110108
if (!show && mergedOptions.animation === false) {
111109
handleExited();
@@ -155,15 +153,26 @@ export function DialogUI({
155153
size
156154
}) {
157155
const { defaultValue = "", refKey = "ref", ...otherInputProps } =
158-
inputProps || {};
156+
inputProps || {};
159157

160-
const [inputValue, setInputValue] = useState(defaultValue);
158+
const [inputValue, setInputValue] = useState("");
161159
const inputRef = useRef();
162160

161+
// only set input value to default value when it shows up
162+
useEffect(() => {
163+
if (show) {
164+
setInputValue(defaultValue);
165+
}
166+
}, [show, defaultValue]);
167+
168+
// if it is closed without animation, clear the input directly
163169
useEffect(() => {
164-
setInputValue(defaultValue);
165-
}, [defaultValue]);
170+
if (!show && animation === false) {
171+
setInputValue("");
172+
}
173+
}, [show, animation]);
166174

175+
// set autoFocus properly
167176
useEffect(() => {
168177
if (show && input && autoFocus) {
169178
inputRef.current.focus();
@@ -201,12 +210,18 @@ export function DialogUI({
201210
setInputValue(e.target.value);
202211
}
203212

213+
function handleExited() {
214+
// clear the input value on finish of the animation
215+
setInputValue("");
216+
onExited();
217+
}
218+
204219
return (
205220
<Modal
206221
show={show}
207222
onHide={handleHide}
208223
centered={centered}
209-
onExited={onExited}
224+
onExited={handleExited}
210225
animation={animation}
211226
scrollable={scrollable}
212227
size={size}

0 commit comments

Comments
 (0)