-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/form] Add form rerendering story
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable no-spaced-func */ | ||
import React, { useCallback, useState, useRef, memo } from 'react'; | ||
import { TextInput, TextInputProps, MultiSelect, MultiSelectProps } from '@mantine/core'; | ||
|
||
export default { title: 'Form' }; | ||
|
||
const TestInput = memo((props: TextInputProps) => { | ||
console.log(`Rerender ${props.name}`); | ||
return <TextInput {...props} />; | ||
}); | ||
|
||
const TestMultiSelect = memo((props: MultiSelectProps) => { | ||
console.log(`Rerender ${props.name}`); | ||
return <MultiSelect {...props} />; | ||
}); | ||
|
||
function useTestForm() { | ||
const [state, setState] = useState({ first: '', second: [] }); | ||
const handleChange = useCallback((path: string, value: any) => { | ||
setState((current) => ({ ...current, [path]: value })); | ||
}, []); | ||
|
||
const callbacks = useRef<Record<string, (event: any) => void>>({}); | ||
|
||
const getInputProps = (path: string) => { | ||
if (!(path in callbacks.current)) { | ||
callbacks.current[path] = (event: any) => | ||
handleChange(path, 'currentTarget' in event ? event.currentTarget.value : event); | ||
} | ||
|
||
return { | ||
value: state[path], | ||
onChange: callbacks.current[path], | ||
}; | ||
}; | ||
|
||
return { getInputProps, values: state }; | ||
} | ||
|
||
const data = ['1', '2', '3']; | ||
|
||
export function RerenderingTest() { | ||
const form = useTestForm(); | ||
|
||
return ( | ||
<div style={{ padding: 40 }}> | ||
<TestInput name="first" {...form.getInputProps('first')} /> | ||
<TestMultiSelect name="second" {...form.getInputProps('second')} data={data} /> | ||
</div> | ||
); | ||
} |