Skip to content

Instantly share code, notes, and snippets.

View cbrannen9a's full-sized avatar

Chris Brannen cbrannen9a

View GitHub Profile
@cbrannen9a
cbrannen9a / Example.js
Last active November 12, 2018 16:50
Example React Hook
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You have written {count} hooks</p>
<button onClick={() => setCount(count + 1)}>
@cbrannen9a
cbrannen9a / .eslintrc
Created November 12, 2018 15:57
Example eslint file for react-hooks
{
"extends": "react-app",
"plugins": [
"react-hooks"
],
"rules": {
"react-hooks/rules-of-hooks": "error"
}
}
@cbrannen9a
cbrannen9a / partial_fieldValidator2.js
Created November 8, 2018 12:20
Partial demo of credit card check using Regex and luhnCheck methods
const validateCardNumber = (number) => {
var regex = new RegExp("^[0-9]{16}$");
if (!regex.test(number)) { return false; }
return luhnCheck(number);
}
const luhnCheck = (val) => {
var sum = 0;
for (var i = 0; i < val.length; i++) {
@cbrannen9a
cbrannen9a / partial_checkout6.js
Created November 8, 2018 12:14
Partial Checkout6
handleValid = () => {
const { activeStep, validation } = this.state;
if (activeStep === 0 && !validation.hasErrorAddressForm) {
return false;
} else if (activeStep === 2) {
return false;
}
return true;
}
//...
@cbrannen9a
cbrannen9a / partial_formValidator.js
Created November 8, 2018 12:10
Partial formValidation for addressForm
export const formValidator = (validation, area) => {
if (area === 'addressForm') {
return !validation.firstName
|| !validation.lastName
|| !validation.address1
|| !validation.city
|| !validation.zip
|| !validation.country
}
return true;
@cbrannen9a
cbrannen9a / partial_checkout5.js
Created November 8, 2018 12:09
Partial checkout adding form validation
handleFormValidation = () => {
const { validation } = this.state;
this.setState({
validation: {
...validation,
hasErrorAddressForm: formValidator(validation, 'addressForm'),
}
});
}
@cbrannen9a
cbrannen9a / partial_addressForm.js
Created November 8, 2018 12:04
Partial Address Form with error and helperText
function AddressForm(props) {
const { state, handleChange } = props;
return (
<Fragment>
<Typography variant="h6" gutterBottom>
Shipping address
</Typography>
<Grid container spacing={24}>
<Grid item xs={12} sm={6}>
@cbrannen9a
cbrannen9a / partial_fieldValidator.js
Created November 8, 2018 12:00
Partial field validator
export const fieldValidator = (name, value) => {
let valid = false;
let message = '';
switch (name) {
case 'firstName':
valid = value.trim().length > 0;
message = validatorMessage(
valid,
@cbrannen9a
cbrannen9a / partial_checkout4.js
Last active November 8, 2018 11:54
The handle Change function with second argument in setState call and handleValidation
handleChange = (name, area) => event => {
const value = event.target.value;
this.setState({
[area]: {
...this.state[area],
[name]: value
}
}, () => { this.handleValidation(name, value); });
}
@cbrannen9a
cbrannen9a / partial_addressForm.js
Created November 8, 2018 11:48
Partial Address Form adding value and change handling
function AddressForm(props) {
const { state, handleChange } = props;
return (
<Fragment>
<Typography variant="h6" gutterBottom>
Shipping address
</Typography>
<Grid container spacing={24}>
<Grid item xs={12} sm={6}>