-
-
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(select): complete select component
- Loading branch information
1 parent
2393b24
commit 3552bf6
Showing
16 changed files
with
348 additions
and
16 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
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,51 @@ | ||
.bigbear-select{ | ||
display: inline-block; | ||
vertical-align: bottom; | ||
margin: 10px; | ||
width: 120px; | ||
position: relative; | ||
cursor: pointer; | ||
.bigbear-select-display{ | ||
height: 30px; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding-left: 10px; | ||
padding-right: 10px; | ||
|
||
@include neufactory-noactive($white,$neu-whiteshadow1,$neu-whiteshadow2); | ||
.bigbear-select-displaytext{ | ||
padding-left: 2px; | ||
padding-right: 2px; | ||
flex-grow: 1; | ||
width: 100%; | ||
height: 80%; | ||
overflow: hidden; | ||
@include ringwrapper($white,$neu-whiteshadow1,$neu-whiteshadow2) | ||
} | ||
.bigbear-select-icon{ | ||
margin-left: 5px; | ||
} | ||
} | ||
.bigbear-select-options{ | ||
z-index: 200; | ||
position: absolute; | ||
left:10px; | ||
@include neufactory-noactive($white,$neu-whiteshadow1,$neu-whiteshadow2); | ||
>div{ | ||
padding-left:10px; | ||
padding-right: 10px; | ||
height: 30px; | ||
cursor: pointer; | ||
@include neufactory-hover($primary,$neu-blueshadow1,$neu-blueshadow2); | ||
&:hover{ | ||
color:$white; | ||
} | ||
} | ||
} | ||
&.disabled{ | ||
cursor: not-allowed; | ||
opacity: .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 Select from "./select"; | ||
|
||
export default Select; |
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,49 @@ | ||
import { Meta, Story, Props ,Preview } from '@storybook/addon-docs/blocks'; | ||
import Select from './select'; | ||
|
||
|
||
|
||
|
||
|
||
<Meta title='DISPLAY| Select 选择框' component={Select} /> | ||
|
||
<br/> | ||
|
||
# Select 选择框 | ||
|
||
<br/> | ||
|
||
## 基本使用 | ||
|
||
如果长文字显示不全且想显示全的,可以自行设定select的宽或者调整字体大小。 | ||
|
||
<Preview> | ||
<Story name='select'> | ||
<div style={{height:'200px'}}> | ||
<Select data={['options1','options2','options3','options4']} callback={(state)=>console.log(state)}></Select> | ||
<Select data={['options1','options2','options3','options4']} icon={null} defaultValue='defaultValue'></Select> | ||
<Select data={['options1longlonglonglong','选项2','选项3333','options4选项4']} icon={null} defaultValue='longlonglonglonglonglonglonglong'></Select> | ||
<Select data={['options1longlonglonglong','选项2','选项3333','options4选项4']} icon={null} defaultValue='longlonglonglonglonglonglonglong' disabled ></Select> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
## 使用模板 | ||
|
||
css会将option下的div选中设置样式,如果不想用默认样式,可以不用div做第一级子标签。 | ||
|
||
使用模板渲染,记得选中时使用setState设置值,之后使用setOpen关闭选框。 | ||
|
||
<Preview> | ||
<Story name='selectrenderTemplate'> | ||
<div style={{height:'200px'}}> | ||
<Select data={['options1','options2','options3','options4']} | ||
renderTemplate={(item,index,setState,setOpen)=><div | ||
onClick={()=>{setState(`${index}:${item}`);setOpen(false)}} key={index}>{index}:{item}</div>}></Select> | ||
</div> | ||
</Story> | ||
</Preview> | ||
|
||
|
||
|
||
<Props of={Select} ></Props> |
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,67 @@ | ||
import React, { useState, ReactNode, useRef, PropsWithChildren, useEffect } from 'react'; | ||
import useClickOutside from '../../hooks/useClickOutside'; | ||
import Icon from '../Icon'; | ||
import Transition from '../Transition/index'; | ||
|
||
|
||
interface SelectProps{ | ||
/** 选框中数据 */ | ||
data:Array<string> | ||
/** 使用模板渲染,setState是设置展示元素的方法 setOpen控制开关*/ | ||
renderTemplate?:(item:string,index:number,setState:React.Dispatch<React.SetStateAction<string>>,setOpen:React.Dispatch<React.SetStateAction<boolean>>)=>ReactNode; | ||
/** 展示右侧的图标 */ | ||
icon?:ReactNode; | ||
/** 选框中初始值 */ | ||
defaultValue?:string | ||
/** 下拉动画时间*/ | ||
timeout?:number, | ||
/**选择的回调值 */ | ||
callback?:(v:string)=>void; | ||
/** 禁用*/ | ||
disabled?:boolean; | ||
} | ||
|
||
|
||
|
||
function Select(props:PropsWithChildren<SelectProps>){ | ||
const {icon,defaultValue,timeout,renderTemplate,callback,data,disabled}=props; | ||
const [state,setState]=useState<string>(defaultValue!) | ||
const [open,setOpen]=useState(false) | ||
const ref= useRef(null) | ||
useClickOutside(ref,()=>setOpen(false)) | ||
useEffect(()=>{ | ||
if(callback)callback(state) | ||
},[callback, state]) | ||
return( | ||
<div className={`bigbear-select ${disabled?'disabled':''}`} ref={ref}> | ||
<div className='bigbear-select-display' onClick={()=>{if(!disabled)setOpen(!open)}} > | ||
<div className='bigbear-select-displaytext'>{state}</div> | ||
{icon?<div className='bigbear-select-icon'>{icon}</div>:null} | ||
</div> | ||
<Transition in={open} animation='zoom-in-top' timeout={timeout!} > | ||
<div className='bigbear-select-options' > | ||
{ | ||
data.map((item,index)=>{ | ||
let renderRes = renderTemplate?renderTemplate(item,index,setState,setOpen):<div onClick={()=>{setState(item);setOpen(false)}} key={index}> {item}</div> | ||
return renderRes | ||
}) | ||
} | ||
</div> | ||
</Transition> | ||
|
||
</div> | ||
) | ||
} | ||
|
||
|
||
Select.defaultProps={ | ||
icon:<Icon icon='angle-down'></Icon>, | ||
defaultValue:'', | ||
timeout:300, | ||
data:[], | ||
disabled:false | ||
} | ||
|
||
|
||
|
||
export default Select; |
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
Oops, something went wrong.