Skip to content

Commit 24e507f

Browse files
authored
fix: The inputNumber should be reset correctly when its initial value is null (#377)
1 parent c3eedf2 commit 24e507f

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script lang="ts">
2+
import { KForm, KFormItem } from '@ikun-ui/form';
3+
import { KInputNumber } from '@ikun-ui/input-number';
4+
const initValue = {
5+
KInputNumber: null
6+
};
7+
let KFormInst: KForm | undefined = undefined;
8+
const handleReset = () => {
9+
KFormInst && KFormInst.resetForm();
10+
};
11+
12+
const rules = {
13+
KInputNumber: [
14+
{ required: true, msg: 'KInputNumber required' },
15+
{ min: 3, max: 5, msg: 'KInputNumber 3 ~5' }
16+
]
17+
};
18+
</script>
19+
20+
<KForm {initValue} {rules} bind:this={KFormInst}>
21+
<KFormItem field="KInputNumber" label="KInputNumber">
22+
<KInputNumber placeholder="KInputNumber" />
23+
</KFormItem>
24+
</KForm>
25+
<button id="reset" on:click={handleReset}>reset</button>

components/Form/__test__/form.spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import KFormItemLabelSlot from './fixture/labelSlot.svelte';
2020
import KFormValidateEvent from './fixture/validateEvent.svelte';
2121
import KFormValidateEventSefField from './fixture/validateEventSefField.svelte';
2222
import KFormValidateEventValidateField from './fixture/validateEventvalidateField.svelte';
23+
import KFormReset from './fixture/inputNumberReset.svelte';
2324
import { tick } from 'svelte';
2425
import { fireEvent, screen, render, waitFor } from '@testing-library/svelte';
2526
import { getValueByPath, parsePath, setValueByPath } from '../src/helpers/fields';
@@ -982,8 +983,6 @@ describe('Test: KForm', () => {
982983
});
983984
expect(instance).toBeTruthy();
984985
await tick();
985-
expect(instance).toBeTruthy();
986-
await tick();
987986
expect(host.innerHTML.includes('Error-Slot')).not.toBeTruthy();
988987

989988
const btn = host.querySelector('#validate');
@@ -993,6 +992,29 @@ describe('Test: KForm', () => {
993992
expect(host.innerHTML.includes('Error-Slot')).toBeTruthy();
994993
expect(host.innerHTML).matchSnapshot();
995994
});
995+
996+
test('The inputNumber component should be reset correctly when its initial value is null', async () => {
997+
// @ts-ignore
998+
const instance = new KFormReset({
999+
target: host
1000+
});
1001+
expect(instance).toBeTruthy();
1002+
await tick();
1003+
const input = host.querySelector('.k-input--inner');
1004+
expect(input.value).toBe('');
1005+
1006+
const upElm = host.querySelector('.k-input-number--up');
1007+
upElm?.click();
1008+
await tick();
1009+
await vi.advanceTimersByTimeAsync(300);
1010+
expect(input.value).toBe('1');
1011+
1012+
const btn = host.querySelector('#reset');
1013+
btn?.click();
1014+
await tick();
1015+
await vi.advanceTimersByTimeAsync(300);
1016+
expect(input.value).toBe('');
1017+
});
9961018
});
9971019

9981020
describe('Test: KForm helper fields', () => {

components/InputNumber/src/index.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
* @internal
4949
*/
5050
export let center: KInputNumberProps['center'] = false;
51+
let resolveValue: Decimal | null = value ? new Deci(value) : null;
52+
let inputRef: null | HTMLInputElement | HTMLTextAreaElement = null;
5153
/*********************** KForm logic start ************************/
5254
let disabledFrom = false;
5355
$: disabledInner = disabledFrom || disabled;
@@ -62,10 +64,11 @@
6264
// on the form value in the KFormItem context
6365
function formUpdateField(init = false) {
6466
field = formContext.split('&').pop();
65-
value = formInstance.getValueByPath(
67+
resolveValue = value = formInstance.getValueByPath(
6668
field,
6769
init ? formInstance.__default_value : formInstance.__value
6870
);
71+
setValueToInput(normalizeVarStrEmpty(`${toPrecision(resolveValue)}`));
6972
}
7073
function formPropsChangeCb(props: Record<any, any>) {
7174
disabledFrom = props.disabled;
@@ -95,7 +98,6 @@
9598
/*********************** KForm logic end ************************/
9699
97100
const dispatch = createEventDispatcher();
98-
let resolveValue: Decimal | null = value ? new Deci(value) : null;
99101
$: {
100102
if (!isInput) {
101103
resolveValue = fixStepStrictlyVal(value);
@@ -203,7 +205,6 @@
203205
}
204206
};
205207
206-
let inputRef: null | HTMLInputElement | HTMLTextAreaElement = null;
207208
const handlePrependClick = () => {
208209
if (disabledInner) return;
209210
inputRef && dispatch('triggerPrepend', new Deci(inputRef.value).toNumber());
@@ -248,14 +249,14 @@
248249
return value;
249250
};
250251
251-
$: toPrecision = (value: Decimal | null) => {
252+
function toPrecision(value: Decimal | null) {
252253
if (value && precision) {
253254
return value.toFixed(precision);
254255
}
255256
256257
if (value && !precision) return value.toString();
257258
return null;
258-
};
259+
}
259260
260261
// class names
261262
const prefixCls = getPrefixCls('input');

0 commit comments

Comments
 (0)