Skip to content

Commit 2305e14

Browse files
committed
fix(core): fix restore form callback data
1 parent ab0fcbe commit 2305e14

File tree

11 files changed

+41
-20
lines changed

11 files changed

+41
-20
lines changed

packages/core/src/components/checkbox/checkbox.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,15 @@ export class Checkbox implements ComponentInterface {
162162
this.checked = this.initialState;
163163
}
164164

165-
formStateRestoreCallback(state: string): void {
166-
this.checked = state === 'true';
165+
formStateRestoreCallback(state: FormData | string): void {
166+
if (typeof state === 'string') {
167+
this.checked = state === 'true';
168+
return;
169+
}
170+
171+
const value = state.get(this.name);
172+
this.value = value?.toString() || 'on';
173+
this.checked = true;
167174
}
168175

169176
connectedCallback(): void {

packages/core/src/components/checkbox/tests/form/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
<h2>Checkbox - form</h2>
4242
<div>
4343
<form>
44-
<pop-checkbox name="custom" error-text="test error">input label</pop-checkbox>
45-
<pop-checkbox name="custom-value" checked error-text="test error">input label</pop-checkbox>
46-
<pop-checkbox name="custom-value" indeterminate error-text="test error">input label</pop-checkbox>
44+
<pop-checkbox name="default" error-text="test error">input label</pop-checkbox>
45+
<pop-checkbox name="checked" checked error-text="test error">input label</pop-checkbox>
46+
<pop-checkbox name="indeterminate" indeterminate="true" error-text="test error">input label</pop-checkbox>
4747
<input type="checkbox" name="native">
4848
<pop-button type="submit" color="primary">submit</pop-button>
4949
<pop-button type="reset" color="ghost">reset</pop-button>

packages/core/src/components/input-file/input-file.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ export class InputFile implements ComponentInterface, FormAssociatedInterface {
165165
this.nativeInput.value = '';
166166
}
167167

168-
formStateRestoreCallback(state: File): void {
169-
this.value = state;
168+
formStateRestoreCallback(state: FormData): void {
169+
this.value = (state.get(this.name) as File) || new File([], '');
170170
}
171171

172172
connectedCallback(): void {

packages/core/src/components/input/input.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ export class Input implements ComponentInterface, FormAssociatedInterface {
313313
this.value = this.initialValue;
314314
}
315315

316-
formStateRestoreCallback(state: string): void {
317-
this.value = state;
316+
formStateRestoreCallback(state: FormData): void {
317+
this.value = state.get(this.name)?.toString() || '';
318318
}
319319

320320
connectedCallback(): void {

packages/core/src/components/input/tests/form/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ <h2>Input - form</h2>
4343
<form>
4444
<pop-input type="text" name="custom" error-text="test error">input label</pop-input>
4545
<pop-input type="text" value="default" name="custom-value" error-text="test error">input label</pop-input>
46+
<pop-input type="text" name="custom-value" auto-complete="name">input label</pop-input>
4647
<input type="text" name="native">
4748
<pop-button type="submit" color="primary">submit</pop-button>
4849
<pop-button type="reset" color="ghost">reset</pop-button>

packages/core/src/components/radio-group/radio-group.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ export class RadioGroup implements ComponentInterface {
129129
this.value = this.initialValue;
130130
}
131131

132-
formStateRestoreCallback(state: string): void {
133-
this.value = state;
132+
formStateRestoreCallback(state: FormData): void {
133+
this.value = state.get(this.name) as any;
134134
}
135135

136136
connectedCallback(): void {

packages/core/src/components/range/range.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ export class Range implements ComponentInterface {
153153
this.value = this.initialValue;
154154
}
155155

156-
formStateRestoreCallback(state: string) {
157-
this.value = +state;
156+
formStateRestoreCallback(state: FormData): void {
157+
const value = Number(state.get(this.name));
158+
this.value = Number.isNaN(value) ? this.max / 2 : value;
158159
}
159160

160161
connectedCallback(): void {

packages/core/src/components/select/select.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ export class Select implements ComponentInterface, FormAssociatedInterface {
226226
this.value = this.initialValues;
227227
}
228228

229-
formStateRestoreCallback(state: any) {
230-
this.value = state;
229+
formStateRestoreCallback(state: FormData): void {
230+
const value = state.get(this.name);
231+
if (value?.toString().includes(',') && this.multiple) {
232+
this.value = value.toString().split(',');
233+
return;
234+
}
235+
this.value = value;
231236
}
232237

233238
connectedCallback(): void {

packages/core/src/components/textarea/textarea.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ export class Textarea implements ComponentInterface, FormAssociatedInterface {
271271
this.value = this.initialValue;
272272
}
273273

274-
formStateRestoreCallback(state: string): void {
275-
this.value = state;
274+
formStateRestoreCallback(state: FormData): void {
275+
this.value = state.get(this.name)?.toString() || '';
276276
}
277277

278278
connectedCallback(): void {

packages/core/src/components/toggle/toggle.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ export class Toggle implements ComponentInterface, FormAssociatedInterface {
155155
this.checked = this.initialState;
156156
}
157157

158-
formStateRestoreCallback(state: string): void {
159-
this.checked = state === 'true';
158+
formStateRestoreCallback(state: FormData | string): void {
159+
if (typeof state === 'string') {
160+
this.checked = state === 'true';
161+
return;
162+
}
163+
164+
const value = state.get(this.name);
165+
this.value = value?.toString() || 'on';
166+
this.checked = true;
160167
}
161168

162169
connectedCallback(): void {

packages/core/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"allowUnreachableCode": false,
55
"declaration": false,
66
"experimentalDecorators": true,
7-
"lib": ["dom", "es2023", "ESNext.Disposable"],
7+
"lib": ["dom", "DOM.Iterable", "es2023", "ESNext.Disposable"],
88
"moduleResolution": "node",
99
"module": "esnext",
1010
"target": "es2021",

0 commit comments

Comments
 (0)