|
| 1 | +import { default as AntdCheckboxGroup } from "antd/es/checkbox/Group"; |
| 2 | +import { SelectInputOptionControl } from "comps/controls/optionsControl"; |
| 3 | +import { BoolCodeControl } from "../../controls/codeControl"; |
| 4 | +import { arrayStringExposingStateControl } from "../../controls/codeStateControl"; |
| 5 | +import { LabelControl } from "../../controls/labelControl"; |
| 6 | +import { ChangeEventHandlerControl } from "../../controls/eventHandlerControl"; |
| 7 | +import { UICompBuilder } from "../../generators"; |
| 8 | +import { CommonNameConfig, NameConfig, withExposingConfigs } from "../../generators/withExposing"; |
| 9 | +import styled, { css } from "styled-components"; |
| 10 | +import { |
| 11 | + selectDivRefMethods, |
| 12 | + TourInputInvalidConfig, |
| 13 | + SelectInputValidationChildren, |
| 14 | + useSelectInputValidate, |
| 15 | +} from "./tourInputConstants"; |
| 16 | +import { formDataChildren } from "../formComp/formDataConstants"; |
| 17 | +import { styleControl } from "comps/controls/styleControl"; |
| 18 | +import { CheckboxStyle, CheckboxStyleType } from "comps/controls/styleControlConstants"; |
| 19 | +import { RadioLayoutOptions, RadioPropertyView } from "./radioCompConstants"; |
| 20 | +import { dropdownControl } from "../../controls/dropdownControl"; |
| 21 | +import { ValueFromOption } from "lowcoder-design"; |
| 22 | +import { EllipsisTextCss } from "lowcoder-design"; |
| 23 | +import { trans } from "i18n"; |
| 24 | +import { RefControl } from "comps/controls/refControl"; |
| 25 | +import { migrateOldData } from "comps/generators/simpleGenerators"; |
| 26 | +import { fixOldInputCompData } from "../textInputComp/textInputConstants"; |
| 27 | + |
| 28 | +export const getStyle = (style: CheckboxStyleType) => { |
| 29 | + return css` |
| 30 | + &, |
| 31 | + .ant-checkbox-wrapper:not(.ant-checkbox-wrapper-disabled) { |
| 32 | + color: ${style.staticText}; |
| 33 | + max-width: calc(100% - 8px); |
| 34 | +
|
| 35 | + span:not(.ant-checkbox) { |
| 36 | + ${EllipsisTextCss}; |
| 37 | + } |
| 38 | +
|
| 39 | + .ant-checkbox-checked { |
| 40 | + .ant-checkbox-inner { |
| 41 | + background-color: ${style.checkedBackground}; |
| 42 | + border-color: ${style.checkedBackground}; |
| 43 | + border-width:${!!style.borderWidth ? style.borderWidth : '2px'}; |
| 44 | +
|
| 45 | + &::after { |
| 46 | + border-color: ${style.checked}; |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + &::after { |
| 51 | + border-color: ${style.checkedBackground}; |
| 52 | + border-width:${!!style.borderWidth ? style.borderWidth : '2px'}; |
| 53 | + border-radius: ${style.radius}; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + .ant-checkbox-inner { |
| 58 | + border-radius: ${style.radius}; |
| 59 | + background-color: ${style.uncheckedBackground}; |
| 60 | + border-color: ${style.uncheckedBorder}; |
| 61 | + border-width:${!!style.borderWidth ? style.borderWidth : '2px'}; |
| 62 | + } |
| 63 | + |
| 64 | + &:hover .ant-checkbox-inner, |
| 65 | + .ant-checkbox:hover .ant-checkbox-inner, |
| 66 | + .ant-checkbox-input + ant-checkbox-inner { |
| 67 | + background-color:${style.hoverBackground ? style.hoverBackground :'#fff'}; |
| 68 | + } |
| 69 | +
|
| 70 | + &:hover .ant-checkbox-checked .ant-checkbox-inner, |
| 71 | + .ant-checkbox:hover .ant-checkbox-inner, |
| 72 | + .ant-checkbox-input + ant-checkbox-inner { |
| 73 | + background-color:${style.hoverBackground ? style.hoverBackground:'#ffff'}; |
| 74 | + } |
| 75 | +
|
| 76 | + &:hover .ant-checkbox-inner, |
| 77 | + .ant-checkbox:hover .ant-checkbox-inner, |
| 78 | + .ant-checkbox-input:focus + .ant-checkbox-inner { |
| 79 | + border-color: ${style.checkedBackground}; |
| 80 | + border-width:${!!style.borderWidth ? style.borderWidth : '2px'}; |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + |
| 85 | +
|
| 86 | + .ant-checkbox-group-item { |
| 87 | + font-family:${style.fontFamily}; |
| 88 | + font-size:${style.textSize}; |
| 89 | + font-weight:${style.textWeight}; |
| 90 | + font-style:${style.fontStyle}; |
| 91 | + text-transform:${style.textTransform}; |
| 92 | + text-decoration:${style.textDecoration}; |
| 93 | + } |
| 94 | + .ant-checkbox-wrapper { |
| 95 | + padding: ${style.padding}; |
| 96 | + .ant-checkbox-inner, |
| 97 | + .ant-checkbox-checked::after { |
| 98 | + border-radius: ${style.radius}; |
| 99 | + } |
| 100 | + } |
| 101 | + `; |
| 102 | +}; |
| 103 | + |
| 104 | +const CheckboxGroup = styled(AntdCheckboxGroup) <{ |
| 105 | + $style: CheckboxStyleType; |
| 106 | + $layout: ValueFromOption<typeof RadioLayoutOptions>; |
| 107 | +}>` |
| 108 | + min-height: 32px; |
| 109 | + ${(props) => props.$style && getStyle(props.$style)} |
| 110 | + ${(props) => { |
| 111 | + if (props.$layout === "horizontal") { |
| 112 | + return css` |
| 113 | + display: flex; |
| 114 | + align-items: center; |
| 115 | + flex-wrap: wrap; |
| 116 | + `; |
| 117 | + } else if (props.$layout === "vertical") { |
| 118 | + return css` |
| 119 | + display: flex; |
| 120 | + flex-direction: column; |
| 121 | + `; |
| 122 | + } else if (props.$layout === "auto_columns") { |
| 123 | + return css` |
| 124 | + break-inside: avoid; |
| 125 | + columns: 160px; |
| 126 | + `; |
| 127 | + } |
| 128 | + }} |
| 129 | +`; |
| 130 | + |
| 131 | +let CheckboxBasicComp = (function () { |
| 132 | + const childrenMap = { |
| 133 | + defaultValue: arrayStringExposingStateControl("defaultValue"), |
| 134 | + value: arrayStringExposingStateControl("value"), |
| 135 | + label: LabelControl, |
| 136 | + disabled: BoolCodeControl, |
| 137 | + onEvent: ChangeEventHandlerControl, |
| 138 | + options: SelectInputOptionControl, |
| 139 | + style: styleControl(CheckboxStyle), |
| 140 | + layout: dropdownControl(RadioLayoutOptions, "horizontal"), |
| 141 | + viewRef: RefControl<HTMLDivElement>, |
| 142 | + |
| 143 | + ...SelectInputValidationChildren, |
| 144 | + ...formDataChildren, |
| 145 | + }; |
| 146 | + return new UICompBuilder(childrenMap, (props) => { |
| 147 | + const [ |
| 148 | + validateState, |
| 149 | + handleChange, |
| 150 | + ] = useSelectInputValidate(props); |
| 151 | + return props.label({ |
| 152 | + required: props.required, |
| 153 | + style: props.style, |
| 154 | + children: ( |
| 155 | + <CheckboxGroup |
| 156 | + ref={props.viewRef} |
| 157 | + disabled={props.disabled} |
| 158 | + value={props.value.value} |
| 159 | + $style={props.style} |
| 160 | + $layout={props.layout} |
| 161 | + options={props.options |
| 162 | + .filter((option) => option.value !== undefined && !option.hidden) |
| 163 | + .map((option) => ({ |
| 164 | + label: option.label, |
| 165 | + value: option.value, |
| 166 | + disabled: option.disabled, |
| 167 | + }))} |
| 168 | + onChange={(values) => { |
| 169 | + handleChange(values as string[]); |
| 170 | + }} |
| 171 | + /> |
| 172 | + ), |
| 173 | + ...validateState, |
| 174 | + }); |
| 175 | + }) |
| 176 | + .setPropertyViewFn((children) => <RadioPropertyView {...children} />) |
| 177 | + .setExposeMethodConfigs(selectDivRefMethods) |
| 178 | + .build(); |
| 179 | +})(); |
| 180 | + |
| 181 | +CheckboxBasicComp = migrateOldData(CheckboxBasicComp, fixOldInputCompData); |
| 182 | + |
| 183 | +export const CheckboxComp = withExposingConfigs(CheckboxBasicComp, [ |
| 184 | + new NameConfig("value", trans("selectInput.valueDesc")), |
| 185 | + TourInputInvalidConfig, |
| 186 | + ...CommonNameConfig, |
| 187 | +]); |
0 commit comments