mirror of
https://github.com/zhigang1992/react.git
synced 2026-01-30 17:18:35 +08:00
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import React, { useMemo } from 'react'
|
|
import withDefaults from '../utils/with-defaults'
|
|
import useTheme from '../styles/use-theme'
|
|
import { NormalTypes } from '../utils/prop-types'
|
|
import { ZeitUIThemes } from '../styles/themes'
|
|
|
|
interface Props {
|
|
type?: NormalTypes
|
|
className?: string
|
|
}
|
|
|
|
const defaultProps = {
|
|
type: 'default' as NormalTypes,
|
|
className: '',
|
|
}
|
|
|
|
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
|
|
export type DotProps = Props & typeof defaultProps & NativeAttrs
|
|
|
|
const getColor = (type: NormalTypes, theme: ZeitUIThemes): string => {
|
|
const colors: { [key in NormalTypes]?: string } = {
|
|
default: theme.palette.accents_2,
|
|
success: theme.palette.success,
|
|
warning: theme.palette.warning,
|
|
error: theme.palette.error,
|
|
}
|
|
return colors[type] || (colors.default as string)
|
|
}
|
|
|
|
const Dot: React.FC<React.PropsWithChildren<DotProps>> = ({
|
|
type,
|
|
children,
|
|
className,
|
|
...props
|
|
}) => {
|
|
const theme = useTheme()
|
|
const color = useMemo(() => getColor(type, theme), [type, theme])
|
|
return (
|
|
<span className={`dot ${className}`} {...props}>
|
|
<span className="icon" />
|
|
<span className="label">{children}</span>
|
|
|
|
<style jsx>{`
|
|
.dot {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.icon {
|
|
width: 0.625rem;
|
|
height: 0.625rem;
|
|
border-radius: 50%;
|
|
background-color: ${color};
|
|
user-select: none;
|
|
}
|
|
|
|
.label {
|
|
margin-left: 8px;
|
|
font-size: 1rem;
|
|
text-transform: capitalize;
|
|
}
|
|
`}</style>
|
|
</span>
|
|
)
|
|
}
|
|
|
|
const MemoDot = React.memo(Dot)
|
|
|
|
export default withDefaults(MemoDot, defaultProps)
|