View changelog with demos on mantine.dev
Form context
@mantine/form
package now exports createFormContext
function to create provider component,
hook to get form object from context and use-form hook with predefined type:
import { createFormContext } from '@mantine/form';
import { TextInput } from '@mantine/core';
// Definition of form values is required
interface FormValues {
age: number;
name: string;
}
// createFormContext returns a tuple with 3 items:
// FormProvider is a component that sets form context
// useFromContext hook return form object that was previously set in FormProvider
// useForm hook works the same way as useForm exported from the package but has predefined type
const [FormProvider, useFormContext, useForm] = createFormContext<FormValues>();
function ContextField() {
const form = useFormContext();
return <TextInput label="Your name" {...form.getInputProps('name')} />;
}
export function Context() {
// Create form as described in use-form documentation
const form = useForm({
initialValues: {
age: 0,
name: '',
},
});
// Wrap your form with FormProvider
return (
<FormProvider form={form}>
<form onSubmit={form.onSubmit(() => {})}>
<ContextField />
</form>
</FormProvider>
);
}
New features
- Indicator component now includes more features to work with number labels and
precessing
prop - Switch component now supports
thumbIcon
prop and any React node can now be used ononLabel
andoffLabel
props - Grid.Col component now supports setting column span (and other related responsive props) to
auto
andcontent
:
use-previous hook
use-previous hook stores the previous value of a state in a ref,
it returns undefined on initial render and the previous value of a state after rerender:
import { TextInput, Text } from '@mantine/core';
import { usePrevious, useInputState } from '@mantine/hooks';
function Demo() {
const [value, setValue] = useInputState('');
const previousValue = usePrevious(value);
return (
<div>
<TextInput
label="Enter some text here"
placeholder="Enter some text here"
id="previous-demo-input"
value={value}
onChange={setValue}
/>
<Text mt="md">Current value: {value}</Text>
<Text>Previous value: {previousValue}</Text>
</div>
);
}
Other changes
- ColorSwatch now supports
withShadow
prop - MultiSelect dropdown is no longer opened when selected item is removed
- Radio.Group component now supports
name
prop to set name on every child Radio component - AppShell component now supports
hidden
prop to hide Header, Footer, Navbar and Aside components - Carousel component now supports
skipSnaps
andcontainScroll
props - NumberInput
type
can now be changed - NotificationsProvider now supports
target
prop to set Portal target - New use-session-storage hook
New Contributors
- @emewjin made their first contribution in #2205
- @MalikBagwala made their first contribution in #2222
- @kabeer05 made their first contribution in #2313
- @leeweisberger made their first contribution in #2317
- @AmelloAster made their first contribution in #2237
Full Changelog: 5.2.7...5.3.0