Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stepper): stepper输入小数 #1113

Merged
merged 7 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,33 @@ export const reconvertUnit = (val: string | number | undefined) => {
if (val == null) return 0;
return isNumber(val) ? Number(val) : Number(val.slice(0, -2));
};

/**
* 格式化数字
* @param value 传入的数字字符串
* @param allowDecimal 是否允许小数,默认为 true
* @param allowNegative 是否允许负数,默认为 true
* @returns 返回格式化后的数字字符串
*/
export const formatNumber = (value: string, allowDecimal = true, allowNegative = true) => {
if (allowDecimal) {
const index = value.indexOf('.');
if (index !== -1) {
value = `${value.slice(0, index + 1)}${value.slice(index).replace(/\./g, '')}`;
}
} else {
const [splitValue = ''] = value.split('.');
value = splitValue;
}

if (allowNegative) {
const index = value.indexOf('-');
if (index !== -1) {
value = `${value.slice(0, index + 1)}${value.slice(index).replace(/-/g, '')}`;
}
} else {
value = value.replace(/-/g, '');
}

return value.replace(/[^\d.-]/g, '');
};
5 changes: 5 additions & 0 deletions src/stepper/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export default {
type: [String, Number] as PropType<TdStepperProps['defaultValue']>,
default: 0,
},
/** 是否整数,为 true 仅允许输入不带小数点的数,为 false 允许输入带小数点的数 */
integer: {
anlyyao marked this conversation as resolved.
Show resolved Hide resolved
type: Boolean,
default: true,
},
/** 输入框失去焦点时触发 */
onBlur: Function as PropType<TdStepperProps['onBlur']>,
/** 数值发生变更时触发 */
Expand Down
14 changes: 8 additions & 6 deletions src/stepper/stepper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
`${name}__input--${size}`,
`${disabled ? name + '--' + theme + '-disabled' : ''}`,
]"
type="tel"
:type="integer ? 'tel' : 'text'"
:inputmode="integer ? 'numeric' : 'decimal'"
:style="inputStyle"
:disabled="disableInput || disabled"
:readonly="disableInput"
Expand Down Expand Up @@ -47,7 +48,7 @@ import { toRefs, computed, defineComponent } from 'vue';
import { AddIcon, RemoveIcon } from 'tdesign-icons-vue-next';
import config from '../config';
import StepperProps from './props';
import { useDefault } from '../shared';
import { useDefault, formatNumber } from '../shared';
import { TdStepperProps } from './type';
import { useFormDisabled } from '../form/hooks';

Expand All @@ -64,7 +65,7 @@ export default defineComponent({
setup(props, context) {
const [stepperValue] = useDefault<TdStepperProps['value'], TdStepperProps>(props, context.emit, 'value', 'change');
const disabled = useFormDisabled();
const { min, max, step } = toRefs(props);
const { min, max, step, integer } = toRefs(props);
const inputStyle = computed(() => (props.inputWidth ? { width: `${props.inputWidth}px` } : ''));

const isDisabled = (type: 'minus' | 'plus') => {
Expand Down Expand Up @@ -94,12 +95,12 @@ export default defineComponent({

const formatValue = (value: number) => {
return Math.max(Math.min(max.value, value, Number.MAX_SAFE_INTEGER), min.value, Number.MIN_SAFE_INTEGER).toFixed(
getLen(step.value),
Math.max(getLen(step.value), getLen(value)),
);
};

const updateValue = (value: TdStepperProps['value']) => {
stepperValue.value = value;
stepperValue.value = Number(formatNumber(`${value}`, !integer.value));
};

const plusValue = () => {
Expand All @@ -119,7 +120,7 @@ export default defineComponent({
};

const handleInput = (e: Event) => {
fennghuang marked this conversation as resolved.
Show resolved Hide resolved
const value = (e.target as HTMLTextAreaElement).value.replace(/[^\d]/g, '');
const value = formatNumber((e.target as HTMLTextAreaElement).value, !integer.value);
stepperValue.value = Number(value);
};

Expand Down Expand Up @@ -148,6 +149,7 @@ export default defineComponent({
handleBlur,
...toRefs(props),
disabled,
integer,
};
},
});
Expand Down