|
| 1 | +import React from "react"; |
| 2 | +import styled from "styled-components"; |
| 3 | +import { FormError, FormInput, StyledFix, useForm } from "typed-react-form"; |
| 4 | +import tv, { SchemaType } from "typed-object-validator"; |
| 5 | + |
| 6 | +// https://github.com/styled-components/styled-components/issues/1349#issuecomment-375293558 |
| 7 | + |
| 8 | +const Container = styled.div` |
| 9 | + height: 100vh; |
| 10 | + /* background: #0001; */ |
| 11 | + display: flex; |
| 12 | + justify-content: center; |
| 13 | + align-items: center; |
| 14 | +`; |
| 15 | + |
| 16 | +const Title = styled.div` |
| 17 | + padding: 1.5rem; |
| 18 | + border-bottom: 1px solid #0002; |
| 19 | + font-size: 1.3rem; |
| 20 | + font-weight: bold; |
| 21 | +`; |
| 22 | + |
| 23 | +const FormContainer = styled.div` |
| 24 | + background: white; |
| 25 | + border-radius: 0.25em; |
| 26 | + box-shadow: 3px 3px 15px 3px #0002; |
| 27 | +
|
| 28 | + min-width: 300px; |
| 29 | +`; |
| 30 | + |
| 31 | +const Form = styled.form` |
| 32 | + padding: 1.5em; |
| 33 | + display: grid; |
| 34 | + gap: 0.8em; |
| 35 | + align-items: center; |
| 36 | + grid-template-columns: 100px 1fr; |
| 37 | +`; |
| 38 | + |
| 39 | +const StyledButton = styled.button` |
| 40 | + cursor: pointer; |
| 41 | + appearance: none; |
| 42 | + padding: 0.5em 1em; |
| 43 | + background: #e22; |
| 44 | + color: white; |
| 45 | + font-weight: bold; |
| 46 | + border: none; |
| 47 | + border-radius: 0.3em; |
| 48 | + font-size: inherit; |
| 49 | + font-family: inherit; |
| 50 | + transition: 100ms; |
| 51 | +
|
| 52 | + &:hover { |
| 53 | + transition: 100ms; |
| 54 | + background: #e22d; |
| 55 | + } |
| 56 | +`; |
| 57 | + |
| 58 | +// Must specify type explicitly |
| 59 | +const StyledInput: StyledFix<typeof FormInput> = styled(FormInput)` |
| 60 | + border: 2px solid #0002; |
| 61 | + border-radius: 0.3em; |
| 62 | + padding: 0.5em; |
| 63 | + outline: none; |
| 64 | +
|
| 65 | + &.typed-form-error { |
| 66 | + border-color: red; |
| 67 | + } |
| 68 | +
|
| 69 | + &.typed-form-dirty { |
| 70 | + border-color: #0005; |
| 71 | + } |
| 72 | +`; |
| 73 | + |
| 74 | +// You can also just use typeof without StyledFix, but this does not support additional styled properties |
| 75 | +const StyledError: typeof FormError = styled(FormError)` |
| 76 | + font-weight: bold; |
| 77 | + color: red; |
| 78 | + margin: 0.1em 0; |
| 79 | + grid-column: 2; |
| 80 | +`; |
| 81 | + |
| 82 | +const FormDataSchema = tv.object({ |
| 83 | + name: tv.string().min(1), |
| 84 | + email: tv.email("Invalid email").min(1) |
| 85 | +}); |
| 86 | +type FormData = SchemaType<typeof FormDataSchema>; |
| 87 | + |
| 88 | +export function StyledForm() { |
| 89 | + const form = useForm<FormData>({ name: "", email: "" }, (values) => FormDataSchema.validate(values) ?? ({} as any)); |
| 90 | + |
| 91 | + function submit() { |
| 92 | + console.log(form.values); |
| 93 | + alert(JSON.stringify(form.values, null, 2)); |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <Container> |
| 98 | + <FormContainer> |
| 99 | + <Title> |
| 100 | + Form using <a href="https://styled-components.com/">styled-components</a> |
| 101 | + </Title> |
| 102 | + <Form onSubmit={form.handleSubmit(submit)}> |
| 103 | + <label>Name</label> |
| 104 | + <StyledInput form={form} name="name" /> |
| 105 | + <StyledError form={form} name="name" /> |
| 106 | + <label>Email</label> |
| 107 | + <StyledInput form={form} name="email" /> |
| 108 | + <StyledError form={form} name="email" /> |
| 109 | + <StyledButton type="submit" style={{ gridColumn: "span 2" }}> |
| 110 | + Submit |
| 111 | + </StyledButton> |
| 112 | + </Form> |
| 113 | + </FormContainer> |
| 114 | + </Container> |
| 115 | + ); |
| 116 | +} |
0 commit comments