This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input {...input({name:"input-name", type:"text", required:true ....})}> | |
/*or*/ | |
<input {...input("input-name", "text")}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Input | |
name="name" | |
onChange={e => setInput({ name: e.target.value })} | |
label="Name" | |
error={errors.name} | |
defaultValue={form.name} | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Observable { | |
constructor() { | |
this.observers = []; | |
} | |
subscribe(fn) { | |
this.observers = [...this.observers, fn]; | |
return () => { | |
this.unsubscribe(fn); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div className="form-group"> | |
<button type="button" className="btn btn-primary" disabled={!isValid}>Submit</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => { |
NewerOlder