Skip to content

Commit c8a85c3

Browse files
author
FalkWolsky
committed
Switch Column Type - WIP
1 parent 4ffc810 commit c8a85c3

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

client/packages/lowcoder/src/comps/comps/tableComp/column/columnTypeComp.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { withType } from "comps/generators";
55
import { trans } from "i18n";
66
import { Dropdown } from "lowcoder-design/src/components/Dropdown";
77
import { BooleanComp } from "./columnTypeComps/columnBooleanComp";
8+
import { SwitchComp } from "./columnTypeComps/columnSwitchComp";
89
import { DateComp } from "./columnTypeComps/columnDateComp";
910
import { ImageComp } from "./columnTypeComps/columnImgComp";
1011
import { LinkComp } from "./columnTypeComps/columnLinkComp";
@@ -77,6 +78,10 @@ const actionOptions = [
7778
label: trans("table.boolean"),
7879
value: "boolean",
7980
},
81+
{
82+
label: trans("table.switch"),
83+
value: "switch",
84+
},
8085
{
8186
label: trans("table.rating"),
8287
value: "rating",
@@ -101,6 +106,7 @@ export const ColumnTypeCompMap = {
101106
markdown: ColumnMarkdownComp,
102107
dateTime: DateTimeComp,
103108
boolean: BooleanComp,
109+
switch: SwitchComp,
104110
rating: RatingComp,
105111
progress: ProgressComp,
106112
date: DateComp,
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { BoolCodeControl } from "comps/controls/codeControl";
2+
import { trans } from "i18n";
3+
import { default as Checkbox } from "antd/es/checkbox";
4+
import { ColumnTypeCompBuilder, ColumnTypeViewFn } from "../columnTypeCompBuilder";
5+
import { ColumnValueTooltip } from "../simpleColumnTypeComps";
6+
import { SwitchStyle, SwitchStyleType, LabelStyle, InputFieldStyle } from "comps/controls/styleControlConstants";
7+
import styled, { css } from "styled-components";
8+
import { CheckboxStyle } from "comps/controls/styleControlConstants";
9+
import { useStyle } from "comps/controls/styleControl";
10+
import { default as Switch } from "antd/es/switch";
11+
import { styleControl } from "comps/controls/styleControl";
12+
import { RefControl } from "comps/controls/refControl";
13+
import { booleanExposingStateControl } from "comps/controls/codeStateControl";
14+
import { changeEvent, eventHandlerControl } from "comps/controls/eventHandlerControl";
15+
16+
interface SwitchWrapperProps {
17+
disabled: boolean;
18+
$style?: SwitchStyleType;
19+
}
20+
21+
const EventOptions = [
22+
changeEvent,
23+
{
24+
label: trans("switchComp.open"),
25+
value: "true",
26+
description: trans("switchComp.openDesc"),
27+
},
28+
{
29+
label: trans("switchComp.close"),
30+
value: "false",
31+
description: trans("switchComp.closeDesc"),
32+
},
33+
] as const;
34+
35+
const getStyle = (style: SwitchStyleType) => {
36+
return css`
37+
.ant-switch-handle::before {
38+
background-color: ${style.handle};
39+
}
40+
button {
41+
background-image: none;
42+
background-color: ${style.unchecked};
43+
&.ant-switch-checked {
44+
background-color: ${style.checked};
45+
}
46+
}
47+
`;
48+
};
49+
50+
const SwitchWrapper = styled.div<{ disabled: boolean }>`
51+
display: flex;
52+
align-items: center;
53+
// Can respond to drag & select events when disabled
54+
${(props) =>
55+
props.disabled &&
56+
`
57+
cursor: not-allowed;
58+
>button:disabled {
59+
pointer-events: none;
60+
}
61+
`};
62+
`;
63+
64+
const childrenMap = {
65+
value: booleanExposingStateControl("value"),
66+
switchState: BoolCodeControl,
67+
onEvent: eventHandlerControl(EventOptions),
68+
disabled: BoolCodeControl,
69+
style: styleControl(InputFieldStyle),
70+
labelStyle: styleControl(LabelStyle.filter((style) => ['accent', 'validate'].includes(style.name) === false)),
71+
viewRef: RefControl<HTMLElement>,
72+
};
73+
74+
const getBaseValue: ColumnTypeViewFn<typeof childrenMap, boolean, boolean> = (props) => props.switchState;
75+
76+
export const SwitchComp = (function () {
77+
return new ColumnTypeCompBuilder(
78+
childrenMap,
79+
(props, dispatch) => {
80+
const value = props.changeValue ?? getBaseValue(props, dispatch);
81+
const CheckBoxComp = () => {
82+
return (
83+
<SwitchWrapper disabled={props.disabled}>
84+
<Switch
85+
checked={props.value.value || true}
86+
disabled={props.disabled}
87+
ref={props.viewRef}
88+
onChange={(checked) => {
89+
props.value.onChange(checked);
90+
props.onEvent("change");
91+
props.onEvent(checked ? "true" : "false");
92+
}}
93+
/>
94+
</SwitchWrapper>
95+
);
96+
};
97+
return <CheckBoxComp />;
98+
},
99+
(nodeValue) => nodeValue.switchState.value,
100+
getBaseValue
101+
)
102+
.setEditViewFn((props) => {
103+
return (
104+
<Switch
105+
checked={props.value}
106+
disabled={false}
107+
onChange={(checked) => {
108+
props.onChange(checked);
109+
// props.onEvent("change");
110+
// props.onEvent(checked ? "true" : "false");
111+
}}
112+
/>
113+
);
114+
})
115+
.setPropertyViewFn((children) => {
116+
return (
117+
<>
118+
{children.switchState.propertyView({
119+
label: trans("table.columnValue"),
120+
tooltip: ColumnValueTooltip,
121+
})}
122+
123+
</>
124+
);
125+
})
126+
.build();
127+
})();

client/packages/lowcoder/src/i18n/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,7 @@ export const en = {
18881888
"button": "Button",
18891889
"image": "Image",
18901890
"boolean": "Boolean",
1891+
"switch": "Switch",
18911892
"rating": "Rating",
18921893
"progress": "Progress",
18931894
"option": "Operation",

0 commit comments

Comments
 (0)