diff --git a/docs/zh-CN/components/form/switch.md b/docs/zh-CN/components/form/switch.md index 1346b3049..cfc8b917e 100755 --- a/docs/zh-CN/components/form/switch.md +++ b/docs/zh-CN/components/form/switch.md @@ -103,6 +103,30 @@ order: 51 } ``` +## 不同尺寸 + +> 2.0.0 及以上版本 + +```schema: scope="body" +{ + "type": "form", + "api": "/api/mock2/form/saveForm", + "body": [ + { + "name": "switch", + "type": "switch", + "label": "" + }, + { + "name": "switch-sm", + "type": "switch", + "label": "", + "size": "sm" + } + ] +} +``` + ## 属性表 除了支持 [普通表单项属性表](./formitem#%E5%B1%9E%E6%80%A7%E8%A1%A8) 中的配置以外,还支持下面一些配置 @@ -114,6 +138,7 @@ order: 51 | offText | `string / IconSchema` | | 关闭时开关显示的内容 | | trueValue | `boolean / string / number` | `true` | 标识真值 | | falseValue | `boolean / string / number` | `false` | 标识假值 | +| size | `"sm" \| "md"` | `"md"` | 开关大小 | IconSchema 配置 | 属性名 | 类型 | 默认值 | 说明 | diff --git a/packages/amis-ui/scss/_properties.scss b/packages/amis-ui/scss/_properties.scss index bda018e8c..312c59377 100644 --- a/packages/amis-ui/scss/_properties.scss +++ b/packages/amis-ui/scss/_properties.scss @@ -1280,8 +1280,27 @@ --Switch-onDisabled-bgColor: #ccc; --Switch-onDisabled-circle-BackgroundColor: var(--white); --Switch-onDisabled-color: #fff; + --Switch-onHover-bgColor: var(--Switch-bgColor); --Switch-valueColor: var(--white); --Switch-width: #{px2rem(50px)}; + --Switch-slider-margin: #{px2rem(1px)}; + --Switch-slider-width: calc( + var(--Switch-height) - var(--Switch-slider-margin) * 2 + ); + --Switch-slider-transition: margin-left var(--animation-duration); + --Switch-text-marginRight: #{px2rem(8px)}; + --Switch-text-marginLeft: #{px2rem(25px)}; + --Switch-width--sm: #{px2rem(28px)}; + --Switch-height--sm: #{px2rem(16px)}; + --Switch-slider-width--sm: calc( + var(--Switch-height--sm) - var(--Switch-slider-margin) * 2 + ); + --Switch-text-marginRight--sm: calc( + var(--Switch-slider-margin) * 2 + var(--Switch-text-marginRight) / 2 + ); + --Switch-text-marginLeft--sm: calc( + var(--Switch-slider-margin) * 2 + var(--Switch-text-marginLeft) / 2 + ); --Table--unsaved-heading-bg: #e8f0fe; --Table--unsaved-heading-color: #4285f4; diff --git a/packages/amis-ui/scss/components/form/_switch.scss b/packages/amis-ui/scss/components/form/_switch.scss index cef1491bf..a2f3f84dc 100644 --- a/packages/amis-ui/scss/components/form/_switch.scss +++ b/packages/amis-ui/scss/components/form/_switch.scss @@ -18,12 +18,13 @@ } .text { - margin: 0 px2rem(7px) 0 px2rem(25px); + margin: 0 var(--Switch-text-marginRight) 0 var(--Switch-text-marginLeft); color: var(--Switch-valueColor); text-indent: var(--gap-xs); text-transform: uppercase; font-size: var(--fontSizeSm); line-height: var(--Switch-height); + vertical-align: super; font-style: normal; font-weight: bold; transition: all var(--animation-duration); @@ -40,27 +41,31 @@ content: ''; position: absolute; background: var(--white); - width: calc(var(--Switch-height) - #{px2rem(2px)}); - top: px2rem(1px); - bottom: px2rem(1px); - left: px2rem(1px); + width: var(--Switch-slider-width); + top: var(--Switch-slider-margin); + bottom: var(--Switch-slider-margin); + left: var(--Switch-slider-margin); border-radius: 50%; - transition: margin-left var(--animation-duration); + transition: var(--Switch-slider-transition); } } + &:hover { + background: var(--Switch-onHover-bgColor); + } + &.is-checked { background: var(--Switch-onActive-bgColor); .slider::before { left: auto; - right: px2rem(1px); + right: var(--Switch-slider-margin); border-width: 0; } .text { color: var(--white); - margin: 0 px2rem(25px) 0 px2rem(7px); + margin: 0 var(--Switch-text-marginLeft) 0 var(--Switch-text-marginRight); } } @@ -78,6 +83,36 @@ position: absolute; opacity: 0; } + + &--sm { + height: var(--Switch-height--sm); + min-width: var(--Switch-width--sm); + + .text { + line-height: var(--Switch-height--sm); + margin: 0 var(--Switch-text-marginRight--sm) 0 + var(--Switch-text-marginLeft--sm); + + > svg { + margin-top: calc((var(--Switch-height--sm) - var(--fontSizeSm)) / 2); + } + } + + .slider::before { + width: var(--Switch-slider-width--sm); + } + + &.is-checked { + .slider::before { + right: var(--Switch-slider-margin); + } + + .text { + margin: 0 var(--Switch-text-marginLeft--sm) 0 + var(--Switch-text-marginRight--sm); + } + } + } } .#{$ns}Switch-option { diff --git a/packages/amis-ui/scss/themes/_cxd-variables.scss b/packages/amis-ui/scss/themes/_cxd-variables.scss index cf3ade515..782a7e5e0 100644 --- a/packages/amis-ui/scss/themes/_cxd-variables.scss +++ b/packages/amis-ui/scss/themes/_cxd-variables.scss @@ -232,14 +232,19 @@ $L1: 0px 4px 6px 0px rgba(8, 14, 26, 0.06), --Switch-width: #{px2rem(44px)}; --Switch-valueColor: #{$G11}; --Switch-bgColor: #{$G7}; - --Switch-onActive-bgColor: #{$G6}; + --Switch-onActive-bgColor: var(--primary); --Switch-onHover-bgColor: #{$G6}; - --Switch-checked-bgColor: var(--primary); --Switch-checked-onHover-bgColor: #{$B7}; --Switch-checked-onActive-bgColor: #{$B7}; --Switch-onDisabled-bgColor: #{$B2}; --Switch-onDisabled-color: #{$G11}; // --Switch-onDisabled-circle-BackgroundColor: #fff; + --Switch-slider-margin: #{px2rem(2px)}; + --Switch-slider-width: calc( + var(--Switch-height) - var(--Switch-slider-margin) * 2 + ); + --Switch-slider-transition: all 0.5s ease; + --Switch-text-margin: 0 px2rem(8px) 0 px2rem(25px); --Calendar-icon-bottom: #{px2rem(-4px)}; --Calendar-icon-width: #{px2rem(10px)}; diff --git a/packages/amis-ui/scss/themes/cxd.scss b/packages/amis-ui/scss/themes/cxd.scss index 74673649a..de2e4756a 100644 --- a/packages/amis-ui/scss/themes/cxd.scss +++ b/packages/amis-ui/scss/themes/cxd.scss @@ -1,54 +1,36 @@ // 百度云舍主题:http://console.yunshe.design/docs/index @import './cxd-variables'; - @import './common'; .#{$ns}Switch { - .text { - margin: 0 px2rem(8px) 0 px2rem(25px); - vertical-align: super; - } - .slider { - &:before { - width: calc(var(--Switch-height) - #{px2rem(4px)}); - top: px2rem(2px); - bottom: px2rem(2px); - left: px2rem(2px); - transition: all 0.5s ease; - } - } &:active { .slider::before { - width: calc(var(--Switch-height) + #{px2rem(2px)}); + width: calc(var(--Switch-slider-width) + var(--Switch-slider-margin) * 2); border-radius: 10px; } } - &:hover { - background: var(--Switch-onHover-bgColor); - } + &.is-checked { - background: var(--Switch-checked-bgColor); - - .slider::before { - right: auto; - left: calc( - 100% - (var(--Switch-height) - #{px2rem(4px)}) - #{px2rem(2px)} - ); - } - - .text { - margin: 0 px2rem(25px) 0 px2rem(8px); - } &:hover { background: var(--Switch-checked-onHover-bgColor); } &:active { background: var(--Switch-checked-onActive-bgColor); + .slider::before { - left: calc( - 100% - (var(--Switch-height) + #{px2rem(2px)}) - #{px2rem(2px)} + right: calc(var(--Switch-slider-margin) * 2); + } + } + } + + &--sm { + &:active { + .slider::before { + width: calc( + var(--Switch-slider-width--sm) + var(--Switch-slider-margin) * 2 ); + border-radius: 10px; } } } diff --git a/packages/amis-ui/src/components/Switch.tsx b/packages/amis-ui/src/components/Switch.tsx index 2b7f21ec1..0719ccc9b 100644 --- a/packages/amis-ui/src/components/Switch.tsx +++ b/packages/amis-ui/src/components/Switch.tsx @@ -9,10 +9,11 @@ import {ClassNamesFn, themeable} from 'amis-core'; import {classPrefix, classnames} from '../themes/default'; const sizeMap = { - md: 'i-switch-md', - lg: 'i-switch-lg', - middle: 'i-switch-md', - large: 'i-switch-lg' + sm: 'Switch--sm', + md: 'Switch--md', + middle: 'Switch--md', + lg: 'Switch--lg', + large: 'Switch--lg' }; const levelMap = { @@ -23,7 +24,7 @@ const levelMap = { interface SwitchProps { id?: string; - size?: 'md' | 'lg' | 'middle' | 'large'; + size?: 'sm' | 'md' | 'lg' | 'middle' | 'large'; level?: 'info' | 'primary' | 'danger'; className?: string; classPrefix: string; diff --git a/packages/amis/__tests__/renderers/Form/__snapshots__/switch.test.tsx.snap b/packages/amis/__tests__/renderers/Form/__snapshots__/switch.test.tsx.snap index 88beb6744..fc2b48673 100644 --- a/packages/amis/__tests__/renderers/Form/__snapshots__/switch.test.tsx.snap +++ b/packages/amis/__tests__/renderers/Form/__snapshots__/switch.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Renderer:switch 1`] = ` +exports[`Renderer:Switch Switch basic props 1`] = `
- The form + 表单 @@ -110,6 +110,138 @@ exports[`Renderer:switch 1`] = `
+ +
+ +`; + +exports[`Renderer:Switch Switch size 1`] = ` +
+
+
+

+ + + 表单 + + +

+
+
+
+ +
+ +
+ + +
+
+
+
+
+ +
+