mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-30 05:05:38 +08:00
feat(progress): add component
This commit is contained in:
@@ -34,3 +34,4 @@ export { default as Input } from './input'
|
||||
export { default as Radio } from './radio'
|
||||
export { default as Select } from './select'
|
||||
export { default as Tabs } from './tabs'
|
||||
export { default as Progress } from './progress'
|
||||
|
||||
3
components/progress/index.ts
Normal file
3
components/progress/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import Progress from './progress'
|
||||
|
||||
export default Progress
|
||||
112
components/progress/progress.tsx
Normal file
112
components/progress/progress.tsx
Normal file
@@ -0,0 +1,112 @@
|
||||
import React from 'react'
|
||||
import withDefaults from '../utils/with-defaults'
|
||||
import useTheme from '../styles/use-theme'
|
||||
import { useProportions } from '../utils/calculations'
|
||||
import { ZeitUIThemesPalette } from 'components/styles/themes'
|
||||
import { NormalTypes } from 'components/utils/prop-types'
|
||||
|
||||
export type ProgressColors = {
|
||||
[key: number]: string
|
||||
}
|
||||
|
||||
interface Props {
|
||||
value?: number
|
||||
max?: number
|
||||
fixedTop?: boolean
|
||||
fixedBottom?: boolean
|
||||
colors?: ProgressColors
|
||||
type?: NormalTypes
|
||||
className?: ''
|
||||
}
|
||||
|
||||
const defaultProps = {
|
||||
value: 0,
|
||||
max: 100,
|
||||
type: 'default' as NormalTypes,
|
||||
fixedTop: false,
|
||||
fixedBottom: false,
|
||||
className: '',
|
||||
}
|
||||
|
||||
export type ProgressProps = Props & typeof defaultProps & React.ProgressHTMLAttributes<any>
|
||||
|
||||
const getCurrentColor = (
|
||||
ratio: number,
|
||||
palette: ZeitUIThemesPalette,
|
||||
type: NormalTypes,
|
||||
colors: ProgressColors = {},
|
||||
): string => {
|
||||
const defaultColors: { [key in NormalTypes]: string } = {
|
||||
default: palette.foreground,
|
||||
success: palette.success,
|
||||
secondary: palette.secondary,
|
||||
warning: palette.warning,
|
||||
error: palette.error,
|
||||
}
|
||||
const colorKeys = Object.keys(colors)
|
||||
if (colorKeys.length === 0) return defaultColors[type]
|
||||
|
||||
const customColorKey = colorKeys.find(key => ratio <= +key)
|
||||
console.log(customColorKey)
|
||||
if (!customColorKey || Number.isNaN(+customColorKey)) return defaultColors[type]
|
||||
return colors[+customColorKey]
|
||||
}
|
||||
|
||||
const Progress: React.FC<ProgressProps> = ({
|
||||
value, max, className, type, colors, fixedTop, fixedBottom, ...props
|
||||
}) => {
|
||||
const theme = useTheme()
|
||||
const percentValue = useProportions(value, max)
|
||||
const currentColor = getCurrentColor(percentValue, theme.palette, type, colors)
|
||||
const fixed = fixedTop || fixedBottom
|
||||
|
||||
return (
|
||||
<div className={`progress ${className} ${fixed ? 'fixed' : ''}`}>
|
||||
<div className="inner" title={`${percentValue}%`} />
|
||||
<progress className={className} value={value} max={max} {...props} />
|
||||
<style jsx>{`
|
||||
progress {
|
||||
position: fixed;
|
||||
top: -1000px;
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.progress {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: .625rem;
|
||||
background-color: ${theme.palette.accents_2};
|
||||
border-radius: ${theme.layout.radius};
|
||||
}
|
||||
|
||||
.fixed {
|
||||
position: fixed;
|
||||
top: ${fixedTop ? 0 : 'unset'};
|
||||
bottom: ${fixedBottom ? 0 : 'unset'};
|
||||
left: 0;
|
||||
border-radius: 0;
|
||||
}
|
||||
|
||||
.fixed > .inner {
|
||||
border-radius: 0;
|
||||
}
|
||||
|
||||
.inner {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 100%;
|
||||
bottom: 0;
|
||||
transition: all 100ms ease-in;
|
||||
border-radius: ${theme.layout.radius};
|
||||
background-color: ${currentColor};
|
||||
width: ${percentValue}%;
|
||||
}
|
||||
`}</style>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default withDefaults(Progress, defaultProps)
|
||||
82
pages/docs/components/progress.mdx
Normal file
82
pages/docs/components/progress.mdx
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Layout, Playground, Attributes } from 'lib/components'
|
||||
import { Progress, Spacer, useTheme, Button } from 'components'
|
||||
import { useState } from 'react'
|
||||
|
||||
export const meta = {
|
||||
title: 'Progress',
|
||||
description: 'Progress',
|
||||
}
|
||||
|
||||
## Progress
|
||||
|
||||
Display progress relative to a limit or related to a task.
|
||||
|
||||
<Playground
|
||||
scope={{ Progress }}
|
||||
code={`
|
||||
<Progress value={50} />
|
||||
`} />
|
||||
|
||||
<Playground
|
||||
title="Custom Max"
|
||||
scope={{ Progress }}
|
||||
code={`
|
||||
<Progress value={45} max={50} />
|
||||
`} />
|
||||
|
||||
<Playground
|
||||
title="Dynamic Colors"
|
||||
scope={{ Progress, useState, useTheme, Button, Spacer }}
|
||||
code={`
|
||||
() => {
|
||||
const theme = useTheme()
|
||||
const [value, setValue] = useState(20)
|
||||
const colors = {
|
||||
20: theme.palette.error,
|
||||
40: theme.palette.warning,
|
||||
60: theme.palette.success,
|
||||
80: '#000',
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Progress value={value} colors={colors} />
|
||||
<Spacer />
|
||||
<Button onClick={() => setValue(value + 20)} auto size="small">Increase</Button>
|
||||
<Spacer x={.5} inline />
|
||||
<Button onClick={() => setValue(20)} auto size="small" type="abort">Reset</Button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
`} />
|
||||
|
||||
<Playground
|
||||
title="Type"
|
||||
scope={{ Progress, Spacer }}
|
||||
code={`
|
||||
<>
|
||||
<Progress type="secondary" value={10} />
|
||||
<Spacer />
|
||||
<Progress type="success" value={45} />
|
||||
<Spacer />
|
||||
<Progress type="warning" value={100} />
|
||||
<Spacer />
|
||||
<Progress type="error" value={21} />
|
||||
</>
|
||||
`} />
|
||||
|
||||
<Attributes edit="/pages/docs/components/progress.mdx">
|
||||
<Attributes.Title>Progress.Props</Attributes.Title>
|
||||
|
||||
| Attribute | Description | Type | Accepted values | Default
|
||||
| ---------- | ---------- | ---- | -------------- | ------ |
|
||||
| **value** | current value | `number` | - | 0 |
|
||||
| **max** | max value | `number` | - | 100 |
|
||||
| **fixedTop** | fix progress to top | `boolean` | - | `false` |
|
||||
| **fixedBottom** | fix progress to bottom | `boolean` | - | `false` |
|
||||
| **colors** | custom colors | `{ [key: number]: string }` | - | - |
|
||||
| **type** | predefined state types | `NormalTypes` | `'default', 'secondary', 'success', 'warning', 'error'` | `default` |
|
||||
| ... | native props | `ProgressHTMLAttributes` | `'aria-busy', 'className', ...` | - |
|
||||
|
||||
</Attributes>
|
||||
|
||||
export default ({ children }) => <Layout meta={meta}>{children}</Layout>
|
||||
Reference in New Issue
Block a user