1. import React, { Fragment, useCallback } from 'react';
    2. import { adaptSymbol, getRealValue, getCentSignBySymbol, getColorByRealValue, getSymbolByRealValue, getRealTextWithPrecision } from './util';
    3. import './index.less';
    4. const Percent: React.SFC<PercentProps.Iprops> = (props) => {
    5. const { abs, value, format, shaped, color, prefix, suffix, precision } = props;
    6. const realValue = getRealValue(value);
    7. const realColor = useMemo(() => adaptSymbol(props.symbol), [props.symbol]);
    8. const renderSymbol = useCallback(() => {
    9. const symbolText = getSymbolByRealValue(realValue, shaped);
    10. if (shaped) {
    11. const kls = `percent_trend ${symbolText}`;
    12. return <span className={kls} style={{ color: realColor }} />;
    13. }
    14. if (symbol.calc && !prefix && !shaped) {
    15. return getSymbolByRealValue(realValue);
    16. }
    17. return null;
    18. }, [symbol, shaped, prefix, realValue]);
    19. const renderContent = useCallback(() => {
    20. const content =
    21. typeof format === 'function'
    22. ? format(realValue)
    23. : getRealTextWithPrecision(realValue, precision, abs);
    24. const cent = getCentSignBySymbol(symbol.cent);
    25. }, [format, realValue, precision, abs, symbol]);
    26. return (
    27. <span style={{props.style}} className={props.className}>
    28. {prefix && prefix}
    29. <span style={{ color: realColor }}>
    30. <Fragment>{renderSymbol()}</Fragment>
    31. {renderContent()}
    32. </span>
    33. <Fragment>{suffix && suffix}</Fragment>
    34. </span>
    35. );
    36. }
    37. Percent.defaultProps = {
    38. abs: false,
    39. color: true,
    40. symbol: true,
    41. shaped: false,
    42. }
    43. export default Percent;