import React from 'react'; import classNames from 'classnames'; import { strokeLineCaps, strokePresetColors } from './shared-props'; import { BaseProps } from '../_utils/props'; export type BarBackgroundType = 'impulse' | 'striped'; export interface BarProps extends BaseProps { percent?: number; /** Customise label style for both outer and inner label */ format?: (percent: number) => React.ReactNode; /** Determine display the label */ showInfo?: boolean; strokeLinecap?: strokeLineCaps; strokeColor?: string | string[]; strokeWidth?: number; /** Determine display the label in the bar or right of bar */ innerText?: boolean; backgroundType?: BarBackgroundType; children?: React.ReactNode; } const Bar = (props: BarProps): React.ReactElement => { const { prefixCls = 'ty-progress-bar', percent = 0, showInfo = true, strokeWidth = 8, strokeLinecap = 'round', strokeColor = 'primary', innerText = false, format, backgroundType, className, style, } = props; let percentage: number = percent > 100 ? 100 : percent; percentage = percentage < 0 ? 0 : percentage; const cls = classNames(prefixCls, className, `${prefixCls}_${strokeLinecap}`); const strokeBgCls = classNames(`${prefixCls}__bg`, { [`${prefixCls}__bg_${strokeColor}`]: typeof strokeColor === 'string' && strokePresetColors.includes(strokeColor), }); const background = typeof strokeColor === 'string' ? strokePresetColors.includes(strokeColor) ? '' : strokeColor : `linear-gradient(to right, ${strokeColor.join(',')})`; const effectCls = classNames(`${prefixCls}__effect`, { [`${prefixCls}__effect_${backgroundType}`]: backgroundType, }); const _renderLabel = () => { if (showInfo && !innerText) { return format ? ( format(percentage) ) : ( {percentage}% ); } return null; }; const _renderInnerLabel = () => { if (showInfo && innerText) { return format ? ( format(percentage) ) : (
{percentage}%
); } return null; }; return (
{_renderInnerLabel()}
{_renderLabel()}
); }; export default Bar;