Skip to content

Commit de545ae

Browse files
author
Edward Xiao
committed
- fix <Select/> item onClick cached issue
1 parent a6205e9 commit de545ae

File tree

8 files changed

+21
-9
lines changed

8 files changed

+21
-9
lines changed

lib/components/Select.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ var component = function component(_ref) {
257257
setInternalValue = _useState8[1];
258258

259259
var prevInternalValue = (0, _const.usePrevious)(internalValue);
260+
var prevOptionList = (0, _const.usePrevious)(optionList);
260261

261262
var _useState9 = useState(false),
262263
_useState10 = _slicedToArray(_useState9, 2),
@@ -579,6 +580,11 @@ var component = function component(_ref) {
579580
useEffect(function () {
580581
setInternalValue(String(value));
581582
}, [value]);
583+
useEffect(function () {
584+
if (prevOptionList && prevOptionList !== optionList) {
585+
setInternalValue(String(optionList[0].id));
586+
}
587+
}, [optionList]);
582588
useEffect(function () {
583589
if (typeof prevInternalValue !== 'undefined' && prevInternalValue !== internalValue) {
584590
check();
@@ -712,7 +718,7 @@ var Option = memo(function (_ref2) {
712718
onMouseOut = _ref2$onMouseOut === void 0 ? function () {} : _ref2$onMouseOut;
713719
var handleOnClick = useCallback(function (e) {
714720
onClick(item, e);
715-
}, []);
721+
}, [item]);
716722
var handleOnMouseOver = useCallback(function () {
717723
onMouseOver(index);
718724
}, []);

lib/react-inputs-validation.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-inputs-validation.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-inputs-validation",
3-
"version": "4.1.4",
3+
"version": "4.1.6",
44
"description": "React form input validation components",
55
"main": "index.js",
66
"repository": {

src/__tests__/Select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('Select component', () => {
203203
});
204204

205205
it('[asyncObj]: Should show error', () => {
206-
const wrapper = mount(<Select onBlur={() => {}} asyncMsgObj={{}} />);
206+
const wrapper = mount(<Select onBlur={() => {}} optionList={OPTION_LIST} asyncMsgObj={{}} />);
207207
const $wrapper = wrapper.find(WRAPPER);
208208
$wrapper.simulate('focus');
209209
$wrapper.simulate('blur');
@@ -226,7 +226,7 @@ describe('Select component', () => {
226226
});
227227

228228
it('[asyncObj]: Should show success', () => {
229-
const wrapper = mount(<Select onBlur={() => {}} asyncMsgObj={{}} />);
229+
const wrapper = mount(<Select onBlur={() => {}} optionList={OPTION_LIST} asyncMsgObj={{}} />);
230230
const $wrapper = wrapper.find(WRAPPER);
231231
$wrapper.simulate('focus');
232232
$wrapper.simulate('blur');

src/js/Inputs/Select.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ const component: React.FC<Props> = ({
163163
const [successMsg, setSuccessMsg] = useState('');
164164
const [internalValue, setInternalValue] = useState(String(value));
165165
const prevInternalValue = usePrevious(internalValue);
166+
const prevOptionList = usePrevious(optionList);
166167
const [show, setShow] = useState(false);
167168
const [isTyping, setIsTyping] = useState(false);
168169
const initKeycodeList: number[] = [];
@@ -439,6 +440,11 @@ const component: React.FC<Props> = ({
439440
useEffect(() => {
440441
setInternalValue(String(value));
441442
}, [value]);
443+
useEffect(() => {
444+
if (prevOptionList && prevOptionList !== optionList) {
445+
setInternalValue(String(optionList[0].id));
446+
}
447+
}, [optionList]);
442448
useEffect(() => {
443449
if (typeof prevInternalValue !== 'undefined' && prevInternalValue !== internalValue) {
444450
check();
@@ -557,7 +563,7 @@ export const Option: React.FC<OptionProps> = memo(
557563
({ index = -1, id = '', className = '', item = { id: '', name: '' }, customStyleOptionListItem = {}, onClick = () => {}, onMouseOver = () => {}, onMouseMove = () => {}, onMouseOut = () => {} }) => {
558564
const handleOnClick = useCallback((e: React.MouseEvent<HTMLElement>) => {
559565
onClick(item, e);
560-
}, []);
566+
}, [item]);
561567
const handleOnMouseOver = useCallback(() => {
562568
onMouseOver(index);
563569
}, []);

0 commit comments

Comments
 (0)