parent
d79599083f
commit
47d0d63a54
4 changed files with 27 additions and 102 deletions
@ -1,116 +1,50 @@ |
||||
import { Button, styled } from '@mui/material'; |
||||
import { |
||||
createElement, |
||||
FC, |
||||
ReactElement, |
||||
ReactNode, |
||||
useCallback, |
||||
useMemo, |
||||
useState, |
||||
} from 'react'; |
||||
import { Box, styled } from '@mui/material'; |
||||
import { createElement, FC, useMemo, useState } from 'react'; |
||||
|
||||
import { BORDER_RADIUS, EERIE_BLACK } from '../../lib/consts/DEFAULT_THEME'; |
||||
|
||||
import BodyText from './BodyText'; |
||||
import FlexBox from '../FlexBox'; |
||||
import IconButton from '../IconButton'; |
||||
import MonoText from './MonoText'; |
||||
|
||||
const InlineButton = styled(Button)({ |
||||
const BaseStyle = styled(Box)({ |
||||
backgroundColor: EERIE_BLACK, |
||||
borderRadius: BORDER_RADIUS, |
||||
minWidth: 'initial', |
||||
color: EERIE_BLACK, |
||||
display: 'inline-flex', |
||||
padding: '0 .6em', |
||||
textTransform: 'none', |
||||
width: 'fit-content', |
||||
|
||||
':hover': { |
||||
backgroundColor: `${EERIE_BLACK}F0`, |
||||
':focus': { |
||||
color: 'unset', |
||||
}, |
||||
}); |
||||
|
||||
const SensitiveText: FC<SensitiveTextProps> = ({ |
||||
children, |
||||
inline: isInline = false, |
||||
monospaced: isMonospaced = false, |
||||
revealButtonProps, |
||||
revealInitially: isRevealInitially = false, |
||||
textLineHeight = 2.8, |
||||
monospaced = false, |
||||
revealInitially = false, |
||||
textProps, |
||||
}) => { |
||||
const [isReveal, setIsReveal] = useState<boolean>(isRevealInitially); |
||||
const [reveal, setReveal] = useState<boolean>(revealInitially); |
||||
|
||||
const clickEventHandler = useCallback(() => { |
||||
setIsReveal((previous) => !previous); |
||||
}, []); |
||||
const content = useMemo(() => { |
||||
if (typeof children !== 'string') return children; |
||||
|
||||
const textSxLineHeight = useMemo<number | string | undefined>( |
||||
() => (isInline ? undefined : textLineHeight || undefined), |
||||
[isInline, textLineHeight], |
||||
); |
||||
|
||||
const textElementType = useMemo( |
||||
() => (isMonospaced ? MonoText : BodyText), |
||||
[isMonospaced], |
||||
); |
||||
const contentElement = useMemo(() => { |
||||
let content: ReactNode; |
||||
const elementType = monospaced ? MonoText : BodyText; |
||||
|
||||
if (isReveal) { |
||||
content = |
||||
typeof children === 'string' |
||||
? createElement( |
||||
textElementType, |
||||
{ |
||||
sx: { |
||||
lineHeight: textSxLineHeight, |
||||
maxWidth: '20em', |
||||
overflowY: 'scroll', |
||||
whiteSpace: 'nowrap', |
||||
}, |
||||
...textProps, |
||||
}, |
||||
children, |
||||
) |
||||
: children; |
||||
} else { |
||||
content = createElement( |
||||
textElementType, |
||||
{ |
||||
sx: { |
||||
lineHeight: textSxLineHeight, |
||||
}, |
||||
...textProps, |
||||
}, |
||||
'*****', |
||||
); |
||||
} |
||||
return createElement(elementType, textProps, children); |
||||
}, [children, monospaced, textProps]); |
||||
|
||||
return content; |
||||
}, [children, isReveal, textElementType, textProps, textSxLineHeight]); |
||||
const rootElement = useMemo<ReactElement>( |
||||
() => |
||||
isInline ? ( |
||||
<InlineButton onClick={clickEventHandler}> |
||||
{contentElement} |
||||
</InlineButton> |
||||
) : ( |
||||
<FlexBox row spacing=".5em"> |
||||
{contentElement} |
||||
<IconButton |
||||
edge="end" |
||||
mapPreset="visibility" |
||||
onClick={clickEventHandler} |
||||
state={String(isReveal)} |
||||
sx={{ marginRight: '-.2em', padding: '.2em' }} |
||||
variant="normal" |
||||
{...revealButtonProps} |
||||
/> |
||||
</FlexBox> |
||||
), |
||||
[clickEventHandler, contentElement, revealButtonProps, isInline, isReveal], |
||||
return ( |
||||
<BaseStyle |
||||
component="div" |
||||
onBlur={() => setReveal(false)} |
||||
onFocus={() => setReveal(true)} |
||||
tabIndex={0} |
||||
> |
||||
{reveal ? content : '*****'} |
||||
</BaseStyle> |
||||
); |
||||
|
||||
return rootElement; |
||||
}; |
||||
|
||||
export default SensitiveText; |
||||
|
Loading…
Reference in new issue