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(slider): fix inputNumberProps bug #941

Merged
Merged
Changes from all 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
37 changes: 12 additions & 25 deletions src/slider/hooks/useSliderInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { computed, Ref } from 'vue';
import { TdSliderProps } from '../type';
import InputNumber, { InputNumberProps } from '../../input-number';

const INPUT_NUMBER_PROPS_INITIAL_STATE: InputNumberProps = {
decimalPlaces: 0,
format: undefined as InputNumberProps['format'],
placeholder: '',
theme: 'column' as InputNumberProps['theme'],
};

interface useSliderInputProps {
inputNumberProps: boolean | TdSliderProps['inputNumberProps'];
max: number;
Expand All @@ -20,30 +27,13 @@ export const useSliderInput = (config: Ref<useSliderInputProps>) => {

/** 根据传入属性缓存计算inputNumber props */
const sliderInputState = computed(() => {
const initialState = {
inputDecimalPlaces: 0,
inputFormat: null as InputNumberProps['format'],
inputPlaceholder: '',
inputTheme: 'column' as InputNumberProps['theme'],
};
let initialState = { ...INPUT_NUMBER_PROPS_INITIAL_STATE };
const inputProps = config.value;
if (typeof inputProps.inputNumberProps !== 'boolean') {
const inputNumbeConfig = inputProps.inputNumberProps as TdSliderProps['inputNumberProps'];
const inputDecimalPlaces = inputNumbeConfig.decimalPlaces;
const inputFormat = inputNumbeConfig.format;
const inputPlaceholder = inputNumbeConfig.placeholder;
const inputTheme = inputNumbeConfig.theme;
if (typeof inputDecimalPlaces === 'number' && !Number.isNaN(inputDecimalPlaces)) {
initialState.inputDecimalPlaces = inputDecimalPlaces;
}
if (inputPlaceholder) {
initialState.inputPlaceholder = inputPlaceholder;
}
if (typeof inputFormat === 'function') {
initialState.inputFormat = inputFormat;
}
const inputTheme = inputProps.inputNumberProps?.theme;
initialState = { ...initialState, ...inputProps.inputNumberProps };
if (['column', 'row', 'normal'].includes(inputTheme)) {
initialState.inputTheme = inputTheme;
initialState.theme = inputTheme;
}
}
return initialState;
Expand All @@ -61,17 +51,14 @@ export const useSliderInput = (config: Ref<useSliderInputProps>) => {
const renderInputNumber = (val: number, changeFn: (val: number) => void) => {
return (
<InputNumber
{...sliderInputState.value}
class={sliderNumberClass.value}
value={val}
step={config.value.step}
onChange={changeFn}
disabled={config.value.disabled}
min={config.value.min}
max={config.value.max}
decimalPlaces={sliderInputState.value.inputDecimalPlaces}
format={sliderInputState.value.inputFormat}
placeholder={sliderInputState.value.inputPlaceholder}
theme={sliderInputState.value.inputTheme}
/>
);
};
Expand Down