Skip to content

Instantly share code, notes, and snippets.

View createdbymahmood's full-sized avatar

Mahmood Bagheri createdbymahmood

View GitHub Profile
@createdbymahmood
createdbymahmood / usage.tsx
Last active December 4, 2020 12:09
Sample for useImperativeHandle
const Register: FC = props => {
const formRef = useRef<ImperativeHandleProps>(null!);
return (
<Fragment>
<Form ref={formRef} />
<button onClick={() => formRef.current.onSomePropFired()}>
sbt form
</button>
</Fragment>
);
@createdbymahmood
createdbymahmood / Input-Textarea.tsx
Last active November 30, 2020 09:08
How to render input/textarea based on some conditions
function isPropsForTextareaElement(
props: InputProps | TextareaProps
): props is TextareaProps {
return "rows" in props;
}
export const TextareaInputGeneralComponent = (
props: InputProps | TextareaProps
) => {
if (isPropsForTextareaElement(props)) {
import React from "react";
// * modules
import classnames from "classnames";
import { Tooltip } from "antd";
// define all text types
type VariantProps = "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p";
type LineheightProps =
| "none"
import { useState, RefObject, useLayoutEffect } from "react";
import useWindowSize from "./useWindowSize";
export function useOverflow(
ref: RefObject<HTMLElement>
): {
refXOverflowing: boolean;
refYOverflowing: boolean;
refXScrollBegin: boolean;
@createdbymahmood
createdbymahmood / composableHoc.tsx
Last active December 16, 2020 14:12
composable hoc
export const composableHoc = (prop: string) => {
return function <T>(Component: ComponentType<T>) {
return function (props: T): JSX.Element {
return <Component {...props} />;
};
};
};
@createdbymahmood
createdbymahmood / accessKeys.ts
Created December 18, 2020 16:34
acl for pages in next js
export const accesKeys = {
guest: {
home: true,
users: true,
user: true,
},
};
import { PartialDeep } from "type-fest";
export type RouteConfigProps = PartialDeep<{
layout: {
include: boolean;
topbar: boolean;
};
accessKey: string;
}>;
type PathImpl<T, K extends keyof T> = K extends string
? T[K] extends Record<string, any>
? T[K] extends ArrayLike<any>
? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}`
: K | `${K}.${PathImpl<T[K], keyof T[K]>}`
: K
: never;
type Path<T> = PathImpl<T, keyof T> | keyof T;
@createdbymahmood
createdbymahmood / Search.ts
Last active January 3, 2021 20:52
Fuzzy Search in JS/TS World!
/* picked from cypress-realworld-app */
import Fuse from "fuse.js";
import { flow, map, curry } from "lodash/fp";
type IItems = {
title: string;
body: string;
};
export const performSearch = (items: object[], options: {}, query: string) =>
@createdbymahmood
createdbymahmood / pruneObject.ts
Created April 30, 2021 10:15
Remove all empty values from an object
import flat, { unflatten } from "flat";
import { reduceRight } from "lodash";
import {
pipe,
reject,
isNil,
isEqual,
get,
fromPairs,
entries,