@tendaui/components 1.0.0 → 1.0.2
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.
- package/LICENSE +21 -21
- package/README.md +176 -176
- package/alert/Alert.tsx +3 -2
- package/button/_example/base.tsx +10 -0
- package/button/_example/icon.tsx +20 -0
- package/color-picker/ColorPickPanel.tsx +9 -0
- package/color-picker/ColorPicker.tsx +67 -0
- package/color-picker/components/panel/alpha.tsx +32 -0
- package/color-picker/components/panel/format/index.tsx +47 -0
- package/color-picker/components/panel/format/inputs.tsx +119 -0
- package/color-picker/components/panel/header.tsx +37 -0
- package/color-picker/components/panel/hue.tsx +20 -0
- package/color-picker/components/panel/index.tsx +191 -0
- package/color-picker/components/panel/saturation.tsx +81 -0
- package/color-picker/components/panel/slider.tsx +76 -0
- package/color-picker/components/panel/swatches.tsx +84 -0
- package/color-picker/components/trigger.tsx +49 -0
- package/color-picker/defaultProps.ts +7 -0
- package/color-picker/helpers.ts +53 -0
- package/color-picker/hooks/useClassNames.ts +9 -0
- package/color-picker/hooks/useStyles.ts +39 -0
- package/color-picker/index.ts +12 -0
- package/color-picker/style/css.js +1 -0
- package/color-picker/style/index.js +1 -0
- package/color-picker/type.ts +143 -0
- package/color-picker/utils/color-picker/cmyk.ts +89 -0
- package/color-picker/utils/color-picker/color.ts +467 -0
- package/color-picker/utils/color-picker/constants.ts +187 -0
- package/color-picker/utils/color-picker/draggable.ts +100 -0
- package/color-picker/utils/color-picker/format.ts +95 -0
- package/color-picker/utils/color-picker/gradient.ts +243 -0
- package/color-picker/utils/color-picker/index.ts +7 -0
- package/color-picker/utils/color-picker/types.ts +33 -0
- package/common/observe.ts +33 -0
- package/common.ts +20 -0
- package/config-provider/ConfigContext.tsx +4 -1
- package/config-provider/index.ts +1 -1
- package/dialog/DialogCard.tsx +4 -6
- package/dialog/hooks/useDialogPosition.ts +1 -2
- package/dialog/plugin.tsx +3 -2
- package/drawer/Drawer.tsx +264 -0
- package/drawer/defaultProps.ts +19 -0
- package/drawer/hooks/useDrag.ts +98 -0
- package/drawer/hooks/useLockStyle.ts +36 -0
- package/drawer/index.ts +5 -0
- package/drawer/style/css.js +1 -0
- package/drawer/style/index.js +1 -0
- package/drawer/type.ts +193 -0
- package/drawer/utils/index.ts +76 -0
- package/fireworks/Fireworks.tsx +138 -0
- package/fireworks/index.ts +10 -0
- package/fireworks/style/css.js +0 -0
- package/fireworks/style/index.js +0 -0
- package/fireworks/type.ts +72 -0
- package/form/FormItem.tsx +5 -5
- package/form/easing.ts +10 -0
- package/form/scroll.ts +124 -0
- package/form/type.ts +519 -519
- package/global-config/default-config.ts +95 -0
- package/global-config/locale/ar_KW.ts +270 -0
- package/global-config/locale/en_US.ts +280 -0
- package/global-config/locale/it_IT.ts +287 -0
- package/global-config/locale/ja_JP.ts +279 -0
- package/global-config/locale/ko_KR.ts +279 -0
- package/global-config/locale/ru_RU.ts +288 -0
- package/global-config/locale/zh_CN.ts +279 -0
- package/global-config/locale/zh_TW.ts +279 -0
- package/global-config/mobile/default-config.ts +6 -0
- package/global-config/mobile/locale/ar_KW.ts +113 -0
- package/global-config/mobile/locale/en_US.ts +114 -0
- package/global-config/mobile/locale/it_IT.ts +114 -0
- package/global-config/mobile/locale/ja_JP.ts +101 -0
- package/global-config/mobile/locale/ko_KR.ts +101 -0
- package/global-config/mobile/locale/ru_RU.ts +113 -0
- package/global-config/mobile/locale/zh_CN.ts +101 -0
- package/global-config/mobile/locale/zh_TW.ts +101 -0
- package/global-config/t.ts +111 -0
- package/hooks/useControlled.ts +3 -3
- package/hooks/useDeepEffect.ts +32 -0
- package/hooks/useGlobalIcon.ts +10 -3
- package/hooks/useLastest.ts +2 -6
- package/hooks/useResizeObserve.ts +36 -0
- package/index.ts +10 -7
- package/input/Input.tsx +4 -1
- package/input/defaultProps.ts +0 -2
- package/input/type.ts +1 -6
- package/input-number/InputNumber.tsx +124 -0
- package/input-number/defaultProps.ts +17 -0
- package/input-number/index.ts +9 -0
- package/input-number/style/css.js +1 -0
- package/input-number/style/index.js +1 -0
- package/input-number/type.ts +147 -0
- package/input-number/useInputNumber.tsx +270 -0
- package/ip-input/IPInput.tsx +516 -0
- package/ip-input/defaultProps.ts +11 -0
- package/ip-input/index.ts +3 -0
- package/ip-input/style/css.js +1 -0
- package/ip-input/style/index.js +1 -0
- package/ip-input/type.ts +115 -0
- package/ip-input/utils.ts +112 -0
- package/layout/Aside.tsx +38 -0
- package/layout/Layout.tsx +104 -0
- package/layout/defaultProps.ts +9 -0
- package/layout/index.ts +9 -0
- package/layout/style/css.js +1 -0
- package/layout/style/index.js +1 -0
- package/layout/type.ts +43 -0
- package/list/List.tsx +144 -0
- package/list/ListItem.tsx +36 -0
- package/list/ListItemMeta.tsx +40 -0
- package/list/defaultProps.ts +11 -0
- package/list/hooks/useListVirtualScroll.ts +82 -0
- package/list/index.ts +11 -0
- package/list/style/css.js +1 -0
- package/list/style/index.js +1 -0
- package/list/type.ts +93 -0
- package/locale/LocalReceiver.ts +55 -0
- package/locale/ar_KW.ts +7 -0
- package/locale/en_US.ts +7 -0
- package/locale/it_IT.ts +6 -0
- package/locale/ja_JP.ts +6 -0
- package/locale/ko_KR.ts +6 -0
- package/locale/ru_RU.ts +6 -0
- package/locale/zh_CN.ts +5 -0
- package/locale/zh_TW.ts +7 -0
- package/notification/NotifyContainer.tsx +2 -2
- package/notification/NotifyContext.tsx +1 -0
- package/package.json +6 -3
- package/popup/Popup.tsx +34 -10
- package/radio/Radio.tsx +24 -0
- package/radio/RadioGroup.tsx +159 -0
- package/radio/defaultProps.ts +18 -0
- package/radio/index.ts +12 -0
- package/radio/style/css.js +0 -0
- package/radio/style/index.js +1 -0
- package/radio/type.ts +115 -0
- package/radio/useKeyboard.ts +36 -0
- package/select/hooks/useOptions.ts +10 -7
- package/select/hooks/usePanelVirtualScroll.ts +1 -1
- package/select/type.ts +382 -382
- package/select-input/type.ts +280 -280
- package/slider/Slider.tsx +270 -0
- package/slider/SliderHandleButton.tsx +50 -0
- package/slider/defaultProps.ts +15 -0
- package/slider/index.ts +9 -0
- package/slider/style/css.js +1 -0
- package/slider/style/index.js +1 -0
- package/slider/type.ts +77 -0
- package/style/all.js +26 -0
- package/styles/_global.scss +39 -39
- package/styles/_vars.scss +358 -386
- package/styles/components/alert/_index.scss +175 -175
- package/styles/components/alert/_vars.scss +39 -39
- package/styles/components/badge/_index.scss +70 -70
- package/styles/components/badge/_vars.scss +25 -25
- package/styles/components/button/_index.scss +499 -511
- package/styles/components/button/_mixins.scss +39 -39
- package/styles/components/button/_vars.scss +120 -122
- package/styles/components/checkbox/_index.scss +158 -158
- package/styles/components/checkbox/_var.scss +60 -60
- package/styles/components/color-picker/_index.scss +586 -0
- package/styles/components/color-picker/_mixins.scss +0 -0
- package/styles/components/color-picker/_vars.scss +84 -0
- package/styles/components/dialog/_animate.scss +135 -135
- package/styles/components/dialog/_index.scss +311 -311
- package/styles/components/dialog/_vars.scss +59 -59
- package/styles/components/drawer/_index.scss +205 -0
- package/styles/components/drawer/_mixins.scss +1 -0
- package/styles/components/drawer/_var.scss +53 -0
- package/styles/components/fireworks/_index.scss +86 -0
- package/styles/components/fireworks/_vars.scss +4 -0
- package/styles/components/form/_index.scss +174 -174
- package/styles/components/form/_mixins.scss +76 -76
- package/styles/components/form/_vars.scss +100 -100
- package/styles/components/input/_index.scss +349 -349
- package/styles/components/input/_mixins.scss +116 -116
- package/styles/components/input/_vars.scss +134 -134
- package/styles/components/input-number/_index.scss +353 -0
- package/styles/components/input-number/_mixins.scss +0 -0
- package/styles/components/input-number/_vars.scss +65 -0
- package/styles/components/ip-input/_index.scss +280 -0
- package/styles/components/layout/_index.scss +47 -0
- package/styles/components/layout/_mixin.scss +0 -0
- package/styles/components/layout/_vars.scss +18 -0
- package/styles/components/layout/doc.scss +74 -0
- package/styles/components/list/_index.scss +172 -0
- package/styles/components/list/_mixins.scss +0 -0
- package/styles/components/list/_vars.scss +41 -0
- package/styles/components/loading/_index.scss +112 -112
- package/styles/components/loading/_vars.scss +39 -39
- package/styles/components/notification/_index.scss +160 -160
- package/styles/components/notification/_mixins.scss +12 -12
- package/styles/components/notification/_vars.scss +59 -59
- package/styles/components/popup/_index.scss +82 -82
- package/styles/components/popup/_mixin.scss +149 -149
- package/styles/components/popup/_var.scss +31 -31
- package/styles/components/radio/_index.scss +376 -0
- package/styles/components/radio/_mixins.scss +0 -0
- package/styles/components/radio/_var.scss +92 -0
- package/styles/components/select/_index.scss +290 -290
- package/styles/components/select/_var.scss +65 -65
- package/styles/components/select-input/_index.scss +5 -5
- package/styles/components/select-input/_var.scss +3 -3
- package/styles/components/slider/_index.scss +241 -0
- package/styles/components/slider/_mixins.scss +0 -0
- package/styles/components/slider/_vars.scss +50 -0
- package/styles/components/switch/_index.scss +279 -279
- package/styles/components/switch/_vars.scss +61 -61
- package/styles/components/table/_index.scss +193 -0
- package/styles/components/table/_var.scss +52 -0
- package/styles/components/tabs/_index.scss +165 -0
- package/styles/components/tabs/_mixins.scss +11 -0
- package/styles/components/tabs/_vars.scss +71 -0
- package/styles/components/tag/_index.scss +316 -316
- package/styles/components/tag/_var.scss +85 -85
- package/styles/components/tag-input/_index.scss +163 -163
- package/styles/components/tag-input/_vars.scss +16 -16
- package/styles/globals.css +250 -250
- package/styles/mixins/_focus.scss +7 -7
- package/styles/mixins/_layout.scss +32 -32
- package/styles/mixins/_reset.scss +10 -10
- package/styles/mixins/_scrollbar.scss +31 -31
- package/styles/mixins/_text.scss +48 -48
- package/styles/rillple.css +16 -16
- package/styles/scrollbar.css +41 -41
- package/styles/themes/_dark.scss +191 -191
- package/styles/themes/_font.scss +69 -79
- package/styles/themes/_index.scss +5 -5
- package/styles/themes/_light.scss +190 -190
- package/styles/themes/_radius.scss +9 -9
- package/styles/themes/_size.scss +68 -68
- package/styles/themes.css +66 -66
- package/styles/utilities/_animation.scss +57 -57
- package/styles/utilities/_tips.scss +9 -9
- package/tab/TabBar.tsx +85 -0
- package/tab/TabNav.tsx +103 -0
- package/tab/TabNavItem.tsx +80 -0
- package/tab/TabPanel.tsx +42 -0
- package/tab/Tabs.tsx +71 -0
- package/tab/defaultProps.ts +19 -0
- package/tab/index.ts +7 -0
- package/tab/style/index.js +1 -0
- package/tab/type.ts +125 -0
- package/tab/useTabClass.ts +20 -0
- package/table/Cell.tsx +109 -0
- package/table/TBody.tsx +77 -0
- package/table/THead.tsx +63 -0
- package/table/TR.tsx +78 -0
- package/table/Table.tsx +73 -0
- package/table/defaultProps.ts +14 -0
- package/table/hooks/index.ts +4 -0
- package/table/hooks/useTableClassName.ts +63 -0
- package/table/hooks/useTableStyle.ts +93 -0
- package/table/index.ts +7 -0
- package/table/style/css.js +1 -0
- package/table/style/index.js +1 -0
- package/table/type.ts +192 -0
- package/tag/Tag.tsx +1 -1
- package/tag-input/hooks/useTagList.tsx +1 -1
- package/utils/dom.ts +4 -0
- package/utils/forwardRefWithStatics.ts +1 -4
- package/utils/input-number/large-number.ts +423 -0
- package/utils/input-number/number.ts +257 -0
- package/utils/isFragment.ts +6 -6
- package/utils/log/index.ts +3 -0
- package/utils/log/log.ts +30 -0
- package/utils/log/types.ts +12 -0
- package/utils/number.ts +21 -0
- package/utils/scroll.ts +26 -0
- package/utils/style.ts +2 -4
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { InputProps } from "../input";
|
|
2
|
+
import { TNode } from "../common";
|
|
3
|
+
import { MouseEvent, KeyboardEvent, FocusEvent, FormEvent, CompositionEvent } from "react";
|
|
4
|
+
|
|
5
|
+
export interface TdInputNumberProps<T = InputNumberValue> {
|
|
6
|
+
/**
|
|
7
|
+
* 文本内容位置,居左/居中/居右
|
|
8
|
+
*/
|
|
9
|
+
align?: "left" | "center" | "right";
|
|
10
|
+
/**
|
|
11
|
+
* 是否允许输入超过 `max` `min` 范围外的数字。为保障用户体验,仅在失去焦点时进行数字范围矫正。默认允许超出,数字超出范围时,输入框变红提醒
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
allowInputOverLimit?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 宽度随内容自适应
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
autoWidth?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* [小数位数](https://en.wiktionary.org/wiki/decimal_place)
|
|
22
|
+
*/
|
|
23
|
+
decimalPlaces?: InputNumberDecimalPlaces;
|
|
24
|
+
/**
|
|
25
|
+
* 禁用组件
|
|
26
|
+
*/
|
|
27
|
+
disabled?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* 格式化输入框展示值。第二个事件参数 `context.fixedNumber` 表示处理过小数位数 `decimalPlaces` 的数字
|
|
30
|
+
*/
|
|
31
|
+
format?: (value: InputNumberValue, context?: { fixedNumber?: InputNumberValue }) => InputNumberValue;
|
|
32
|
+
/**
|
|
33
|
+
* 透传 Input 输入框组件全部属性
|
|
34
|
+
*/
|
|
35
|
+
inputProps?: InputProps;
|
|
36
|
+
/**
|
|
37
|
+
* 左侧文本
|
|
38
|
+
*/
|
|
39
|
+
label?: TNode;
|
|
40
|
+
/**
|
|
41
|
+
* 是否作为大数使用。JS 支持的最大数字位数是 16 位,超过 16 位的数字需作为字符串大数处理。此时,数据类型必须保持为字符串,否则会丢失数据
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
largeNumber?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 最大值。如果是大数,请传入字符串
|
|
47
|
+
* @default Infinity
|
|
48
|
+
*/
|
|
49
|
+
max?: InputNumberValue;
|
|
50
|
+
/**
|
|
51
|
+
* 最小值。如果是大数,请传入字符串
|
|
52
|
+
* @default -Infinity
|
|
53
|
+
*/
|
|
54
|
+
min?: InputNumberValue;
|
|
55
|
+
/**
|
|
56
|
+
* 占位符
|
|
57
|
+
*/
|
|
58
|
+
placeholder?: string;
|
|
59
|
+
/**
|
|
60
|
+
* 只读状态
|
|
61
|
+
*/
|
|
62
|
+
readonly?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* 组件尺寸
|
|
65
|
+
* @default medium
|
|
66
|
+
*/
|
|
67
|
+
size?: "small" | "medium" | "large";
|
|
68
|
+
/**
|
|
69
|
+
* 文本框状态
|
|
70
|
+
* @default default
|
|
71
|
+
*/
|
|
72
|
+
status?: "default" | "success" | "warning" | "error";
|
|
73
|
+
/**
|
|
74
|
+
* 数值改变步数,可以是小数。如果是大数,请保证数据类型为字符串
|
|
75
|
+
* @default 1
|
|
76
|
+
*/
|
|
77
|
+
step?: InputNumberValue;
|
|
78
|
+
/**
|
|
79
|
+
* 后置内容
|
|
80
|
+
*/
|
|
81
|
+
suffix?: TNode;
|
|
82
|
+
/**
|
|
83
|
+
* 按钮布局
|
|
84
|
+
* @default row
|
|
85
|
+
*/
|
|
86
|
+
theme?: "column" | "row" | "normal";
|
|
87
|
+
/**
|
|
88
|
+
* 输入框下方提示文本,会根据不同的 `status` 呈现不同的样式
|
|
89
|
+
*/
|
|
90
|
+
tips?: TNode;
|
|
91
|
+
/**
|
|
92
|
+
* 数字输入框的值。当值为 '' 时,输入框显示为空
|
|
93
|
+
*/
|
|
94
|
+
value?: T;
|
|
95
|
+
/**
|
|
96
|
+
* 数字输入框的值。当值为 '' 时,输入框显示为空,非受控属性
|
|
97
|
+
*/
|
|
98
|
+
defaultValue?: T;
|
|
99
|
+
/**
|
|
100
|
+
* 失去焦点时触发
|
|
101
|
+
*/
|
|
102
|
+
onBlur?: (value: InputNumberValue, context: { e: FocusEvent<HTMLDivElement> }) => void;
|
|
103
|
+
/**
|
|
104
|
+
* 值变化时触发,`type` 表示触发本次变化的来源
|
|
105
|
+
*/
|
|
106
|
+
onChange?: (value: T, context: ChangeContext) => void;
|
|
107
|
+
/**
|
|
108
|
+
* 回车键按下时触发
|
|
109
|
+
*/
|
|
110
|
+
onEnter?: (value: InputNumberValue, context: { e: KeyboardEvent<HTMLDivElement> }) => void;
|
|
111
|
+
/**
|
|
112
|
+
* 获取焦点时触发
|
|
113
|
+
*/
|
|
114
|
+
onFocus?: (value: InputNumberValue, context: { e: FocusEvent<HTMLDivElement> }) => void;
|
|
115
|
+
/**
|
|
116
|
+
* 键盘按下时触发
|
|
117
|
+
*/
|
|
118
|
+
onKeydown?: (value: InputNumberValue, context: { e: KeyboardEvent<HTMLDivElement> }) => void;
|
|
119
|
+
/**
|
|
120
|
+
* 按下字符键时触发(keydown -> keypress -> keyup)
|
|
121
|
+
*/
|
|
122
|
+
onKeypress?: (value: InputNumberValue, context: { e: KeyboardEvent<HTMLDivElement> }) => void;
|
|
123
|
+
/**
|
|
124
|
+
* 释放键盘时触发
|
|
125
|
+
*/
|
|
126
|
+
onKeyup?: (value: InputNumberValue, context: { e: KeyboardEvent<HTMLDivElement> }) => void;
|
|
127
|
+
/**
|
|
128
|
+
* 最大值或最小值校验结束后触发,`exceed-maximum` 表示超出最大值,`below-minimum` 表示小于最小值
|
|
129
|
+
*/
|
|
130
|
+
onValidate?: (context: { error?: "exceed-maximum" | "below-minimum" }) => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type InputNumberDecimalPlaces = number | { enableRound: boolean; places: number };
|
|
134
|
+
|
|
135
|
+
export type InputNumberValue = number | string;
|
|
136
|
+
|
|
137
|
+
export interface ChangeContext {
|
|
138
|
+
type: ChangeSource;
|
|
139
|
+
e:
|
|
140
|
+
| FormEvent<HTMLInputElement>
|
|
141
|
+
| MouseEvent<HTMLDivElement | SVGElement>
|
|
142
|
+
| FocusEvent<HTMLDivElement>
|
|
143
|
+
| KeyboardEvent<HTMLDivElement>
|
|
144
|
+
| CompositionEvent<HTMLDivElement>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type ChangeSource = "add" | "reduce" | "input" | "blur" | "enter" | "clear" | "props";
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect } from "react";
|
|
2
|
+
import classNames from "classnames";
|
|
3
|
+
import {
|
|
4
|
+
canAddNumber,
|
|
5
|
+
canInputNumber,
|
|
6
|
+
canReduceNumber,
|
|
7
|
+
formatUnCompleteNumber,
|
|
8
|
+
canSetValue,
|
|
9
|
+
getMaxOrMinValidateResult,
|
|
10
|
+
getStepValue,
|
|
11
|
+
formatThousandths,
|
|
12
|
+
largeNumberToFixed
|
|
13
|
+
} from "../utils/input-number/number";
|
|
14
|
+
import useConfig from "../hooks/useConfig";
|
|
15
|
+
import useControlled from "../hooks/useControlled";
|
|
16
|
+
import useCommonClassName from "../hooks/useCommonClassName";
|
|
17
|
+
import { InputNumberValue, TdInputNumberProps } from "./type";
|
|
18
|
+
import { InputProps } from "../input";
|
|
19
|
+
|
|
20
|
+
export const specialCode = ["-", ".", "e", "E"];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 独立一个组件 Hook 方便用户直接使用相关逻辑 自定义任何样式的数字输入框
|
|
24
|
+
*/
|
|
25
|
+
export default function useInputNumber<T extends InputNumberValue = InputNumberValue>(props: TdInputNumberProps<T>) {
|
|
26
|
+
const { SIZE, STATUS } = useCommonClassName();
|
|
27
|
+
const { classPrefix } = useConfig();
|
|
28
|
+
const [tValue, onChange] = useControlled(props, "value", props.onChange);
|
|
29
|
+
const [userInput, setUserInput] = useState("");
|
|
30
|
+
const [isError, setIsError] = useState<"exceed-maximum" | "below-minimum">();
|
|
31
|
+
|
|
32
|
+
const inputRef = useRef(null);
|
|
33
|
+
|
|
34
|
+
const { max, min, largeNumber, decimalPlaces, allowInputOverLimit, onValidate } = props;
|
|
35
|
+
|
|
36
|
+
const disabledReduce = props.disabled || !canReduceNumber(tValue, props.min, props.largeNumber);
|
|
37
|
+
const disabledAdd = props.disabled || !canAddNumber(tValue, props.max, props.largeNumber);
|
|
38
|
+
|
|
39
|
+
const wrapClasses = classNames(`${classPrefix}-input-number`, SIZE[props.size], {
|
|
40
|
+
[STATUS.disabled]: props.disabled,
|
|
41
|
+
[`${classPrefix}-is-controls-right`]: props.theme === "column",
|
|
42
|
+
[`${classPrefix}-input-number--${props.theme}`]: props.theme,
|
|
43
|
+
[`${classPrefix}-input-number--auto-width`]: props.autoWidth
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const reduceClasses = classNames(`${classPrefix}-input-number__decrease`, { [STATUS.disabled]: disabledReduce });
|
|
47
|
+
const addClasses = classNames(`${classPrefix}-input-number__increase`, { [STATUS.disabled]: disabledAdd });
|
|
48
|
+
|
|
49
|
+
const getUserInput = (value: InputNumberValue) => {
|
|
50
|
+
if (!value && value !== 0) return "";
|
|
51
|
+
let inputStr = value || value === 0 ? String(value) : "";
|
|
52
|
+
if (!inputRef.current.currentElement.contains?.(document.activeElement)) {
|
|
53
|
+
const num = formatUnCompleteNumber(inputStr, {
|
|
54
|
+
decimalPlaces,
|
|
55
|
+
largeNumber,
|
|
56
|
+
isToFixed: true
|
|
57
|
+
});
|
|
58
|
+
inputStr = num || num === 0 ? String(num) : "";
|
|
59
|
+
if (props.format) {
|
|
60
|
+
inputStr = String(props.format(value, { fixedNumber: inputStr }));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return inputStr;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const inputValue = [undefined, null].includes(tValue) ? "" : String(tValue);
|
|
68
|
+
// userInput.value 为非合法数字,则表示用户正在输入,此时无需处理
|
|
69
|
+
if (!largeNumber && !Number.isNaN(userInput)) {
|
|
70
|
+
if (parseFloat(userInput) !== tValue) {
|
|
71
|
+
setUserInput(getUserInput(inputValue));
|
|
72
|
+
}
|
|
73
|
+
const fixedNumber = Number(largeNumberToFixed(inputValue, decimalPlaces, largeNumber));
|
|
74
|
+
if (
|
|
75
|
+
decimalPlaces !== undefined &&
|
|
76
|
+
![undefined, null].includes(tValue) &&
|
|
77
|
+
Number(fixedNumber) !== Number(tValue)
|
|
78
|
+
) {
|
|
79
|
+
onChange(fixedNumber as T, { type: "props", e: undefined });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (largeNumber) {
|
|
83
|
+
const tmpUserInput = getUserInput(inputValue);
|
|
84
|
+
setUserInput(tmpUserInput);
|
|
85
|
+
if (decimalPlaces !== undefined && largeNumberToFixed(inputValue, decimalPlaces, largeNumber) !== tValue) {
|
|
86
|
+
onChange(tmpUserInput as T, { type: "props", e: undefined });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// eslint-disable-next-line
|
|
90
|
+
}, [tValue]);
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
if ([undefined, "", null].includes(tValue)) return;
|
|
95
|
+
const error = getMaxOrMinValidateResult({
|
|
96
|
+
value: tValue as InputNumberValue,
|
|
97
|
+
max,
|
|
98
|
+
min,
|
|
99
|
+
largeNumber
|
|
100
|
+
});
|
|
101
|
+
setIsError(error);
|
|
102
|
+
onValidate?.({ error });
|
|
103
|
+
}, [tValue, max, min, largeNumber, onValidate]);
|
|
104
|
+
|
|
105
|
+
const handleStepValue = (op: "add" | "reduce") => {
|
|
106
|
+
const newValue = getStepValue({
|
|
107
|
+
op,
|
|
108
|
+
step: props.step,
|
|
109
|
+
max: props.max,
|
|
110
|
+
min: props.min,
|
|
111
|
+
lastValue: tValue,
|
|
112
|
+
largeNumber: props.largeNumber
|
|
113
|
+
});
|
|
114
|
+
const { largeNumber, max, min } = props;
|
|
115
|
+
const overLimit = getMaxOrMinValidateResult({
|
|
116
|
+
value: newValue,
|
|
117
|
+
largeNumber,
|
|
118
|
+
max,
|
|
119
|
+
min
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
overLimit,
|
|
123
|
+
newValue
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const handleReduce = (e: any) => {
|
|
128
|
+
if (disabledReduce || props.readonly) return;
|
|
129
|
+
const r = handleStepValue("reduce");
|
|
130
|
+
if (r.overLimit && !allowInputOverLimit) return;
|
|
131
|
+
onChange(r.newValue, { type: "reduce", e });
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const handleAdd = (e: any) => {
|
|
135
|
+
if (disabledAdd || props.readonly) return;
|
|
136
|
+
const r = handleStepValue("add");
|
|
137
|
+
if (r.overLimit && !allowInputOverLimit) return;
|
|
138
|
+
onChange(r.newValue, { type: "add", e });
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// 1.2 -> 1. -> 1
|
|
142
|
+
const onInnerInputChange: InputProps["onChange"] = (inputValue, { e }) => {
|
|
143
|
+
// 处理千分位
|
|
144
|
+
const val = formatThousandths(inputValue);
|
|
145
|
+
if (!canInputNumber(val, largeNumber)) return;
|
|
146
|
+
|
|
147
|
+
setUserInput(val);
|
|
148
|
+
|
|
149
|
+
if (largeNumber) {
|
|
150
|
+
onChange(val as T, { type: "input", e: e as any });
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (canSetValue(String(val), Number(tValue))) {
|
|
155
|
+
const newVal = val === "" ? undefined : Number(val);
|
|
156
|
+
onChange(newVal as T, { type: "input", e: e as any });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const handleBlur = (value: string, ctx: { e: React.FocusEvent<HTMLDivElement, Element> }) => {
|
|
161
|
+
if (!props.allowInputOverLimit && value !== undefined) {
|
|
162
|
+
const r = getMaxOrMinValidateResult({
|
|
163
|
+
value: tValue,
|
|
164
|
+
largeNumber,
|
|
165
|
+
max,
|
|
166
|
+
min
|
|
167
|
+
});
|
|
168
|
+
if (r === "below-minimum") {
|
|
169
|
+
onChange(min as T, { type: "blur", e: ctx.e });
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (r === "exceed-maximum") {
|
|
173
|
+
onChange(max as T, { type: "blur", e: ctx.e });
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const newValue = formatUnCompleteNumber(value, {
|
|
178
|
+
decimalPlaces,
|
|
179
|
+
largeNumber
|
|
180
|
+
});
|
|
181
|
+
setUserInput(getUserInput(newValue));
|
|
182
|
+
if (newValue !== tValue) {
|
|
183
|
+
setUserInput(tValue as string);
|
|
184
|
+
onChange(newValue as T, { type: "blur", e: ctx.e });
|
|
185
|
+
}
|
|
186
|
+
props.onBlur?.(newValue, ctx);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const handleFocus = (_: string, ctx: { e: React.FocusEvent<HTMLDivElement, Element> }) => {
|
|
190
|
+
setUserInput(tValue as string);
|
|
191
|
+
props.onFocus?.(tValue as string, ctx);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const handleKeydown = (value: string, ctx: { e: React.KeyboardEvent<HTMLDivElement> }) => {
|
|
195
|
+
const { e } = ctx;
|
|
196
|
+
const keyEvent = {
|
|
197
|
+
ArrowUp: handleAdd,
|
|
198
|
+
ArrowDown: handleReduce
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
if (keyEvent[e.key] !== undefined) {
|
|
202
|
+
keyEvent[e.key](e);
|
|
203
|
+
}
|
|
204
|
+
props.onKeydown?.(value, ctx);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const handleKeyup = (value: string, ctx: { e: React.KeyboardEvent<HTMLDivElement> }) => {
|
|
208
|
+
props.onKeyup?.(value, ctx);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const handleKeypress = (value: string, ctx: { e: React.KeyboardEvent<HTMLDivElement> }) => {
|
|
212
|
+
props.onKeypress?.(value, ctx);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const handleEnter = (value: string, ctx: { e: React.KeyboardEvent<HTMLDivElement> }) => {
|
|
216
|
+
setUserInput(getUserInput(value));
|
|
217
|
+
const newValue = formatUnCompleteNumber(value, {
|
|
218
|
+
decimalPlaces: props.decimalPlaces,
|
|
219
|
+
largeNumber: props.largeNumber
|
|
220
|
+
});
|
|
221
|
+
if (newValue !== value && String(newValue) !== value) {
|
|
222
|
+
onChange(newValue as T, { type: "enter", e: ctx.e });
|
|
223
|
+
}
|
|
224
|
+
props.onEnter?.(newValue, ctx);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const handleClear: InputProps["onClear"] = ({ e }) => {
|
|
228
|
+
onChange(undefined, { type: "clear", e });
|
|
229
|
+
setUserInput("");
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const focus = () => {
|
|
233
|
+
inputRef.current.focus();
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const blur = () => {
|
|
237
|
+
inputRef.current.blur();
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const listeners = {
|
|
241
|
+
onBlur: handleBlur,
|
|
242
|
+
onFocus: handleFocus,
|
|
243
|
+
onKeydown: handleKeydown,
|
|
244
|
+
onKeyup: handleKeyup,
|
|
245
|
+
onKeypress: handleKeypress,
|
|
246
|
+
onEnter: handleEnter,
|
|
247
|
+
onClick: focus,
|
|
248
|
+
onClear: handleClear
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
classPrefix,
|
|
253
|
+
wrapClasses,
|
|
254
|
+
reduceClasses,
|
|
255
|
+
addClasses,
|
|
256
|
+
inputRef,
|
|
257
|
+
listeners,
|
|
258
|
+
isError,
|
|
259
|
+
setIsError,
|
|
260
|
+
userInput,
|
|
261
|
+
setUserInput,
|
|
262
|
+
tValue,
|
|
263
|
+
focus,
|
|
264
|
+
blur,
|
|
265
|
+
onChange,
|
|
266
|
+
handleReduce,
|
|
267
|
+
handleAdd,
|
|
268
|
+
onInnerInputChange
|
|
269
|
+
};
|
|
270
|
+
}
|