Skip to content

Commit

Permalink
feat(form): allow array data on same input names
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Aug 10, 2024
1 parent 8eea251 commit c544671
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
56 changes: 35 additions & 21 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,35 @@ function formatTime(seconds: number): string {
return `${formattedMinutes}:${formattedSeconds}`
}

function Timer({ value }: { value: number }) {
const [timeLeft, setTimeLeft] = useState(600)
const [progress, setProgress] = useState(value)
function Timer({
value,
initialTime,
interval
}: {
value: number
initialTime: number
interval: number
}) {
const [progress, setProgress] = useState(0)
const [timeLeft, setTimeLeft] = useState(initialTime)

useEffect(() => {
const interval = setInterval(() => {
if (progress < 100) {
setTimeLeft((prevTimeLeft) => {
if (prevTimeLeft > 0) {
setProgress((prevProgress) => prevProgress + 100 / 600)
setTimeLeft(initialTime)
setProgress(0)
}, [value])

Check warning on line 76 in src/App.tsx

View workflow job for this annotation

GitHub Actions / ci

React Hook useEffect has a missing dependency: 'initialTime'. Either include it or remove the dependency array. If 'setTimeLeft' needs the current value of 'initialTime', you can also switch to useReducer instead of useState and read 'initialTime' in the reducer

return prevTimeLeft - 1
} else {
clearInterval(interval)
useEffect(() => {
if (timeLeft === 0) {
return
}

return prevTimeLeft
}
})
}
}, 1000)
const timer = setInterval(() => {
setTimeLeft((prevTime) => prevTime - 1)
setProgress((prevProgress) => prevProgress + 100 / initialTime)
}, interval)

return () => clearInterval(interval)
}, [progress])
return () => clearInterval(timer)
}, [timeLeft])

Check warning on line 89 in src/App.tsx

View workflow job for this annotation

GitHub Actions / ci

React Hook useEffect has missing dependencies: 'initialTime' and 'interval'. Either include them or remove the dependency array. If 'setProgress' needs the current value of 'initialTime', you can also switch to useReducer instead of useState and read 'initialTime' in the reducer

return (
<CircularProgress value={progress} size="lg">
Expand Down Expand Up @@ -218,7 +224,7 @@ export const App: React.FC = () => {
</Flexbox>
</WidgetWrapper>
<WidgetWrapper>
<Timer value={0} />
<Timer value={0} initialTime={600} interval={1_000} />
</WidgetWrapper>
</Flexbox>
</Section>
Expand Down Expand Up @@ -284,6 +290,14 @@ export const App: React.FC = () => {
}}
>
<Flexbox gap="md">
<Checkbox name="ingredients[]" value="sugar" label="Sugar" />
<Checkbox
name="ingredients[]"
value="eggs"
label="Eggs"
checked
/>
<Checkbox name="ingredients[]" value="bread" label="Bread" />
<Input name="username" placeholder="Enter your username..." />
<Input type="date" name="date" placeholder="Date..." />
<Input
Expand All @@ -303,8 +317,8 @@ export const App: React.FC = () => {
type="password"
/>
<Input name="content" placeholder="Your content..." multiline />
<Text>Length</Text>
<RangeSlider name="length" min={0} max={100} step={10} />
<Text>Random size</Text>
<RangeSlider name="random-size" min={0} max={100} step={10} />
<Switch name="notification" label="Enable notifications" />
<RadioGroup defaultValue="lemon">
<Flexbox gap="md">
Expand Down
18 changes: 17 additions & 1 deletion src/components/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ export function Form({ children, onSubmit }: FormProps) {

const form = event.currentTarget
const formData = new FormData(form)
const data = Object.fromEntries(formData)
const data: Record<string, unknown> = {}

for (const [key, value] of formData.entries()) {
if (data[key] && key.endsWith('[]')) {
if (Array.isArray(data[key])) {
data[key].push(value)

Check failure on line 21 in src/components/form/form.tsx

View workflow job for this annotation

GitHub Actions / ci

Unsafe call of an `any` typed value

Check failure on line 21 in src/components/form/form.tsx

View workflow job for this annotation

GitHub Actions / release

Object is of type 'unknown'.
} else {
data[key] = [data[key], value]
}
} else {
if (key.endsWith('[]')) {
data[key] = [value]
} else {
data[key] = value
}
}
}

onSubmit(data)
}
Expand Down

0 comments on commit c544671

Please sign in to comment.