@kne/react-filter 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1467,16 +1467,17 @@ const withFilterValue = WrappedComponent => {
1467
1467
  };
1468
1468
  };
1469
1469
 
1470
- const _excluded$4 = ["label", "onChange", "value", "placeholder"];
1470
+ const _excluded$4 = ["label", "onChange", "value", "placeholder", "searchDelay"];
1471
1471
  const {
1472
1472
  Search
1473
1473
  } = Input;
1474
- const SearchInput = withFilterValue(_ref => {
1474
+ const SearchInput = withLocale(withFilterValue(_ref => {
1475
1475
  let {
1476
1476
  label,
1477
1477
  onChange,
1478
1478
  value,
1479
- placeholder
1479
+ placeholder,
1480
+ searchDelay = 500
1480
1481
  } = _ref,
1481
1482
  props = _objectWithoutPropertiesLoose(_ref, _excluded$4);
1482
1483
  const {
@@ -1484,21 +1485,74 @@ const SearchInput = withFilterValue(_ref => {
1484
1485
  } = useIntl({
1485
1486
  moduleName: 'Filter'
1486
1487
  });
1488
+ const propsValue = value == null ? void 0 : value.value;
1489
+ const [inputValue, setInputValue] = useState(propsValue || '');
1490
+ const inputValueRef = useRef('');
1491
+ const onChangeRef = useRef(onChange);
1492
+ const searchTimerRef = useRef(null);
1493
+ const isComposingRef = useRef(false);
1494
+ inputValueRef.current = inputValue;
1495
+ onChangeRef.current = onChange;
1496
+ const clearSearchTimer = () => {
1497
+ if (searchTimerRef.current) {
1498
+ clearTimeout(searchTimerRef.current);
1499
+ searchTimerRef.current = null;
1500
+ }
1501
+ };
1502
+ const submitSearch = searchValue => {
1503
+ onChangeRef.current == null || onChangeRef.current(searchValue ? {
1504
+ label: searchValue,
1505
+ value: searchValue
1506
+ } : null);
1507
+ };
1508
+ const scheduleSearch = searchValue => {
1509
+ clearSearchTimer();
1510
+ searchTimerRef.current = setTimeout(() => {
1511
+ searchTimerRef.current = null;
1512
+ submitSearch(searchValue);
1513
+ }, searchDelay);
1514
+ };
1515
+ useEffect(() => {
1516
+ if (propsValue !== inputValueRef.current) {
1517
+ clearSearchTimer();
1518
+ setInputValue(propsValue || '');
1519
+ }
1520
+ }, [propsValue]);
1521
+ useEffect(() => {
1522
+ return () => {
1523
+ clearSearchTimer();
1524
+ };
1525
+ }, []);
1487
1526
  return /*#__PURE__*/jsx(Search, _extends({}, props, {
1488
1527
  placeholder: placeholder || formatMessage({
1489
1528
  id: 'inputPlaceholder'
1490
1529
  }, {
1491
1530
  label
1492
1531
  }),
1493
- value: (value == null ? void 0 : value.value) || '',
1494
- onSearch: value => {
1495
- onChange({
1496
- label: value,
1497
- value
1498
- });
1532
+ value: inputValue,
1533
+ onChange: e => {
1534
+ const nextValue = e.target.value;
1535
+ setInputValue(nextValue);
1536
+ if (!isComposingRef.current) {
1537
+ scheduleSearch(nextValue);
1538
+ }
1539
+ },
1540
+ onCompositionStart: () => {
1541
+ isComposingRef.current = true;
1542
+ clearSearchTimer();
1543
+ },
1544
+ onCompositionEnd: e => {
1545
+ isComposingRef.current = false;
1546
+ const nextValue = e.target.value;
1547
+ setInputValue(nextValue);
1548
+ scheduleSearch(nextValue);
1549
+ },
1550
+ onSearch: searchValue => {
1551
+ clearSearchTimer();
1552
+ submitSearch(searchValue);
1499
1553
  }
1500
1554
  }));
1501
- });
1555
+ }));
1502
1556
 
1503
1557
  /**
1504
1558
  * 从筛选值中提取原始值数组。