x-ui-design 0.4.60 → 0.4.62

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.
@@ -1,4 +1,7 @@
1
1
  export declare const MASK_CHAR = "_";
2
2
  export declare const MASK_REGEX: RegExp;
3
3
  export declare function stripMask(value: string, mask: string, maskChar?: string): string;
4
- export declare function applyMask(raw: string, mask: string, maskChar?: string): string;
4
+ export declare function applyMask(raw: string, mask: string, maskChar?: string): {
5
+ masked: string;
6
+ rawIndex: number;
7
+ };
package/dist/index.esm.js CHANGED
@@ -1013,7 +1013,7 @@ const FormItem$1 = ({
1013
1013
  }, !props.noStyle && (label || name) && /*#__PURE__*/React.createElement("label", {
1014
1014
  className: `${prefixCls}-label`,
1015
1015
  htmlFor: name
1016
- }, label || name, ":", isRequired && /*#__PURE__*/React.createElement("span", {
1016
+ }, label || name, isRequired && /*#__PURE__*/React.createElement("span", {
1017
1017
  className: `${prefixCls}-required`
1018
1018
  }, "*")), Children.map(childrenList, (child, key) => {
1019
1019
  if (/*#__PURE__*/isValidElement(child)) {
@@ -2773,7 +2773,10 @@ function applyMask(raw, mask, maskChar = MASK_CHAR) {
2773
2773
  masked += mChar;
2774
2774
  }
2775
2775
  }
2776
- return masked;
2776
+ return {
2777
+ masked,
2778
+ rawIndex
2779
+ };
2777
2780
  }
2778
2781
 
2779
2782
  var css_248z$b = ".xUi-input-container{align-items:center;background-color:transparent;border:1px solid var(--xui-border-color);border-radius:var(--xui-border-radius-sm);display:flex;overflow:hidden}.xUi-input-container:not(.xUi-input-error):not(.xUi-input-disabled):has(.xUi-input):hover,.xUi-input-container:not(.xUi-input-error):not(.xUi-input-disabled):has(.xUi-input:focus){border:1px solid var(--xui-primary-color)}.xUi-input-container.xUi-input-error{border-color:var(--xui-error-color)}.xUi-input-container.xUi-input-error .error-svg-icon,.xUi-input-suffix .error-svg-icon{color:var(--xui-error-color)}.xUi-input-wrapper{align-items:center;display:flex;flex-grow:1;margin:-1px;position:relative;transition:border .3s}.xUi-input,.xUi-input-wrapper{background-color:transparent;height:-webkit-fill-available}.xUi-input{border:none;color:var(--xui-text-color);flex:1;outline:none;padding:.1px 7px;width:100%}.xUi-input:placeholder-shown{text-overflow:ellipsis}.xUi-input::placeholder{color:var(--xui-text-color);opacity:.6}.xUi-input-prefix,.xUi-input-suffix{background-color:transparent;gap:4px}.xUi-input-addon,.xUi-input-prefix,.xUi-input-suffix{align-items:center;color:var(--xui-text-color);display:flex;height:-webkit-fill-available;padding:0 7px}.xUi-input-addon.xUi-input-after{border-left:1px solid var(--xui-border-color)}.xUi-input-addon.xUi-input-before{border-right:1px solid var(--xui-border-color)}.xUi-input-large .xUi-input-addon{padding:0 10px}.xUi-input-clear{align-items:center;cursor:pointer;display:flex;margin:0 5px;position:relative;width:16px}.xUi-input-clear svg{color:var(--xui-text-color)}.xUi-input-disabled,.xUi-input-disabled .xUi-input,.xUi-input-disabled .xUi-input-suffix{background-color:var(--xui-color-disabled);cursor:not-allowed}.xUi-input-small{height:22px}.xUi-input-large .xUi-input-clear,.xUi-input-small .xUi-input,.xUi-input-small .xUi-input::placeholder{font-size:var(--xui-font-size-md)}.xUi-input-middle{border-radius:var(--xui-border-radius-md);height:30px}.xUi-input-large .xUi-input-clear,.xUi-input-middle .xUi-input,.xUi-input-middle .xUi-input::placeholder{font-size:var(--xui-font-size-md)}.xUi-input-large{border-radius:var(--xui-border-radius-lg);height:44px}.xUi-input-large .xUi-input,.xUi-input-large .xUi-input-clear,.xUi-input-large .xUi-input::placeholder{font-size:var(--xui-font-size-lg)}";
@@ -2810,8 +2813,10 @@ const InputComponent = /*#__PURE__*/forwardRef(({
2810
2813
  }, ref) => {
2811
2814
  const inputRef = useRef(null);
2812
2815
  const lastKeyPressed = useRef(null);
2813
- const internalValue = mask ? applyMask(stripMask(`${value ?? ''}`, mask, maskChar), mask, maskChar) : value ?? '';
2816
+ const internalValue = mask ? applyMask(stripMask(`${value ?? ''}`, mask, maskChar), mask, maskChar).masked : value ?? '';
2817
+ const [maskValue, setMaskValue] = useState(internalValue);
2814
2818
  const [iconRenderVisible, setIconRenderVisible] = useState(false);
2819
+ const animationRef = useRef(null);
2815
2820
  useImperativeHandle(ref, () => ({
2816
2821
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2817
2822
  // @ts-expect-error
@@ -2825,43 +2830,55 @@ const InputComponent = /*#__PURE__*/forwardRef(({
2825
2830
  }
2826
2831
  }
2827
2832
  }));
2828
- // useEffect(() => {
2829
- // setInternalValue(mask ? applyMask(stripMask(`${value ?? ''}`, mask, maskChar), mask, maskChar) : `${value ?? ''}`);
2830
- // }, [value, mask, maskChar]);
2833
+ useEffect(() => {
2834
+ if (mask) {
2835
+ setMaskValue(applyMask(stripMask(`${value ?? ''}`, mask, maskChar), mask, maskChar).masked);
2836
+ }
2837
+ }, [value, mask, maskChar]);
2831
2838
  const handleChange = e => {
2832
- const inputEl = inputRef.current;
2833
- if (!inputEl) return;
2834
- const rawInput = e.target.value;
2835
- let nextCaret = inputEl.selectionStart ?? rawInput.length;
2839
+ if (!inputRef.current) return;
2840
+ let rawInput = e.target.value;
2836
2841
  const raw = mask ? rawInput.replace(maskRegex, '') : rawInput;
2837
- const masked = mask ? applyMask(raw, mask, maskChar) : rawInput;
2838
- // setInternalValue(masked);
2839
2842
  if (mask) {
2840
- requestAnimationFrame(() => {
2841
- if (!inputEl) return;
2842
- const isBackspace = lastKeyPressed.current === 'Backspace';
2843
- const isDelete = lastKeyPressed.current === 'Delete';
2844
- while (mask.includes(masked[nextCaret - 1])) {
2845
- if (isBackspace || isDelete) {
2843
+ if (!inputRef.current) return;
2844
+ if (animationRef.current) {
2845
+ cancelAnimationFrame(animationRef.current);
2846
+ }
2847
+ const {
2848
+ masked,
2849
+ rawIndex
2850
+ } = applyMask(raw, mask, maskChar);
2851
+ rawInput = masked;
2852
+ animationRef.current = requestAnimationFrame(() => {
2853
+ const isRemove = lastKeyPressed.current === 'Delete' || lastKeyPressed.current === 'Backspace';
2854
+ let nextCaret = !isRemove ? rawIndex : inputRef.current?.selectionStart ?? 0;
2855
+ while (isRemove ? mask.includes(rawInput[nextCaret - 1]) : maskChar !== rawInput[nextCaret]) {
2856
+ if (!isRemove && !rawInput[nextCaret]) {
2857
+ break;
2858
+ }
2859
+ if (isRemove) {
2846
2860
  nextCaret--;
2847
2861
  } else {
2848
2862
  nextCaret++;
2849
2863
  }
2850
2864
  }
2851
- inputEl.setSelectionRange(nextCaret, nextCaret);
2865
+ inputRef.current?.setSelectionRange(nextCaret, nextCaret);
2852
2866
  });
2853
2867
  }
2868
+ setMaskValue(rawInput);
2854
2869
  const eventWithMaskedValue = {
2855
2870
  ...e,
2856
2871
  target: {
2857
2872
  ...e.target,
2858
- value: masked
2873
+ value: rawInput
2859
2874
  }
2860
2875
  };
2861
2876
  props.onChange?.(eventWithMaskedValue);
2862
2877
  };
2863
2878
  const handleClear = e => {
2864
- // setInternalValue('');
2879
+ if (mask) {
2880
+ setMaskValue('');
2881
+ }
2865
2882
  e.target.value = '';
2866
2883
  props.onChange?.(e);
2867
2884
  };
@@ -2891,7 +2908,7 @@ const InputComponent = /*#__PURE__*/forwardRef(({
2891
2908
  type: iconRenderVisible ? 'text' : 'password'
2892
2909
  } : {}, {
2893
2910
  disabled: disabled,
2894
- value: internalValue,
2911
+ value: maskValue,
2895
2912
  onChange: handleChange,
2896
2913
  onKeyDown: handleOnKeyDown,
2897
2914
  className: clsx([prefixCls, className])