Skip to content

Commit 67332c2

Browse files
separate defaultValue and value for switch comp
1 parent b83efb2 commit 67332c2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

client/packages/lowcoder/src/comps/comps/switchComp.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { trans } from "i18n";
1616
import { RefControl } from "comps/controls/refControl";
1717
import { refMethods } from "comps/generators/withMethodExposing";
1818
import { blurMethod, clickMethod, focusWithOptions } from "comps/utils/methodUtils";
19+
import { fixOldInputCompData } from "./textInputComp/textInputConstants";
1920

2021
import { useContext, useEffect } from "react";
2122
import { EditorContext } from "comps/editorState";
@@ -88,6 +89,7 @@ function fixOldData(oldData: any) {
8889
*/
8990
let SwitchTmpComp = (function () {
9091
const childrenMap = {
92+
defaultValue: booleanExposingStateControl("defaultValue"),
9193
value: booleanExposingStateControl("value"),
9294
label: LabelControl,
9395
onEvent: eventHandlerControl(EventOptions),
@@ -105,6 +107,13 @@ let SwitchTmpComp = (function () {
105107
...formDataChildren,
106108
};
107109
return new UICompBuilder(childrenMap, (props) => {
110+
const defaultValue = { ...props.defaultValue }.value;
111+
const value = { ...props.value }.value;
112+
113+
useEffect(() => {
114+
props.value.onChange(defaultValue);
115+
}, [defaultValue]);
116+
108117
return props.label({
109118
style: props.style,
110119
labelStyle: props.labelStyle,
@@ -113,7 +122,7 @@ let SwitchTmpComp = (function () {
113122
children: (
114123
<SwitchWrapper disabled={props.disabled} $style={props.inputFieldStyle}>
115124
<Switch
116-
checked={props.value.value}
125+
checked={value}
117126
disabled={props.disabled}
118127
ref={props.viewRef}
119128
onChange={(checked) => {
@@ -130,7 +139,7 @@ let SwitchTmpComp = (function () {
130139
return (
131140
<>
132141
<Section name={sectionNames.basic}>
133-
{children.value.propertyView({ label: trans("switchComp.defaultValue") })}
142+
{children.defaultValue.propertyView({ label: trans("switchComp.defaultValue") })}
134143
</Section>
135144

136145
<FormDataPropertyView {...children} />
@@ -170,6 +179,8 @@ let SwitchTmpComp = (function () {
170179
.build();
171180
})();
172181

182+
SwitchTmpComp = migrateOldData(SwitchTmpComp, fixOldInputCompData);
183+
173184
export const SwitchComp = withExposingConfigs(SwitchTmpComp, [
174185
new NameConfig("value", trans("switchComp.valueDesc")),
175186
...CommonNameConfig,

0 commit comments

Comments
 (0)