diff --git a/packages/design/components/SensitiveText/index.stories.tsx b/packages/design/components/SensitiveText/index.stories.tsx new file mode 100644 index 00000000..7101bdf6 --- /dev/null +++ b/packages/design/components/SensitiveText/index.stories.tsx @@ -0,0 +1,22 @@ +import React from 'react'; +import { ComponentStory, ComponentMeta } from '@storybook/react'; +import { SensitiveText } from '.'; + +// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export +export default { + title: 'Tailchat/SensitiveText', + component: SensitiveText, + // More on argTypes: https://storybook.js.org/docs/react/api/argtypes + argTypes: {}, +} as ComponentMeta; + +// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args +const Template: ComponentStory = (args) => ( + +); + +export const Default = Template.bind({}); +// More on args: https://storybook.js.org/docs/react/writing-stories/args +Default.args = { + text: 'fooooo', +}; diff --git a/packages/design/components/SensitiveText/index.tsx b/packages/design/components/SensitiveText/index.tsx new file mode 100644 index 00000000..73d3bd49 --- /dev/null +++ b/packages/design/components/SensitiveText/index.tsx @@ -0,0 +1,36 @@ +import React, { useState } from 'react'; +import { Icon } from '../Icon'; + +interface SensitiveTextProps { + text: string; +} +export const SensitiveText: React.FC = React.memo( + (props) => { + const { text } = props; + const [show, setShow] = useState(false); + + return ( + + {show ? text : getMaskedText(text)} + + setShow((before) => !before)} + /> + + ); + } +); +SensitiveText.displayName = 'SensitiveText'; + +function getMaskedText(text: string) { + const len = text.length; + if (len > 2) { + return `${text[0]}****${text[len - 1]}`; + } else if (len === 2) { + return `${text[0]}*`; + } else { + return '**'; + } +} diff --git a/packages/design/components/index.ts b/packages/design/components/index.ts index 795ce292..c64ddc5a 100644 --- a/packages/design/components/index.ts +++ b/packages/design/components/index.ts @@ -6,6 +6,7 @@ export { DelayTip } from './DelayTip'; export { Highlight } from './Highlight'; export { Icon } from './Icon'; export { Image } from './Image'; +export { SensitiveText } from './SensitiveText'; export { WebMetaForm } from './WebMetaForm'; export {