|
| 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 | +})(); |
0 commit comments