Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema: System fields and withCursorPosition HOC #1822

Merged
merged 7 commits into from
Feb 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { cloneDeep } from "lodash";
import { FormValue } from "./views/FieldForm";
import { FieldSettingsOptions } from "../../../../../../shell/services/types";
import { convertLabelValue } from "../../utils";
import { withCursorPosition } from "../../../../../../shell/components/withCursorPosition";

const TextFieldWithCursorPosition = withCursorPosition(TextField);

export type Validation = "length" | "required" | "unique";
export type FieldNames =
Expand Down Expand Up @@ -129,6 +132,9 @@ export const FieldFormInput = ({
});
};

const InputTextField =
fieldConfig.name === "name" ? TextFieldWithCursorPosition : TextField;

return (
<Grid item xs={fieldConfig.gridSize}>
{fieldConfig.type === "input" && (
Expand Down Expand Up @@ -166,7 +172,7 @@ export const FieldFormInput = ({
</Typography>
)}
</Box>
<TextField
<InputTextField
name={fieldConfig.name}
required={fieldConfig.required}
fullWidth={fieldConfig.fullWidth}
Expand Down Expand Up @@ -394,7 +400,7 @@ const KeyValueInput = ({
error={Boolean(labelErrorMsg)}
disabled={disabledFields.includes("value")}
/>
<TextField
<TextFieldWithCursorPosition
name="key"
required
fullWidth
Expand Down
210 changes: 119 additions & 91 deletions src/apps/schema/src/appRevamp/components/Field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ import {
useDeleteContentModelFieldMutation,
useUndeleteContentModelFieldMutation,
} from "../../../../../../shell/services/instance";
import { TYPE_TEXT } from "../configs";
import { TYPE_TEXT, SystemField } from "../configs";
import { notify } from "../../../../../../shell/store/notifications";

type Params = {
id: string;
};
interface Props {
field: ContentModelField;
field: ContentModelField | SystemField;
index: number;
onReorder: () => void;
setDraggedIndex: (index: number) => void;
setHoveredIndex: (index: number) => void;
onReorder?: () => void;
setDraggedIndex?: (index: number) => void;
setHoveredIndex?: (index: number) => void;
disableDrag: boolean;
isDeactivated?: boolean;
withDragIcon: boolean;
withMenu: boolean;
}

export const Field = ({
Expand All @@ -51,6 +53,8 @@ export const Field = ({
setHoveredIndex,
disableDrag,
isDeactivated,
withDragIcon,
withMenu,
}: Props) => {
const ref = useRef(null);
const [isDragging, setIsDragging] = useState(false);
Expand Down Expand Up @@ -145,11 +149,13 @@ export const Field = ({
e.stopPropagation();

try {
await navigator.clipboard.writeText(field?.ZUID);
const { ZUID } = field as ContentModelField;

await navigator.clipboard.writeText(ZUID);

setIsZuidCopied(true);
} catch (error) {
console.error("Failed to copy ZUID", error);
console.error("Failed to copy field ZUID", error);
}
};

Expand All @@ -161,7 +167,7 @@ export const Field = ({

setIsFieldNameCopied(true);
} catch (error) {
console.error("Failed to copy ZUID", error);
console.error("Failed to copy field name", error);
}
};

Expand All @@ -181,6 +187,14 @@ export const Field = ({
return { opacity: 1 };
};

const getGridTemplate = () => {
if (withDragIcon) {
return "28px 24px minmax(auto, min-content) 90px";
} else {
return "24px minmax(auto, min-content) 90px";
}
};

return (
<Box
minHeight="40px"
Expand All @@ -205,25 +219,27 @@ export const Field = ({
alignItems="center"
justifyContent="space-between"
pr={1}
pl={0.5}
pl={withDragIcon ? 0.5 : 1}
gap={1}
>
<Box
display="grid"
gridTemplateColumns="28px 24px minmax(auto, min-content) 90px"
gridTemplateColumns={getGridTemplate()}
alignItems="center"
>
<IconButton
className="drag-handle"
size="small"
disableRipple
disabled={disableDrag}
onMouseEnter={() => setIsDraggable(true)}
onMouseLeave={() => setIsDraggable(false)}
sx={{ cursor: "grab" }}
>
<DragIndicatorRoundedIcon />
</IconButton>
{withDragIcon && (
<IconButton
className="drag-handle"
size="small"
disableRipple
disabled={disableDrag}
onMouseEnter={() => setIsDraggable(true)}
onMouseLeave={() => setIsDraggable(false)}
sx={{ cursor: "grab" }}
>
<DragIndicatorRoundedIcon />
</IconButton>
)}
<FieldIcon type={field.datatype} />
<Tooltip title={field.label} enterDelay={3000}>
<Typography px={1.5} variant="body2" fontWeight="700" noWrap>
Expand Down Expand Up @@ -255,78 +271,90 @@ export const Field = ({
{isFieldNameCopied ? "Copied" : field.name}
</Typography>
</Button>
<IconButton onClick={handleMenuClick} size="small">
<MoreHorizRoundedIcon />
</IconButton>
<Menu
open={isMenuOpen}
onClose={() => setAnchorEl(null)}
anchorEl={anchorEl}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem
disabled={isDeactivated}
onClick={() => history.push(`${location.pathname}/${field.ZUID}`)}
>
<ListItemIcon>
<DriveFileRenameOutlineRoundedIcon />
</ListItemIcon>
<ListItemText>Edit Field</ListItemText>
</MenuItem>
<MenuItem onClick={handleCopyZuid}>
<ListItemIcon>
{isZuidCopied ? <CheckIcon /> : <WidgetsRoundedIcon />}
</ListItemIcon>
<ListItemText>{isZuidCopied ? "Copied" : "Copy ZUID"}</ListItemText>
</MenuItem>
{isDeletingField || isUndeletingField ? (
<MenuItem>
<ListItemIcon>
<CircularProgress size={24} color="inherit" />
</ListItemIcon>
{isDeletingField && (
<ListItemText>De-activating Field</ListItemText>
)}
{isUndeletingField && (
<ListItemText>Re-activating Field</ListItemText>
)}
</MenuItem>
) : (
<MenuItem
onClick={() => {
if (isDeactivated) {
undeleteContentModelField({
modelZUID,
fieldZUID: field?.ZUID,
});
} else {
deleteContentModelField({
modelZUID,
fieldZUID: field?.ZUID,
});
}
{withMenu && (
<>
<IconButton onClick={handleMenuClick} size="small">
<MoreHorizRoundedIcon />
</IconButton>
<Menu
open={isMenuOpen}
onClose={() => setAnchorEl(null)}
anchorEl={anchorEl}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<ListItemIcon>
{isDeactivated ? (
<PlayCircleOutlineRoundedIcon />
) : (
<HighlightOffRoundedIcon />
)}
</ListItemIcon>
<ListItemText>
{isDeactivated ? "Re-activate Field" : "De-activate Field"}
</ListItemText>
</MenuItem>
)}
</Menu>
<MenuItem
disabled={isDeactivated}
onClick={() => {
const { ZUID } = field as ContentModelField;

history.push(`${location.pathname}/${ZUID}`);
}}
>
<ListItemIcon>
<DriveFileRenameOutlineRoundedIcon />
</ListItemIcon>
<ListItemText>Edit Field</ListItemText>
</MenuItem>
<MenuItem onClick={handleCopyZuid}>
<ListItemIcon>
{isZuidCopied ? <CheckIcon /> : <WidgetsRoundedIcon />}
</ListItemIcon>
<ListItemText>
{isZuidCopied ? "Copied" : "Copy ZUID"}
</ListItemText>
</MenuItem>
{isDeletingField || isUndeletingField ? (
<MenuItem>
<ListItemIcon>
<CircularProgress size={24} color="inherit" />
</ListItemIcon>
{isDeletingField && (
<ListItemText>De-activating Field</ListItemText>
)}
{isUndeletingField && (
<ListItemText>Re-activating Field</ListItemText>
)}
</MenuItem>
) : (
<MenuItem
onClick={() => {
const { ZUID } = field as ContentModelField;

if (isDeactivated) {
undeleteContentModelField({
modelZUID,
fieldZUID: ZUID,
});
} else {
deleteContentModelField({
modelZUID,
fieldZUID: ZUID,
});
}
}}
>
<ListItemIcon>
{isDeactivated ? (
<PlayCircleOutlineRoundedIcon />
) : (
<HighlightOffRoundedIcon />
)}
</ListItemIcon>
<ListItemText>
{isDeactivated ? "Re-activate Field" : "De-activate Field"}
</ListItemText>
</MenuItem>
)}
</Menu>
</>
)}
</Box>
</Box>
);
Expand Down
Loading