Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Flash.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import classnames from 'classnames';
import React from 'react';
import { Container } from './components/Container';
import { Value } from './components/Value';
import { ComponentsConfig } from './components/types';

import { formatters, Formatter } from './formatters/index';

Expand Down Expand Up @@ -45,6 +48,10 @@ export interface Props {
* Value to display. The only required prop.
*/
value: number;
/**
* Components configs to do custom rendering
*/
components?: ComponentsConfig;
}

/**
Expand Down Expand Up @@ -79,6 +86,10 @@ export const Flash = ({
upColor = '#00d865',
value,
stylePrefix = 'rvf_Flash',
components = {
Container,
Value,
},
}: Props) => {
const ref = React.useRef<number>(value);
const [flash, setFlash] = React.useState<FlashDirection | null>(null);
Expand Down Expand Up @@ -121,8 +132,8 @@ export const Flash = ({
}, [value, timeout]);

return (
<div className={cls} style={style}>
<span className={`${stylePrefix}__value`}>{valueFormatter(value)}</span>
</div>
<components.Container cls={cls} style={style}>
<components.Value value={value} valueFormatter={valueFormatter} stylePrefix={stylePrefix} />
</components.Container>
);
};
25 changes: 25 additions & 0 deletions src/components/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { ReactNode } from 'react';

export interface CommonProps {
/**
* classnames to be used with the Container component
*/
cls: string;

/**
* Custom styles to be used for the Container component
*/
style: React.CSSProperties;
}

export interface ContainerProps extends CommonProps {
children: ReactNode;
}

export const Container = ({ children, cls, style }: ContainerProps) => {
return (
<div className={cls} style={style}>
{children}
</div>
);
};
23 changes: 23 additions & 0 deletions src/components/Value.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { Formatter } from '../formatters/index';

export interface CommonProps {
/**
* Pass your own formatter function.
*/
valueFormatter: Formatter;
/**
* Prefix for the CSS selectors in the DOM.
*/
stylePrefix: string;
/**
* Value to display..
*/
value: number;
}

export interface ValueProps extends CommonProps {}

export const Value = ({ valueFormatter, stylePrefix = 'rvf_Flash', value }: ValueProps) => {
return <span className={`${stylePrefix}__value`}>{valueFormatter(value)}</span>;
};
8 changes: 8 additions & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ComponentType } from 'react';
import { ContainerProps } from './Container';
import { ValueProps } from './Value';

export interface ComponentsConfig {
Container: ComponentType<ContainerProps>;
Value: ComponentType<ValueProps>;
}
19 changes: 19 additions & 0 deletions stories/Flash.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Flash, Props } from '../src/Flash';
import { useInterval } from './useInterval';
import { ValueSetter } from './components/ValueSetter';
import pkg from '../package.json';
import { CustomContainer } from './components/CustomContainer';
import { CustomValue } from './components/CustomValue';

export default {
title: 'Flash',
Expand Down Expand Up @@ -225,3 +227,20 @@ export const MakeItNice = () => {
</div>
);
};

export const CustomComponent = () => {
return (
<ValueSetter initialValue={999}>
{(value: number) => (
<Flash
value={value}
formatter="currency"
components={{
Container: CustomContainer,
Value: CustomValue,
}}
/>
)}
</ValueSetter>
);
};
26 changes: 26 additions & 0 deletions stories/components/CustomContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { ContainerProps } from '../../src/components/Container';

export const CustomContainer = ({ children, cls, style }: ContainerProps) => {
const customStyles: React.CSSProperties = {
border: '1px solid rgba(0, 0, 0, .1)',
borderRadius: '3px',
display: 'inline-flex',
margin: 50,
width: 150,
padding: '20px 40px 25px',
alignItems: 'center',
justifyContent: 'center',
fontFamily: '-apple-system, BlinkMacSystemFont',
fontSize: 42,
fontWeight: 200,
boxShadow: '0 11px 17px -8px rgba(0, 0, 0, 0.3)',
transitionProperty: 'background-color, box-shadow, border-color !important',
};

return (
<div className={cls} style={{ ...style, ...customStyles }}>
{children}
</div>
);
};
10 changes: 10 additions & 0 deletions stories/components/CustomValue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { ValueProps } from '../../src/components/Value';

export const CustomValue = ({ stylePrefix, valueFormatter, value }: ValueProps) => {
return (
<span className={`${stylePrefix}__value`}>
{valueFormatter(value)}
</span>
);
};