-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/chart-props
- Loading branch information
Showing
157 changed files
with
5,140 additions
and
322 deletions.
There are no files selected for viewing
324 changes: 162 additions & 162 deletions
324
.yarn/releases/yarn-4.4.1.cjs → .yarn/releases/yarn-4.5.0.cjs
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
nodeLinker: node-modules | ||
|
||
yarnPath: .yarn/releases/yarn-4.4.1.cjs | ||
yarnPath: .yarn/releases/yarn-4.5.0.cjs |
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
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
116 changes: 116 additions & 0 deletions
116
apps/help.mantine.dev/src/demos/CustomInputUseForm.demo.tsx
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,116 @@ | ||
/* eslint-disable no-console */ | ||
import { Button, Text } from '@mantine/core'; | ||
import { isNotEmpty, useForm } from '@mantine/form'; | ||
import { useUncontrolled } from '@mantine/hooks'; | ||
import { MantineDemo } from '@mantinex/demo'; | ||
|
||
const code = ` | ||
import { Button, Text } from '@mantine/core'; | ||
import { isNotEmpty, useForm } from '@mantine/form'; | ||
import { useUncontrolled } from '@mantine/hooks'; | ||
interface CustomInputProps extends Omit<React.ComponentPropsWithoutRef<'input'>, 'onChange'> { | ||
value?: string; | ||
defaultValue?: string; | ||
onChange?: (value: string) => void; | ||
error?: React.ReactNode; | ||
} | ||
function CustomInput({ value, defaultValue, onChange, error, ...others }: CustomInputProps) { | ||
const [_value, setValue] = useUncontrolled({ | ||
value, | ||
defaultValue, | ||
finalValue: '', | ||
onChange, | ||
}); | ||
return ( | ||
<div> | ||
<input value={_value} onChange={(event) => setValue(event.currentTarget.value)} {...others} /> | ||
<Text c="red">{error}</Text> | ||
</div> | ||
); | ||
} | ||
function Demo() { | ||
const form = useForm({ | ||
mode: 'uncontrolled', | ||
initialValues: { | ||
customInput: '', | ||
}, | ||
validate: { | ||
customInput: isNotEmpty('This field is required'), | ||
}, | ||
}); | ||
return ( | ||
<form onSubmit={form.onSubmit(console.log)}> | ||
<CustomInput | ||
{...form.getInputProps('customInput')} | ||
key={form.key('customInput')} | ||
placeholder="Custom input" | ||
/> | ||
<Button type="submit" mt="md"> | ||
Submit | ||
</Button> | ||
</form> | ||
); | ||
} | ||
`; | ||
|
||
interface CustomInputProps extends Omit<React.ComponentPropsWithoutRef<'input'>, 'onChange'> { | ||
value?: string; | ||
defaultValue?: string; | ||
onChange?: (value: string) => void; | ||
error?: React.ReactNode; | ||
} | ||
|
||
function CustomInput({ value, defaultValue, onChange, error, ...others }: CustomInputProps) { | ||
const [_value, setValue] = useUncontrolled({ | ||
value, | ||
defaultValue, | ||
finalValue: '', | ||
onChange, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<input value={_value} onChange={(event) => setValue(event.currentTarget.value)} {...others} /> | ||
<Text c="red">{error}</Text> | ||
</div> | ||
); | ||
} | ||
|
||
function Demo() { | ||
const form = useForm({ | ||
mode: 'uncontrolled', | ||
initialValues: { | ||
customInput: '', | ||
}, | ||
validate: { | ||
customInput: isNotEmpty('This field is required'), | ||
}, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={form.onSubmit(console.log)}> | ||
<CustomInput | ||
{...form.getInputProps('customInput')} | ||
key={form.key('customInput')} | ||
placeholder="Custom input" | ||
/> | ||
|
||
<Button type="submit" mt="md"> | ||
Submit | ||
</Button> | ||
</form> | ||
); | ||
} | ||
|
||
export const CustomInputUseFormDemo: MantineDemo = { | ||
type: 'code', | ||
component: Demo, | ||
code, | ||
centered: true, | ||
}; |
5 changes: 5 additions & 0 deletions
5
apps/help.mantine.dev/src/demos/MultiSelectPlaceholder.demo.module.css
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,5 @@ | ||
.inputField { | ||
&:not(:only-child)::placeholder { | ||
color: transparent; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
apps/help.mantine.dev/src/demos/MultiSelectPlaceholder.demo.tsx
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,31 @@ | ||
import { MultiSelect } from '@mantine/core'; | ||
import { MantineDemo } from '@mantinex/demo'; | ||
import classes from './MultiSelectPlaceholder.demo.module.css'; | ||
|
||
const code = ` | ||
`; | ||
|
||
const cssCode = `.inputField { | ||
&:not(:only-child)::placeholder { | ||
color: transparent; | ||
} | ||
}`; | ||
|
||
function Demo() { | ||
return ( | ||
<MultiSelect | ||
data={['React', 'Angular']} | ||
placeholder="Pick one or more" | ||
classNames={{ inputField: classes.inputField }} | ||
/> | ||
); | ||
} | ||
|
||
export const MultiSelectPlaceholderDemo: MantineDemo = { | ||
type: 'code', | ||
component: Demo, | ||
code: [ | ||
{ fileName: 'Demo.module.css', code: cssCode, language: 'scss' }, | ||
{ fileName: 'Demo.tsx', code, language: 'tsx' }, | ||
], | ||
}; |
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
75 changes: 75 additions & 0 deletions
75
apps/help.mantine.dev/src/pages/q/custom-input-use-form.mdx
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,75 @@ | ||
import { CustomInputUseFormDemo } from '@/demos/CustomInputUseForm.demo'; | ||
import { Layout } from '@/layout'; | ||
|
||
export const meta = { | ||
title: 'How to integrate custom input with use-form hook?', | ||
description: 'Learn how to add use-form support for custom inputs', | ||
slug: 'custom-input-use-form', | ||
category: 'forms', | ||
tags: ['use-form', 'useForm'], | ||
created_at: 'July 22, 2024', | ||
last_updated_at: 'July 22, 2024', | ||
}; | ||
|
||
export default Layout(meta); | ||
|
||
## How @mantine/form works | ||
|
||
[use-form](https://mantine.dev/form/use-form) is used to store form values, | ||
errors, touched, dirty and validation state. In order to support all `@mantine/form` | ||
features (including `form.getInputProps()`) custom inputs must accept the following props: | ||
|
||
- `value` – input value in controlled mode | ||
- `defaultValue` – input value in uncontrolled mode | ||
- `onChange` – input change handler used both for controlled and uncontrolled modes | ||
- `error` – validation error message | ||
- `onBlur` – used to set field as touched | ||
|
||
## use-uncontrolled hook | ||
|
||
In most cases, the easiest way to add support for both controlled and uncontrolled modes is to use | ||
[use-uncontrolled](https://mantine.dev/hooks/use-uncontrolled) hook. It allows to use `value` and | ||
`defaultValue` props together and automatically handles controlled/uncontrolled mode switching. | ||
|
||
Example of a custom input that supports both controlled and uncontrolled modes: | ||
|
||
```tsx | ||
import { useUncontrolled } from '@mantine/hooks'; | ||
|
||
interface CustomInputProps { | ||
value?: string; | ||
defaultValue?: string; | ||
onChange?: (value: string) => void; | ||
} | ||
|
||
function CustomInput({ | ||
value, | ||
defaultValue, | ||
onChange, | ||
}: CustomInputProps) { | ||
const [_value, handleChange] = useUncontrolled({ | ||
value, | ||
defaultValue, | ||
finalValue: 'Final', | ||
onChange, | ||
}); | ||
|
||
return ( | ||
<input | ||
type="text" | ||
value={_value} | ||
onChange={(event) => handleChange(event.currentTarget.value)} | ||
/> | ||
); | ||
} | ||
``` | ||
|
||
## Full example | ||
|
||
In the following example `CustomInput` component supports all `@mantine/form` features: | ||
|
||
- `value`, `defaultValue` and `onChange` are used to control input value | ||
- `error` is used to display validation error message | ||
- `onBlur` (part of `...others` props) is used to set field as touched | ||
|
||
<Demo data={CustomInputUseFormDemo} /> |
Oops, something went wrong.