mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-29 04:35:32 +08:00
refactor(select): use context first to pass ref
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React from 'react'
|
import React, { MutableRefObject } from 'react'
|
||||||
import { NormalSizes } from '../utils/prop-types'
|
import { NormalSizes } from '../utils/prop-types'
|
||||||
|
|
||||||
export interface SelectConfig {
|
export interface SelectConfig {
|
||||||
@@ -8,6 +8,7 @@ export interface SelectConfig {
|
|||||||
updateVisible?: Function
|
updateVisible?: Function
|
||||||
size?: NormalSizes
|
size?: NormalSizes
|
||||||
disableAll?: boolean
|
disableAll?: boolean
|
||||||
|
ref?: MutableRefObject<HTMLElement | null>
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultContext = {
|
const defaultContext = {
|
||||||
|
|||||||
31
components/select/select-dropdown.tsx
Normal file
31
components/select/select-dropdown.tsx
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import useTheme from '../styles/use-theme'
|
||||||
|
import { useSelectContext } from './select-context'
|
||||||
|
import Dropdown from '../shared/dropdown'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
visible: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const SelectDropdown: React.FC<React.PropsWithChildren<Props>> = ({
|
||||||
|
visible, children,
|
||||||
|
}) => {
|
||||||
|
const theme = useTheme()
|
||||||
|
const { ref } = useSelectContext()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dropdown parent={ref} visible={visible}>
|
||||||
|
<div className="select-dropdown">
|
||||||
|
{children}
|
||||||
|
<style jsx>{`
|
||||||
|
.select-dropdown {
|
||||||
|
border-radius: ${theme.layout.radius};
|
||||||
|
box-shadow: ${theme.expressiveness.shadowLarge};
|
||||||
|
}
|
||||||
|
`}</style>
|
||||||
|
</div>
|
||||||
|
</Dropdown>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SelectDropdown
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
import React, { MutableRefObject, useMemo, useRef, useState } from 'react'
|
import React, { useMemo, useRef, useState } from 'react'
|
||||||
import useTheme from '../styles/use-theme'
|
|
||||||
import SelectOption from './select-option'
|
|
||||||
import SelectIcon from './select-icon'
|
|
||||||
import Dropdown from '../shared/dropdown'
|
|
||||||
import useClickAway from '../utils/use-click-away'
|
|
||||||
import { ZeitUIThemes } from '../styles/themes'
|
|
||||||
import { SelectContext, SelectConfig } from './select-context'
|
|
||||||
import { NormalSizes } from '../utils/prop-types'
|
import { NormalSizes } from '../utils/prop-types'
|
||||||
import { getSizes } from './styles'
|
import useClickAway from '../utils/use-click-away'
|
||||||
import { pickChildByProps, pickChildrenFirst } from '../utils/collections'
|
import { pickChildByProps, pickChildrenFirst } from '../utils/collections'
|
||||||
|
import SelectIcon from './select-icon'
|
||||||
|
import SelectOption from './select-option'
|
||||||
|
import useTheme from '../styles/use-theme'
|
||||||
|
import SelectDropdown from './select-dropdown'
|
||||||
|
import { SelectContext, SelectConfig } from './select-context'
|
||||||
|
import { getSizes } from './styles'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
@@ -31,25 +30,6 @@ const defaultProps = {
|
|||||||
|
|
||||||
export type SelectProps = Props & typeof defaultProps & React.HTMLAttributes<any>
|
export type SelectProps = Props & typeof defaultProps & React.HTMLAttributes<any>
|
||||||
|
|
||||||
const getDropdown = (
|
|
||||||
ref: MutableRefObject<HTMLDivElement | null>,
|
|
||||||
children: React.ReactNode | null,
|
|
||||||
theme: ZeitUIThemes,
|
|
||||||
visible: boolean,
|
|
||||||
) => (
|
|
||||||
<Dropdown parent={ref} visible={visible}>
|
|
||||||
<div className="select-dropdown">
|
|
||||||
{children}
|
|
||||||
<style jsx>{`
|
|
||||||
.select-dropdown {
|
|
||||||
border-radius: ${theme.layout.radius};
|
|
||||||
box-shadow: ${theme.expressiveness.shadowLarge};
|
|
||||||
}
|
|
||||||
`}</style>
|
|
||||||
</div>
|
|
||||||
</Dropdown>
|
|
||||||
)
|
|
||||||
|
|
||||||
const Select: React.FC<React.PropsWithChildren<SelectProps>> = ({
|
const Select: React.FC<React.PropsWithChildren<SelectProps>> = ({
|
||||||
children, size, disabled, initialValue: init, placeholder,
|
children, size, disabled, initialValue: init, placeholder,
|
||||||
icon: Icon, onChange, className, pure, ...props
|
icon: Icon, onChange, className, pure, ...props
|
||||||
@@ -68,10 +48,10 @@ const Select: React.FC<React.PropsWithChildren<SelectProps>> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const initialValue: SelectConfig = useMemo(() => ({
|
const initialValue: SelectConfig = useMemo(() => ({
|
||||||
value, visible, updateValue, updateVisible,
|
value, visible, updateValue, updateVisible, size, ref,
|
||||||
size, disableAll: disabled,
|
disableAll: disabled,
|
||||||
}), [visible, size, disabled])
|
}), [visible, size, disabled, ref])
|
||||||
|
|
||||||
const clickHandler = (event: React.MouseEvent<HTMLDivElement>) => {
|
const clickHandler = (event: React.MouseEvent<HTMLDivElement>) => {
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
event.nativeEvent.stopImmediatePropagation()
|
event.nativeEvent.stopImmediatePropagation()
|
||||||
@@ -94,7 +74,7 @@ const Select: React.FC<React.PropsWithChildren<SelectProps>> = ({
|
|||||||
<div className={`select ${className}`} ref={ref} onClick={clickHandler} {...props}>
|
<div className={`select ${className}`} ref={ref} onClick={clickHandler} {...props}>
|
||||||
{!value && <span className="value placeholder">{placeholder}</span>}
|
{!value && <span className="value placeholder">{placeholder}</span>}
|
||||||
{value && <span className="value">{selectedChild}</span>}
|
{value && <span className="value">{selectedChild}</span>}
|
||||||
{getDropdown(ref, children, theme, visible)}
|
<SelectDropdown visible={visible}>{children}</SelectDropdown>
|
||||||
{!pure && <div className="icon"><Icon /></div>}
|
{!pure && <div className="icon"><Icon /></div>}
|
||||||
<style jsx>{`
|
<style jsx>{`
|
||||||
.select {
|
.select {
|
||||||
|
|||||||
Reference in New Issue
Block a user