parent
53ae8ecdbd
commit
92b722c118
6 changed files with 83 additions and 19 deletions
@ -0,0 +1,64 @@ |
||||
import { createElement, FC, ReactNode, useMemo, useState } from 'react'; |
||||
|
||||
import BodyText from './BodyText'; |
||||
import FlexBox from '../FlexBox'; |
||||
import IconButton from '../IconButton'; |
||||
import MonoText from './MonoText'; |
||||
|
||||
const SensitiveText: FC<SensitiveTextProps> = ({ |
||||
children, |
||||
monospaced: isMonospaced = false, |
||||
revealInitially: isRevealInitially = false, |
||||
textProps, |
||||
}) => { |
||||
const [isReveal, setIsReveal] = useState<boolean>(isRevealInitially); |
||||
|
||||
const textElementType = useMemo( |
||||
() => (isMonospaced ? MonoText : BodyText), |
||||
[isMonospaced], |
||||
); |
||||
const contentElement = useMemo(() => { |
||||
let content: ReactNode; |
||||
|
||||
if (isReveal) { |
||||
content = |
||||
typeof children === 'string' |
||||
? createElement( |
||||
textElementType, |
||||
{ |
||||
sx: { |
||||
lineHeight: 2.8, |
||||
maxWidth: '20em', |
||||
overflowY: 'scroll', |
||||
whiteSpace: 'nowrap', |
||||
}, |
||||
...textProps, |
||||
}, |
||||
children, |
||||
) |
||||
: children; |
||||
} else { |
||||
content = createElement(textElementType, textProps, '*****'); |
||||
} |
||||
|
||||
return content; |
||||
}, [children, isReveal, textElementType, textProps]); |
||||
|
||||
return ( |
||||
<FlexBox row spacing=".5em"> |
||||
{contentElement} |
||||
<IconButton |
||||
edge="end" |
||||
mapPreset="visibility" |
||||
onClick={() => { |
||||
setIsReveal((previous) => !previous); |
||||
}} |
||||
state={String(isReveal)} |
||||
sx={{ marginRight: '-.2em', padding: '.2em' }} |
||||
variant="normal" |
||||
/> |
||||
</FlexBox> |
||||
); |
||||
}; |
||||
|
||||
export default SensitiveText; |
@ -0,0 +1,7 @@ |
||||
type SensitiveTextOptionalProps = { |
||||
monospaced?: boolean; |
||||
revealInitially?: boolean; |
||||
textProps?: import('../components/Text').BodyTextProps; |
||||
}; |
||||
|
||||
type SensitiveTextProps = SensitiveTextOptionalProps; |
Loading…
Reference in new issue