refactor form input fields

This commit is contained in:
Thomas Ruoff
2020-09-02 00:01:18 +02:00
committed by Thomas Ruoff
parent 0ed66962ba
commit b92ff0e3d8
5 changed files with 149 additions and 179 deletions

View File

@@ -1,6 +1,6 @@
import React, { useContext } from 'react' import React, { useContext } from 'react'
import { WizardContext } from './context/wizardStore' import { WizardContext } from './context/wizardStore'
import Required from './required' import Input from './input'
export default function Contact() { export default function Contact() {
const { state, onChangeEvent } = useContext(WizardContext) const { state, onChangeEvent } = useContext(WizardContext)
@@ -9,77 +9,42 @@ export default function Contact() {
return ( return (
<> <>
<div className="fsw"> <Input
<div className="fs"> label="Name"
<label className="flabel"> required
Name <Required />
</label>
<input
type="text"
name="name" name="name"
value={name} value={name}
onChange={onChangeEvent} onChange={onChangeEvent}
required
className="input-text"
/> />
</div> <Input
</div> label="E-Mail"
<div className="fsw">
<div className="fs">
<label className="flabel">
E-Mail <Required />
</label>
<input
type="email" type="email"
name="email" name="email"
value={email} value={email}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className="input-text"
/> />
</div> <Input
</div> label="Straße"
<div className="fsw">
<div className="fs">
<label className="flabel">
Straße <Required />
</label>
<input
type="text"
name="street" name="street"
value={street} value={street}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className="input-text"
/> />
</div> <Input
</div> label="PLZ"
<div className="fsw">
<div className="fs">
<label className="flabel">
PLZ <Required />
</label>
<input
type="text"
name="zip" name="zip"
value={zip} value={zip}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className="input-text"
/> />
<label className="flabel"> <Input
Stadt <Required /> label="Stadt"
</label>
<input
type="text"
name="city" name="city"
value={city} value={city}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className="input-text"
/> />
</div>
</div>
</> </>
) )
} }

View File

@@ -7,7 +7,7 @@ import useSWR from 'swr'
import { getNextBigger, getNextSmaller } from '../../helpers/array' import { getNextBigger, getNextSmaller } from '../../helpers/array'
import { dateFormatBackend } from '../../helpers/date' import { dateFormatBackend } from '../../helpers/date'
import { WizardContext } from './context/wizardStore' import { WizardContext } from './context/wizardStore'
import Required from './required' import InputWrapper from './inputWrapper'
const fetcher = (path: string) => fetch(path).then((r) => r.json()) const fetcher = (path: string) => fetch(path).then((r) => r.json())
@@ -70,11 +70,7 @@ export default function DateSelect() {
} }
return ( return (
<div className="fsw"> <InputWrapper label="Datum" required>
<div className="fs">
<label className="flabel">
Datum <Required />
</label>
<DayPickerInput <DayPickerInput
ref={fromRef} ref={fromRef}
inputProps={{ className: 'input-text', required: true }} inputProps={{ className: 'input-text', required: true }}
@@ -128,7 +124,6 @@ export default function DateSelect() {
></path> ></path>
</svg> </svg>
</button> </button>
</div> </InputWrapper>
</div>
) )
} }

View File

@@ -0,0 +1,17 @@
import React from 'react'
import InputWrapper from './inputWrapper'
interface InputProps extends React.InputHTMLAttributes<T> {
label: string
}
export default function Input(props: InputProps) {
const { label, required, type = 'text', ...rest } = props
return (
<InputWrapper label="label" required={required}>
<input type={type} required={required} className="input-text" {...rest} />
</InputWrapper>
)
}

View File

@@ -0,0 +1,23 @@
import React from 'react'
import Required from './required'
export default function Input(props: {
label: string
required: boolean
children: React.ReactChildren
}) {
const { label, required, children } = props
return (
<>
<div className="fsw">
<div className="fs">
<label className="flabel">
{label} {required && <Required />}
</label>
{children}
</div>
</div>
</>
)
}

View File

@@ -1,59 +1,29 @@
import React, { useContext } from 'react' import React, { useContext } from 'react'
import { WizardContext } from './context/wizardStore' import { WizardContext } from './context/wizardStore'
import Required from './required' import Input from './input'
export default function Contact() { export default function Contact() {
const { state, onChangeEvent } = useContext(WizardContext) const { state, onChangeEvent } = useContext(WizardContext)
const { formDataChanged } = state
const { purpose, destination, org } = state.formData const { purpose, destination, org } = state.formData
return ( return (
<> <>
<div className="fsw"> <Input
<div className="fs"> label="Zweck der Fahrt"
<label className="flabel">
Zweck der Fahrt <Required />
</label>
<input
type="text"
name="purpose" name="purpose"
value={purpose} value={purpose}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className={`input-text ${
formDataChanged.includes('purpose') && 'input-changed'
}`}
/> />
</div> <Input
</div> label="Ziel der Fahrt"
<div className="fsw">
<div className="fs">
<label className="flabel">
Ziel der Fahrt <Required />
</label>
<input
type="text"
name="destination" name="destination"
value={destination} value={destination}
onChange={onChangeEvent} onChange={onChangeEvent}
required required
className="input-text"
/> />
</div> <Input label="Verein" name="org" value={org} onChange={onChangeEvent} />
</div>
<div className="fsw">
<div className="fs">
<label className="flabel">Verein</label>
<input
type="text"
name="org"
value={org}
onChange={onChangeEvent}
className="input-text"
/>
</div>
</div>
</> </>
) )
} }