Skip to content

Commit

Permalink
fix: bearing validation not working, feat: added don't shrink if menu…
Browse files Browse the repository at this point in the history
… direction is top option (#54)

* fix: bearing validation not working

* fix: bearing validation
  • Loading branch information
matttdawson authored Nov 1, 2022
1 parent 48685c6 commit 7f20cd5
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 27 deletions.
8 changes: 4 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./src/react-menu3/index";
export * from "./src/react-menu3/types";

export { UpdatingContext } from "./src/contexts/UpdatingContext";
export { UpdatingContextProvider } from "./src/contexts/UpdatingContextProvider";
export { GridContext } from "./src/contexts/GridContext";
export { GridContextProvider } from "./src/contexts/GridContextProvider";
export * from "./src/contexts/UpdatingContext";
export * from "./src/contexts/UpdatingContextProvider";
export * from "./src/contexts/GridContext";
export * from "./src/contexts/GridContextProvider";

export type { GridBaseRow } from "./src/components/Grid";
export { Grid } from "./src/components/Grid";
Expand Down
1 change: 1 addition & 0 deletions src/components/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GridFormProps<RowType extends GridBaseRow> {
cellEditorParams: ICellEditorParams;
updateValue: (saveFn: (selectedRows: RowType[]) => Promise<boolean>) => Promise<boolean>;
saving: boolean;
data: RowType;
value: any;
field: string | undefined;
selectedRows: RowType[];
Expand Down
3 changes: 3 additions & 0 deletions src/components/GridPopoverHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export const useGridPopoverHook = <RowType extends GridBaseRow>(
saveButtonRef={saveButtonRef}
menuClassName={"lui-menu"}
onClose={(event: { reason: string }) => triggerSave(event.reason).then()}
reposition={"initial"}
viewScroll={"auto"}
dontShrinkIfDirectionIsTop={true}
>
{saving && (
<div
Expand Down
2 changes: 1 addition & 1 deletion src/components/gridForm/GridFormEditBearing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const GridFormEditBearing = <RowType extends GridBaseRow>(props: GridForm
maxLength: 16,
onKeyDown: async (e) => e.key === "Enter" && triggerSave().then(),
}}
formatted={bearingStringValidator(value) ? "?" : convertDDToDMS(bearingNumberParser(value))}
formatted={bearingStringValidator(value, formProps.range) ? "?" : convertDDToDMS(bearingNumberParser(value))}
error={bearingStringValidator(value, formProps.range)}
/>
</div>,
Expand Down
52 changes: 32 additions & 20 deletions src/components/gridPopoverEdit/GridPopoverEditBearing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,38 @@ export const GridPopoverEditBearingLike = <RowType extends GridBaseRow>(

export const GridPopoverEditBearing = <RowType extends GridBaseRow>(
colDef: GenericCellColDef<RowType, GridFormEditBearingProps<RowType>>,
) => ({
...GridPopoverEditBearingLike(colDef),
valueFormatter: bearingValueFormatter,
range: (value: number | null) => {
if (value === null) return "Bearing is required";
if (value >= 360) return "Bearing must be less than 360 degrees";
if (value < 0) return "Bearing must not be negative";
return null;
},
});
) => {
const init = GridPopoverEditBearingLike(colDef);
return {
...init,
valueFormatter: bearingValueFormatter,
cellEditorParams: {
range: (value: number | null) => {
if (value === null) return "Bearing is required";
if (value >= 360) return "Bearing must be less than 360 degrees";
if (value < 0) return "Bearing must not be negative";
return null;
},
...init.cellEditorParams,
},
};
};

export const GridPopoverEditBearingCorrection = <RowType extends GridBaseRow>(
colDef: GenericCellColDef<RowType, GridFormEditBearingProps<RowType>>,
) => ({
...GridPopoverEditBearingLike(colDef),
valueFormatter: bearingCorrectionValueFormatter,
range: (value: number | null) => {
if (value === null) return "Bearing is required";
if (value >= 360) return "Bearing must be less than 360 degrees";
if (value <= -180) return "Bearing must be greater then -180 degrees";
return null;
},
});
) => {
const init = GridPopoverEditBearingLike(colDef);
return {
...init,
valueFormatter: bearingCorrectionValueFormatter,
cellEditorParams: {
range: (value: number | null) => {
if (value === null) return "Bearing is required";
if (value >= 360) return "Bearing must be less than 360 degrees";
if (value <= -180) return "Bearing must be greater then -180 degrees";
return null;
},
...init.cellEditorParams,
},
};
};
19 changes: 19 additions & 0 deletions src/react-menu3/components/MenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const MenuList = ({
endTransition,
isDisabled,
menuItemFocus,
dontShrinkIfDirectionIsTop,
offsetX = 0,
offsetY = 0,
children,
Expand Down Expand Up @@ -417,6 +418,23 @@ export const MenuList = ({
className: arrowClassName,
});

const minHeight = useRef(0);
if (dontShrinkIfDirectionIsTop && menuRef.current?.getBoundingClientRect) {
const h = menuRef.current?.getBoundingClientRect().height;
if (minHeight.current < h) minHeight.current = h;
}

const dontShrinkOps = useMemo(
() =>
expandedDirection === "top" && dontShrinkIfDirectionIsTop
? {
overflowY: isSubmenuOpen ? "" : "auto",
minHeight: (isSubmenuOpen ? 0 : minHeight.current) + "px",
}
: undefined,
[dontShrinkIfDirectionIsTop, expandedDirection, isSubmenuOpen],
);

return (
<ul
role="menu"
Expand All @@ -428,6 +446,7 @@ export const MenuList = ({
style={{
...menuStyle,
...overflowStyle,
...dontShrinkOps,
margin: 0,
display: state === "closed" ? "none" : undefined,
position: "absolute",
Expand Down
4 changes: 4 additions & 0 deletions src/react-menu3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ export interface RootMenuProps extends BaseMenuProps, MenuStateOptions {
* Event fired when descendent menu items are clicked.
*/
onItemClick?: EventHandler<ClickEvent>;
/**
* NEW Don't shrink container if menu direction is "top"
*/
dontShrinkIfDirectionIsTop?: boolean;
}

export interface ExtraMenuProps {
Expand Down
2 changes: 1 addition & 1 deletion src/stories/components/GridPopoutBearing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const GridReadOnlyTemplate: ComponentStory<typeof Grid> = (props: GridProps) =>
headerName: "Bearing Correction",
cellEditorParams: {
multiEdit: true,
placeHolder: "Enter Bearing",
placeHolder: "Enter Bearing Correction",
onSave: async (selectedRows: ITestRow[], value: ITestRow["bearingCorrection"]) => {
await wait(1000);
selectedRows.forEach((row) => (row["bearingCorrection"] = value));
Expand Down
1 change: 0 additions & 1 deletion src/utils/bearing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const bearingStringValidator = (
}

const bearing = parseFloat(value);

return customValidate ? customValidate(bearing) : null;
};

Expand Down

0 comments on commit 7f20cd5

Please sign in to comment.