feat(EditableTypography): support disable state
This commit is contained in:
@@ -13,7 +13,7 @@ function EditableTypography(props) {
|
|||||||
iconPosition = 'start',
|
iconPosition = 'start',
|
||||||
iconSize = 'large',
|
iconSize = 'large',
|
||||||
sx,
|
sx,
|
||||||
disabledEditing = false,
|
disabled = false,
|
||||||
prefixValue = '',
|
prefixValue = '',
|
||||||
...typographyProps
|
...typographyProps
|
||||||
} = props;
|
} = props;
|
||||||
@@ -21,10 +21,10 @@ function EditableTypography(props) {
|
|||||||
const [editing, setEditing] = React.useState(false);
|
const [editing, setEditing] = React.useState(false);
|
||||||
|
|
||||||
const handleClick = React.useCallback(() => {
|
const handleClick = React.useCallback(() => {
|
||||||
if (disabledEditing) return;
|
if (disabled) return;
|
||||||
|
|
||||||
setEditing((editing) => !editing);
|
setEditing((editing) => !editing);
|
||||||
}, [disabledEditing]);
|
}, [disabled]);
|
||||||
|
|
||||||
const handleTextFieldClick = React.useCallback((event) => {
|
const handleTextFieldClick = React.useCallback((event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
@@ -33,8 +33,9 @@ function EditableTypography(props) {
|
|||||||
const handleTextFieldKeyDown = React.useCallback(
|
const handleTextFieldKeyDown = React.useCallback(
|
||||||
async (event) => {
|
async (event) => {
|
||||||
const target = event.target;
|
const target = event.target;
|
||||||
|
const eventKey = event.key;
|
||||||
|
|
||||||
if (event.key === 'Enter') {
|
if (eventKey === 'Enter') {
|
||||||
if (target.value !== children) {
|
if (target.value !== children) {
|
||||||
await onConfirm(target.value);
|
await onConfirm(target.value);
|
||||||
}
|
}
|
||||||
@@ -42,11 +43,11 @@ function EditableTypography(props) {
|
|||||||
setEditing(false);
|
setEditing(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === 'Escape') {
|
if (eventKey === 'Escape') {
|
||||||
setEditing(false);
|
setEditing(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[children],
|
[children, onConfirm],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleTextFieldBlur = React.useCallback(
|
const handleTextFieldBlur = React.useCallback(
|
||||||
@@ -84,19 +85,14 @@ function EditableTypography(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box sx={sx} onClick={handleClick} editing={editing} disabled={disabled}>
|
||||||
sx={sx}
|
{!disabled && iconPosition === 'start' && editing === false && (
|
||||||
onClick={handleClick}
|
|
||||||
editing={editing}
|
|
||||||
disabledEditing={disabledEditing}
|
|
||||||
>
|
|
||||||
{iconPosition === 'start' && editing === false && (
|
|
||||||
<EditIcon fontSize={iconSize} sx={{ mr: 1 }} />
|
<EditIcon fontSize={iconSize} sx={{ mr: 1 }} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{component}
|
{component}
|
||||||
|
|
||||||
{iconPosition === 'end' && editing === false && (
|
{!disabled && iconPosition === 'end' && editing === false && (
|
||||||
<EditIcon fontSize={iconSize} sx={{ ml: 1 }} />
|
<EditIcon fontSize={iconSize} sx={{ ml: 1 }} />
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
@@ -105,7 +101,7 @@ function EditableTypography(props) {
|
|||||||
|
|
||||||
EditableTypography.propTypes = {
|
EditableTypography.propTypes = {
|
||||||
children: PropTypes.string.isRequired,
|
children: PropTypes.string.isRequired,
|
||||||
disabledEditing: PropTypes.bool,
|
disabled: PropTypes.bool,
|
||||||
iconPosition: PropTypes.oneOf(['start', 'end']),
|
iconPosition: PropTypes.oneOf(['start', 'end']),
|
||||||
iconSize: PropTypes.oneOf(['small', 'large']),
|
iconSize: PropTypes.oneOf(['small', 'large']),
|
||||||
onConfirm: PropTypes.func,
|
onConfirm: PropTypes.func,
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ import MuiBox from '@mui/material/Box';
|
|||||||
import MuiTextField from '@mui/material/TextField';
|
import MuiTextField from '@mui/material/TextField';
|
||||||
import { inputClasses } from '@mui/material/Input';
|
import { inputClasses } from '@mui/material/Input';
|
||||||
|
|
||||||
const boxShouldForwardProp = (prop) =>
|
const boxShouldForwardProp = (prop) => !['editing', 'disabled'].includes(prop);
|
||||||
!['editing', 'disabledEditing'].includes(prop);
|
|
||||||
|
|
||||||
export const Box = styled(MuiBox, {
|
export const Box = styled(MuiBox, {
|
||||||
shouldForwardProp: boxShouldForwardProp,
|
shouldForwardProp: boxShouldForwardProp,
|
||||||
@@ -15,7 +14,7 @@ export const Box = styled(MuiBox, {
|
|||||||
max-width: 90%;
|
max-width: 90%;
|
||||||
height: 33px;
|
height: 33px;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
${({ disabledEditing }) => !disabledEditing && 'cursor: pointer;'}
|
${({ disabled }) => !disabled && 'cursor: pointer;'}
|
||||||
${({ editing }) => editing && 'border-bottom: 1px dashed #000;'}
|
${({ editing }) => editing && 'border-bottom: 1px dashed #000;'}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user