-
-
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(radio): complete radio component
- Loading branch information
1 parent
96edbf7
commit 3fff13d
Showing
10 changed files
with
164 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
.bigbear-radio-wrapper{ | ||
@include neufactory-noactive($white,$neu-whiteshadow1,$neu-whiteshadow2); | ||
display: inline-block; | ||
padding: 5px; | ||
.bigbear-radio-label { | ||
position: relative; | ||
margin:0; | ||
cursor: pointer; | ||
margin-left:5px; | ||
margin-right: 5px; | ||
.bigbear-radio-input{ | ||
position: absolute; | ||
opacity: 0; | ||
} | ||
.bigbear-radio-dot { | ||
position: relative; | ||
@include square(20px); | ||
@include ringwrapper($white,$neu-whiteshadow1,$neu-whiteshadow2); | ||
border-radius: 50%; | ||
display: inline-block; | ||
vertical-align: sub; | ||
|
||
} | ||
.bigbear-radio-dot.radio-active::after{ | ||
content: ''; | ||
position: absolute; | ||
@include square(10px); | ||
border-radius:50%; | ||
@include neufactory-noactive($primary,$neu-blueshadow1,$neu-blueshadow2); | ||
top:5px; | ||
left:5px; | ||
} | ||
.bigbear-radio-value{ | ||
padding:5px; | ||
text-shadow: 1px 1px 4px $neu-whiteshadow1, -1px -1px 4px $neu-whiteshadow2; | ||
} | ||
.radio-active ~ .bigbear-radio-value{ | ||
color:$primary; | ||
} | ||
&.radio-disabled{ | ||
cursor: not-allowed; | ||
opacity: 0.5; | ||
} | ||
} | ||
} |
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 Radio from "./radio"; | ||
|
||
export default Radio; |
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,47 @@ | ||
import { Meta, Story, Props ,Preview } from '@storybook/addon-docs/blocks'; | ||
import Radio from './radio'; | ||
import Icon from '../Icon' | ||
|
||
<Meta title='Display|Radio 单选按钮' component={Radio} /> | ||
|
||
<br/> | ||
|
||
# Radio 单选按钮 | ||
|
||
<br/> | ||
|
||
## 基本使用 | ||
|
||
不同于别的组件库封装,直接传个数组就可以使用,大大节约代码量。 | ||
|
||
|
||
<Preview> | ||
<Story name='Radio'> | ||
<Radio data={['radio1','radio2','radio3']} callback={(v)=>console.log(v)}></Radio> | ||
</Story> | ||
</Preview> | ||
|
||
## 默认选中 defaultIndex | ||
|
||
<Preview> | ||
<Story name='defaUltvalue'> | ||
<Radio data={['radio1','radio2','radio3']} defaultIndex={2}></Radio> | ||
</Story> | ||
</Preview> | ||
|
||
|
||
## 禁用 disableIndex | ||
|
||
禁用传入想禁的序号即可,不用一个个组件写disabled了。 | ||
|
||
<Preview> | ||
<Story name='disabled'> | ||
<Radio data={['radio1','radio2','radio3','radio4','radio5']} disableIndex={[2,3]}></Radio> | ||
</Story> | ||
</Preview> | ||
|
||
|
||
|
||
## 属性详情 | ||
|
||
<Props of={Radio} /> |
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,58 @@ | ||
import React, { useState, PropsWithChildren, useRef } from 'react'; | ||
import classnames from 'classnames' | ||
|
||
interface RadioProps{ | ||
/** 数据*/ | ||
data:Array<string> | ||
/** 默认选中索引*/ | ||
defaultIndex?:number; | ||
/** 回调值 */ | ||
callback?:(arr:Array<boolean>)=>void; | ||
/** 额外类名 */ | ||
className?:string; | ||
/** 禁用索引 */ | ||
disableIndex?:Array<number> | ||
} | ||
|
||
|
||
|
||
function Radio(props:PropsWithChildren<RadioProps>){ | ||
const {defaultIndex,callback,data,className,disableIndex}=props | ||
const disableRef=useRef(new Array(data.length).fill(false)) | ||
if(disableIndex){ | ||
disableIndex.forEach((v)=>disableRef.current[v]=true) | ||
} | ||
const classes = classnames('bigbear-radio-wrapper',className) | ||
const [state,setState]=useState(new Array(data.length).fill(false).map((v,i)=>i===defaultIndex?true:v)) | ||
return ( | ||
<div className={classes}> | ||
{ | ||
data.map((value,index)=>{ | ||
return( | ||
<label className={`bigbear-radio-label ${disableRef.current[index]?'radio-disabled':''}`} key={index}> | ||
<input type='radio' className='bigbear-radio-input' checked={state[index]} | ||
onClick={()=>{ | ||
if(!disableRef.current[index]){ | ||
let newState=new Array(data.length).fill(false) | ||
newState[index]=true; | ||
setState(newState) | ||
if(callback)callback(newState) | ||
} | ||
}} onChange={()=>{}} ></input> | ||
<span className={`bigbear-radio-dot ${state[index]?'radio-active':''}`}></span> | ||
<span className={'bigbear-radio-value'}>{value}</span> | ||
</label> | ||
) | ||
}) | ||
} | ||
|
||
</div> | ||
) | ||
} | ||
|
||
|
||
Radio.defaultProps={ | ||
data:[], | ||
} | ||
|
||
export default Radio; |
This file was deleted.
Oops, something went wrong.
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