Skip to content

Instantly share code, notes, and snippets.

View phatnguyenuit's full-sized avatar
💪
Working on ReactJS and TypeScript.

Phát Nguyễn (Fast) phatnguyenuit

💪
Working on ReactJS and TypeScript.
View GitHub Profile
@phatnguyenuit
phatnguyenuit / EnhanceCountDown.tsx
Last active October 6, 2019 16:05
Enhance Count Down Component in ReactJS with Typescript
import * as React from 'react';
import { EnhanceCountDownPropsType, EnhanceCountDownStateType } from './types';
class EnhanceCountDown extends React.PureComponent<
EnhanceCountDownPropsType,
EnhanceCountDownStateType
> {
countDownInterval?: number;
constructor(props: EnhanceCountDownPropsType) {
@phatnguyenuit
phatnguyenuit / types.ts
Created October 6, 2019 03:25
Type-checking for Enhance Count Down Component
export interface EnhanceCountDownPropsType {
timeout: number;
decreaseTimeout?: number;
intervalTimeout?: number;
children(props: InjectedCountDownProps): JSX.Element;
}
export interface EnhanceCountDownStateType {
finished: boolean;
pending: boolean;
@phatnguyenuit
phatnguyenuit / trigger-onchange.js
Last active October 14, 2019 01:47
Trigger onChange manually ReactJS
class App extends React.Component {
constructor (props) {
super(props)
this.state = { name: '' }
this.onChange = this.onChange.bind(this)
}
onChange (e) {
this.setState({ name: e.target.value })
}
@phatnguyenuit
phatnguyenuit / .eslintrc.js
Last active December 5, 2019 08:49
Base configuration for ReactJS with Typescript. Follow this guide: https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
@phatnguyenuit
phatnguyenuit / index.js
Last active December 30, 2019 03:08
Get NPM dependencies recursion
const axios = require("axios");
function getDependencies(packageName) {
return new Promise(async (resolve, reject) => {
if (!packageName) {
reject(new Error("Missing package name."));
}
try {
const response = await axios.request({
method: "get",
@phatnguyenuit
phatnguyenuit / types.ts
Last active January 3, 2020 10:24
OTPInputProps
export interface OTPInputProps {
length: number; // Number of inputs
onChangeOTP: (otp: string) => any; // Handle onOTPChange to use its value
autoFocus?: boolean; // Auto focus to input programmatically
isNumberInput?: boolean; // If otp is number
disabled?: boolean;
style?: CSSProperties; // Style for container OTP
className?: string; // Class for container OTP
@phatnguyenuit
phatnguyenuit / OTPInput.tsx
Last active September 21, 2020 00:45
OTPInput definition summary
export function OTPInputComponent(props: OTPInputProps) {
const {
length,
isNumberInput,
autoFocus,
disabled,
onChangeOTP,
inputClassName,
inputStyle,
...rest
@phatnguyenuit
phatnguyenuit / cart.php
Last active January 4, 2020 02:41
Simple Ajax Cart Example
<?php
session_start();
if (!isset($_SESSION['shopping_cart'])) {
$_SESSION['shopping_cart'] = array();
}
$product_id = $_POST['id'];
$product_name = $_POST['name'];
@phatnguyenuit
phatnguyenuit / handleOnFocus.ts
Created January 4, 2020 11:01
handleOnFocus.ts
// Handle onFocus input
const handleOnFocus = useCallback(
(index: number) => () => {
focusInput(index);
},
[focusInput]
);
@phatnguyenuit
phatnguyenuit / focusInputHelper.ts
Created January 4, 2020 11:03
Focus `inputIndex` input
// Focus `inputIndex` input
const focusInput = useCallback(
(inputIndex: number) => {
const selectedIndex = Math.max(Math.min(length - 1, inputIndex), 0);
setActiveInput(selectedIndex);
},
[length]
);