-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(switch): complete switch component
- Loading branch information
1 parent
cd5a84e
commit 2393b24
Showing
12 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
.bigbear-switch-label{ | ||
position: relative; | ||
cursor: pointer; | ||
input{ | ||
opacity: 0; | ||
} | ||
} | ||
|
||
.bigbear-switch-label-disabled{ | ||
opacity: 60%; | ||
cursor: not-allowed; | ||
} | ||
|
||
|
||
.bigbear-switch-label-size-default{ | ||
width: $switch-width-default; | ||
height: $switch-height-default; | ||
input:checked~ .bigbear-switch-btn-size-default{ | ||
left:calc(100% - #{$switch-height-default}) | ||
} | ||
} | ||
.bigbear-switch-check-size-default{ | ||
border-radius: $switch-border-radius-default; | ||
} | ||
.bigbear-switch-btn-size-default{ | ||
width: $switch-height-default; | ||
height: $switch-height-default; | ||
} | ||
|
||
|
||
.bigbear-switch-label-size-lg{ | ||
width: $switch-width-lg; | ||
height: $switch-height-lg; | ||
input:checked~ .bigbear-switch-btn-size-lg{ | ||
left:calc(100% - #{$switch-height-lg}) | ||
} | ||
} | ||
.bigbear-switch-check-size-lg{ | ||
border-radius: $switch-border-radius-lg; | ||
} | ||
.bigbear-switch-btn-size-lg{ | ||
width: $switch-height-lg; | ||
height: $switch-height-lg; | ||
} | ||
|
||
|
||
|
||
|
||
@each $key,$val in $theme-colors{ | ||
.bigbear-switch-check-#{$key}{ | ||
color:#{nth($val,2)}; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
padding: 1px; | ||
@include switch-neu(nth($val, 1),nth($val,3),nth($val,4)); | ||
} | ||
} | ||
@each $key,$val in $theme-colors{ | ||
.bigbear-switch-btn-#{$key}{ | ||
@include neufactory-noactive(nth($val, 1),nth($val,3),nth($val,4)); | ||
display: inline-block; | ||
position: absolute; | ||
border-radius: 50%; | ||
left:0; | ||
transition:all .36s cubic-bezier(.78,.14,.15,.86); | ||
} | ||
|
||
} | ||
|
||
.bigbear-switch-label-size-sm{ | ||
width: $switch-width-sm; | ||
height: $switch-height-sm; | ||
input:checked~ .bigbear-switch-btn-size-sm{ | ||
left:calc(100% - #{$switch-height-sm}) | ||
} | ||
} | ||
.bigbear-switch-check-size-sm{ | ||
border-radius: $switch-border-radius-sm; | ||
padding: 0; | ||
} | ||
.bigbear-switch-btn-size-sm{ | ||
width: $switch-height-sm; | ||
height: $switch-height-sm; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Switch from "./switch"; | ||
|
||
export default Switch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Meta, Story, Props ,Preview } from '@storybook/addon-docs/blocks'; | ||
import Button from '../Button' | ||
import Switch from './switch' | ||
|
||
<Meta title='DISPLAY|Switch 开关' component={Switch} /> | ||
|
||
<br/> | ||
|
||
# Switch 开关 | ||
<br/> | ||
|
||
## 基本使用 | ||
|
||
底座和按钮都可以根据喜好换颜色! | ||
|
||
<Preview> | ||
<Story name='switch'> | ||
<div> | ||
<Switch></Switch> | ||
<Switch bottomType='primary' defaultState={true} ></Switch> | ||
<Switch bottomType='danger'></Switch> | ||
<Switch bottomType='warning' btnType='warning'></Switch> | ||
<Switch bottomType='success' btnType='success'></Switch> | ||
<Switch bottomType='info' btnType='info'></Switch> | ||
<Switch bottomType='dark' btnType='danger'></Switch> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
## 大小 switchSize | ||
|
||
有大中小3种 | ||
|
||
<Preview> | ||
<Story name='switchsizelg'> | ||
<div> | ||
<Switch switchSize='lg' ></Switch> | ||
<Switch switchSize='lg' bottomType='primary'></Switch> | ||
<Switch switchSize='lg' bottomType='danger' defaultState={true} ></Switch> | ||
<Switch switchSize='lg' bottomType='warning' btnType='warning'></Switch> | ||
<Switch switchSize='lg' bottomType='success' btnType='secondary'></Switch> | ||
<Switch switchSize='lg' bottomType='info' btnType='info'></Switch> | ||
<Switch switchSize='lg' bottomType='dark' btnType='danger'></Switch> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
<Preview> | ||
<Story name='switchsizesm'> | ||
<div> | ||
<Switch switchSize='sm' ></Switch> | ||
<Switch switchSize='sm' bottomType='primary'></Switch> | ||
<Switch switchSize='sm' bottomType='danger'></Switch> | ||
<Switch switchSize='sm' bottomType='warning' btnType='warning'></Switch> | ||
<Switch switchSize='sm' bottomType='success' btnType='success'></Switch> | ||
<Switch switchSize='sm' bottomType='info' btnType='info'></Switch> | ||
<Switch switchSize='sm' bottomType='dark' btnType='danger'></Switch> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
## 禁用 disabled | ||
|
||
<Preview> | ||
<Story name='switchdisable'> | ||
<div> | ||
<Switch disabled={true} ></Switch> | ||
<Switch disabled={true} bottomType='primary'></Switch> | ||
<Switch disabled={true} bottomType='danger'></Switch> | ||
<Switch disabled={true} bottomType='warning' btnType='warning'></Switch> | ||
<Switch disabled={true} bottomType='success' btnType='success'></Switch> | ||
<Switch disabled={true} bottomType='info' btnType='info'></Switch> | ||
<Switch disabled={true} bottomType='dark' btnType='danger'></Switch> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
## 属性详情 | ||
|
||
|
||
<Props of={Switch} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useState, PropsWithChildren } from 'react' | ||
import classNames from 'classnames' | ||
|
||
export interface SwitchProps{ | ||
/** 底座颜色*/ | ||
bottomType?:"primary" | "default" | "danger" | "secondary" | "success" | "info" | "light" | "warning" | "dark" ; | ||
/** 按钮颜色*/ | ||
btnType?:"primary" | "default" | "danger" | "secondary" | "success" | "info" | "light" | "warning" | "dark" ; | ||
/** 大小*/ | ||
switchSize?:"default"|"sm"| "lg" | ||
/** 禁用*/ | ||
disabled:boolean; | ||
/** 默认开关*/ | ||
defaultState:boolean; | ||
/** 选择后回调 */ | ||
callback?:(v:boolean)=>void | ||
/** 改变状态前回调,需要返回状态值 */ | ||
beforeChange?:(v:boolean)=>boolean | ||
} | ||
|
||
|
||
|
||
export function Switch (props:PropsWithChildren<SwitchProps>){ | ||
const {bottomType,btnType,switchSize,disabled,defaultState,callback,beforeChange}=props | ||
const [checked,setChecked]=useState(defaultState) | ||
const labelClassName=classNames('bigbear-switch-label',{ | ||
[`bigbear-switch-label-${bottomType}`]:bottomType, | ||
[`bigbear-switch-label-size-${switchSize}`]:switchSize, | ||
[`bigbear-switch-label-disabled`]:disabled | ||
}) | ||
const switchCheckName=classNames('bigbear-switch-check',{ | ||
[`bigbear-switch-check-${bottomType}`]:bottomType, | ||
[`bigbear-switch-check-size-${switchSize}`]:switchSize | ||
}) | ||
const btnClassName =classNames('bigbear-switch-btn',{ | ||
[`bigbear-switch-btn-${btnType}`]:btnType, | ||
[`bigbear-switch-btn-size-${switchSize}`]:switchSize | ||
}) | ||
return( | ||
<label className={labelClassName} > | ||
<input type='checkbox' checked={checked} disabled={disabled} onChange={()=>{ | ||
let value=!checked; | ||
if(beforeChange)value=beforeChange(value) | ||
setChecked(value) | ||
if(callback)callback(value) | ||
}} ></input> | ||
<span className={switchCheckName}> | ||
</span> | ||
<span className={btnClassName}></span> | ||
</label> | ||
) | ||
} | ||
|
||
Switch.defaultProps={ | ||
bottomType:'default', | ||
btnType:'default', | ||
switchSize:'default', | ||
disabled:false, | ||
defaultState:false | ||
}; | ||
|
||
|
||
|
||
export default Switch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters