Advertisement
Guest User

react-styled-component

a guest
Aug 20th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const App = () => {
  2.   const [emailVal, setEmailVal ] = useState('');
  3.   const [passwordVal, setPasswordVal ] = useState('');
  4.  
  5.   const handleChange = (e) => {
  6.     if (e.target.name === 'email') {
  7.       setEmailVal(e.target.value);
  8.       return
  9.     }
  10.  
  11.     setPasswordVal(e.target.value);
  12.   }
  13.  
  14.  
  15.   const inputStyle = {
  16.     appearance: 'none',
  17.     display: 'block',
  18.     verticalAlign: 'middle',
  19.     color: 'white',
  20.     width: '75%',
  21.     maxWidth: '38rem',
  22.     lineHeight: 'inherit',
  23.     letterSpacing: 'inherit',
  24.     fontFamily: 'inherit',
  25.     backgroundColor: 'transparent',
  26.     borderRadius: '5px',
  27.     borderWidth: '1px',
  28.     borderStyle: 'solid',
  29.     borderColor: 'rgb(221, 225, 228)',
  30.     transition: 'box-shadow 0.125s ease-out 0s',
  31.     margin: '0px'
  32.   }
  33.  
  34.   const inputs = [
  35.     {
  36.       data: { type: 'email', name: 'email', label: 'email', placeholder: 'enter email', required: true },
  37.       fieldStyle: { width: '75%', maxHeight: '5rem', justifyContent: 'space-between', flexDirection: 'column'},
  38.       inputStyle: { ...inputStyle }
  39.     },
  40.     {
  41.       data: { type: 'password', name: 'password', label: 'password', placeholder: 'enter password', required: true },
  42.       fieldStyle: { width: '75%', maxHeight: '5rem', justifyContent: 'space-between', flexDirection: 'column'},
  43.       inputStyle: { ...inputStyle }
  44.     }
  45.   ]
  46.  
  47.   const buttons = [
  48.     { text: 'Submit', type: 'submit', cb: null, style: 'squareButton' },
  49.     { text: 'Cancel', type: 'cancel', cb: null, style: 'squareButton' }
  50.   ]
  51.  
  52.   // const form = {
  53.   //   data: { name: 'signinForm', submit: 'signup', cb: signin },
  54.   //   style: { height: 1, justifyContent: 'space-around', flexDirection: 'column', px: 2, fontSizeModule: [1, 2, 3, 4]},
  55.   // }
  56.  
  57.   return (
  58.     <Box
  59.       fontSize="18px"
  60.       color="black"
  61.       backgroundColor="black"
  62.       height="50vh"
  63.       width="50vh"
  64.       as="form"
  65.     >
  66.       <Field
  67.         data={{ name: 'email', type: 'text', placeholder: 'enter email', label: 'email'}}
  68.         fieldStyle={{ width: '75%', maxHeight: '5rem', justifyContent: 'space-between', flexDirection: 'column'}}
  69.         inputStyle={{...inputStyle}}
  70.         errors={[]}
  71.         value={emailVal}
  72.         onChange={handleChange}
  73.       />
  74.       <Field
  75.         data={{ name: 'password', type: 'password', placeholder: 'enter password', label: 'email'}}
  76.         fieldStyle={{ width: '75%', maxHeight: '5rem', justifyContent: 'space-between', flexDirection: 'column'}}
  77.         inputStyle={{...inputStyle}}
  78.         errors={[]}
  79.         value={passwordVal}
  80.         onChange={handleChange}
  81.       />
  82.     </Box>
  83.   )
  84. }
  85.  
  86.  
  87. import styled from "styled-components";
  88. import React, { useState } from 'react'
  89.  
  90. import Box from './Box';
  91.  
  92. export const Error = styled(Box).attrs({
  93.   className: 'error',
  94.   color: 'error',
  95.   f: 1,
  96.   ml: 1,
  97.   my: 0
  98. })`
  99.   font-weight: normal;
  100.   &:before { content: '—'; }
  101. `
  102.  
  103. const handleErrorMessages = errorMessages => {
  104.   return errorMessages.map( errorMessage => <Box as="item" key={errorMessage}><Error>{errorMessage}</Error></Box> )
  105. }
  106.  
  107. const Field = ({ data: { name, type, placeholder, label }, fieldStyle, inputStyle, ...props }) => {
  108.   const [shown, setShown] = useState(false);
  109.   const errorColor = props.errors.length >= 1 ? '#e95667' : null;
  110.   const {display, ...inputStyleContainer } = inputStyle;
  111.   const {border, borderTop, borderRight, borderBottom, borderLeft, ...inputStyleNoBorder } = inputStyle;
  112.  
  113.   const inputType =
  114.     {
  115.       select: 'select',
  116.       slider: 'slider',
  117.       textarea: 'textarea'
  118.     }[type] || 'input'
  119.    
  120.     return (
  121.     <Box display="flex" {...fieldStyle}>
  122.       <Box
  123.         alignItems="flex-end"
  124.       >
  125.         <Box as="label" for={name} id={name}>
  126.           {label}
  127.         </Box>
  128.         {props.errors.length >= 1 ? <Box as="list" ml="2rem" color={errorColor} fontSize="1.2rem">{handleErrorMessages(props.errors)}</Box> : ''}
  129.       </Box>
  130.       <Box
  131.         {...inputStyleContainer}
  132.         display="flex"
  133.         borderColor={errorColor}
  134.         focusColor={errorColor}
  135.         foucsBoxShadowColor={errorColor}
  136.       >
  137.         <Box
  138.           {...props}
  139.           {...inputStyleNoBorder}
  140.           border="none"
  141.           forwardedAs={inputType}
  142.           name={name}
  143.           type={shown && type === "password" ? 'text' : type}
  144.           placeholder={placeholder}
  145.         />
  146.         { type === "password" ?
  147.         <Box
  148.           as="p"
  149.           fontSize="12px"
  150.           m="auto"
  151.           onClick={() => setShown(!shown)}
  152.           caps
  153.           cursor='pointer'
  154.         >
  155.           {shown ? 'hide':'show'}
  156.         </Box> : '' }
  157.       </Box>
  158.     </Box>
  159.   );
  160. };
  161.  
  162. Field.displayName = 'Field';
  163.  
  164. export default Field
  165.  
  166.  
  167. import React from 'react';
  168. import styled from 'styled-components';
  169. import { color, typography, layout, flexbox,
  170.   grid, background, border, position, shadow } from 'styled-system'
  171. import cleanElement from 'clean-element';
  172. import { filterProps } from './filterProps';
  173.  
  174. const Base = props => {
  175.  
  176.   const Div = styled.div``;
  177.   const next = filterProps(props)
  178.   return <Div {...next}/>;
  179. }
  180.  
  181. const Box = styled(cleanElement(Base))(
  182.   {
  183.     boxSizing: 'border-box',
  184.     minWidth: 0,
  185.   },
  186.   color, typography, layout, flexbox, grid, background, border, position, shadow
  187. );
  188.  
  189. export default Box;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement