Skip to content

Instantly share code, notes, and snippets.

View jucian0's full-sized avatar
🇧🇷

Juciano jucian0

🇧🇷
View GitHub Profile
import "./styles.css";
import PubSub from "./PubSub";
const firstInput = document.getElementById("first-input");
const secondInput = document.getElementById("second-input");
const firstSubscriberBtn = document.getElementById("first-subscriber-btn");
const secondSubscriberBtn = document.getElementById("second-subscriber-btn");
const firstUnSubscriberBtn = document.getElementById("first-un-subscriber-btn");
const secondUnSubscriberBtn = document.getElementById(
"second-un-subscriber-btn"
class PubSub {
constructor() {
this.subscribers = {};
}
subscribe(event, fn) {
if (Array.isArray(this.subscribers[event])) {
this.subscribers[event] = [...this.subscribers[event], fn];
} else {
this.subscribers[event] = [fn];
<input {...input({name:"input-name", type:"text", required:true ....})}>
/*or*/
<input {...input("input-name", "text")}>
const initialValues = {
name:"Jesse",
email:"jesse@jesse.com",
pets:["felix"],
accept:false
}
const validation = yup.object().shape({
name: yup.string().required("This field is required"),
<Input
name="name"
onChange={e => setInput({ name: e.target.value })}
label="Name"
error={errors.name}
defaultValue={form.name}
/>
import "./styles.css";
import Observable from "./Observer";
const input = document.getElementById("text-input");
const firstSubscriberBtn = document.getElementById("first-subscriber-btn");
const secondSubscriberBtn = document.getElementById("second-subscriber-btn");
const firstUnSubscriberBtn = document.getElementById("first-un-subscriber-btn");
const secondUnSubscriberBtn = document.getElementById(
"second-un-subscriber-btn"
);
class Observable {
constructor() {
this.observers = [];
}
subscribe(fn) {
this.observers = [...this.observers, fn];
return () => {
this.unsubscribe(fn);
};
<div className="form-group">
<button type="button" className="btn btn-primary" disabled={!isValid}>Submit</button>
</div>
import { useState, useEffect, useCallback } from 'react'
import { ValidationError } from "yup"
const useValidation = (values, schema) => {
const [errors, setErrors] = useState({})
const [isValid, setIsValid] = useState(false)
const validate = useCallback(async () => {
try {
await schema.validate(values, { abortEarly: false })
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { debounce } from '../Debounce';
const Input = ({ error, label, onChange, ...rest }) => {
const [touched, setTouched] = useState(false)
const inputRef = useRef(null)
const debounceInput = useCallback(debounce(onChange, 500), [debounce])
const blurInput = useCallback(() => setTouched(true), [setTouched])
useEffect(() => {