Skip to content

Instantly share code, notes, and snippets.

View JoaoCnh's full-sized avatar
🎯
Focusing

João Cunha JoaoCnh

🎯
Focusing
View GitHub Profile
@JoaoCnh
JoaoCnh / UserForm.js
Created September 8, 2017 20:46
UserForm
import React from 'react';
import { Formik } from 'formik';
import Yup from 'yup';
import VirtualizedSelect from 'react-virtualized-select';
import 'react-select/dist/react-select.css';
import 'react-virtualized/styles.css';
import 'react-virtualized-select/styles.css';
const imaginaryThings = [
@JoaoCnh
JoaoCnh / schema.js
Last active September 30, 2023 17:59
Yup conditional validation
Yup.object().shape({
email: Yup.string().email('Invalid email address').required('Email is required!'),
username: Yup.string().required('This man needs a username').when('email', (email, schema) => {
if (email === '[email protected]') { return schema.min(10); }
return schema;
}),
});
@JoaoCnh
JoaoCnh / schema.js
Created September 8, 2017 21:18
Yup Validation 2
Yup.object().shape({
email: Yup.string().email('Invalid email address').required('Email is required!'),
username: Yup.string().required('This man needs a ${path}').when('email', (email, schema) => {
if (email === '[email protected]') {
return schema.label('papidipupi').min(10);
}
return schema.label('babidibiba');
});
@JoaoCnh
JoaoCnh / schema.js
Created September 8, 2017 21:30
Yup Validation 3
Yup.object().shape({
email: Yup.string().email('Invalid email address').required('Email is required!'),
username: Yup.string().required('This man needs a ${path}').when('email', (email, schema) => {
if (email === '[email protected]') {
return schema.label('papidipupi').min(10);
}
return schema.label('babidibiba');
}).test('is-zigzagging', '${path} is not zigzagging', value => value === 'zigzagging'),
});
@JoaoCnh
JoaoCnh / ex.jsx
Created December 18, 2017 14:14
jsx formik file upload
<input id="file" name="file" type="file" onChange={(event) => {
setFieldValue("file", event.currentTarget.files[0]);
}} />
@JoaoCnh
JoaoCnh / ex1.js
Created December 18, 2017 14:18
example file data
JSON.stringify({
fileName: values.file.name,
type: values.file.type,
size: `${values.file.size} bytes`
}, null, 2);
@JoaoCnh
JoaoCnh / CarsList.js
Created January 18, 2018 23:40
Cars list component
export default class CarsList extends React.Component {
state = {
cars: [],
isLoading: false,
};
_fetch = async () => {
const res = await fetch("api/url");
const json = await res.json();
@JoaoCnh
JoaoCnh / List.js
Created January 19, 2018 00:44
List render props
import React from "react";
import PropTypes from "prop-types";
class List extends React.Component {
static propTypes = {
render: PropTypes.func.isRequired,
url: PropTypes.string.isRequired,
};
state = {
@JoaoCnh
JoaoCnh / comp.js
Created January 19, 2018 00:52
React Comp
<List
url="https://api.github.com/users/JoaoCnh/repos"
render={({ list, isLoading }) => (
<div>
<h2>My repos</h2>
{isLoading && <h2>Loading...</h2>}
<ul>
{list.length > 0 && list.map(repo => (
<li key={repo.id}>
@JoaoCnh
JoaoCnh / AjaxForm.js
Last active January 22, 2018 09:23
Ajax Form Component
import React from "react";
import PropTypes from "prop-types";
class AjaxForm extends React.Component {
static propTypes = {
type: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
onSuccess: PropTypes.func.isRequired,
};