Skip to content

Commit

Permalink
Ad UI 9399 fix get input props (#3602)
Browse files Browse the repository at this point in the history
* chore(mantine): bump to mantine v7, first commit
rebasing

* chore(mantine): lots of css modules, preparing theme and components
rebasing

* chore(mantine)!: more breaking stuff

* chore(mantine)!: more module, less v6, build working

* chore(mantine)!: trying to style my branch so it looks 1:1 with master

* chore(mantine)!: apply some visual fixes to look like prod

* chore(mantine)!: fixed table style and rows

* chore(mantine)!: fixed all the rest style expect table gutter grid

* chore(mantine)!: fix interfaces and remove comments

* chore(useForm)!: change condition in useform hook

* chore(useForm)!: fixed collection demo

* chore(useForm): fixed ternary and faker-js

* chore(useForm): removed console.log

* chore(useForm)!: fix unit test
  • Loading branch information
FelixBlaisThon authored Jan 9, 2024
1 parent 47dad99 commit 4f1917a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Collection', () => {
const Fixture = () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<Collection<string> newItem="" {...form.getInputProps('fruits')}>
<Collection<string> newItem="" {...form.getInputProps('fruits', {type: 'collection'})}>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
);
Expand All @@ -29,7 +29,7 @@ describe('Collection', () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<>
<Collection newItem="" {...form.getInputProps('fruits')}>
<Collection newItem="" {...form.getInputProps('fruits', {type: 'collection'})}>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
<div data-testid="form-state">{JSON.stringify(form.values)}</div>
Expand Down Expand Up @@ -58,7 +58,11 @@ describe('Collection', () => {
const Fixture = () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<Collection newItem="" {...form.getInputProps('fruits')} onRemoveItem={onRemoveItemSpy}>
<Collection
newItem=""
{...form.getInputProps('fruits', {type: 'collection'})}
onRemoveItem={onRemoveItemSpy}
>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
);
Expand Down Expand Up @@ -88,7 +92,7 @@ describe('Collection', () => {
return (
<>
<button onClick={() => setDisabled(true)}>disable</button>
<Collection newItem="" {...form.getInputProps('fruits')} disabled={disabled}>
<Collection newItem="" {...form.getInputProps('fruits', {type: 'collection'})} disabled={disabled}>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
</>
Expand All @@ -107,7 +111,7 @@ describe('Collection', () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<>
<Collection newItem="new" {...form.getInputProps('fruits')}>
<Collection newItem="new" {...form.getInputProps('fruits', {type: 'collection'})}>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
<div data-testid="form-state">{JSON.stringify(form.values)}</div>
Expand All @@ -133,7 +137,11 @@ describe('Collection', () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<>
<Collection newItem="new" {...form.getInputProps('fruits')} allowAdd={allowAdd}>
<Collection
newItem="new"
{...form.getInputProps('fruits', {type: 'collection'})}
allowAdd={allowAdd}
>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
<div data-testid="form-state">{JSON.stringify(form.values)}</div>
Expand All @@ -155,7 +163,12 @@ describe('Collection', () => {
const Fixture = () => {
const form = useForm({initialValues: {fruits: ['banana']}});
return (
<Collection data-testid="collection" {...form.getInputProps('fruits')} newItem="new" required>
<Collection
data-testid="collection"
{...form.getInputProps('fruits', {type: 'collection'})}
newItem="new"
required
>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
);
Expand All @@ -175,7 +188,12 @@ describe('Collection', () => {
const Fixture = () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<Collection data-testid="collection" {...form.getInputProps('fruits')} newItem="new" required>
<Collection
data-testid="collection"
{...form.getInputProps('fruits', {type: 'collection'})}
newItem="new"
required
>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
);
Expand All @@ -198,7 +216,12 @@ describe('Collection', () => {
const Fixture = () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<Collection data-testid="collection" {...form.getInputProps('fruits')} newItem="new" required>
<Collection
data-testid="collection"
{...form.getInputProps('fruits', {type: 'collection'})}
newItem="new"
required
>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
);
Expand Down Expand Up @@ -228,7 +251,7 @@ describe('Collection', () => {
const form = useForm({initialValues: {fruits: ['banana', 'orange']}});
return (
<>
<Collection newItem="new" {...form.getInputProps('fruits')} draggable>
<Collection newItem="new" {...form.getInputProps('fruits', {type: 'collection'})} draggable>
{(name) => <span data-testid="item">{name}</span>}
</Collection>
</>
Expand All @@ -249,7 +272,7 @@ describe('Collection', () => {
return (
<Collection
data-testid="collection"
{...form.getInputProps('fruits')}
{...form.getInputProps('fruits', {type: 'collection'})}
newItem="new"
required
draggable
Expand Down
18 changes: 18 additions & 0 deletions packages/mantine/src/form/__tests__/useForm.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {renderHook} from '../../__tests__/Utils';
import {useForm} from '../useForm';

describe('useForm', () => {
it("returns 'onReorderItem', 'onRemoveItem' & 'onInsertItem' if the form is 'collection' type", () => {
const view = renderHook(() => useForm({initialValues: {fruits: ['banana', 'orange']}}));

const propsTypeCollection = view.result.current.getInputProps('fruits', {type: 'collection'});
expect(typeof propsTypeCollection.onReorderItem).toBe('function');
expect(typeof propsTypeCollection.onRemoveItem).toBe('function');
expect(typeof propsTypeCollection.onInsertItem).toBe('function');

const propsNotCollection = view.result.current.getInputProps('fruits');
expect(typeof propsNotCollection.onReorderItem).toBe('undefined');
expect(typeof propsNotCollection.onRemoveItem).toBe('undefined');
expect(typeof propsNotCollection.onInsertItem).toBe('undefined');
});
});
34 changes: 30 additions & 4 deletions packages/mantine/src/form/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
import {useForm as useMantineForm} from '@mantine/form';
import {GetInputProps} from '@mantine/form/lib/types';
import {LooseKeys, UseFormInput, _TransformValues} from '@mantine/form/lib/types';

export const useForm: typeof useMantineForm = (options) => {
type InputTypes = 'input' | 'checkbox' | 'collection';

type GetInputProps<Values> = <Field extends LooseKeys<Values>>(
path: Field,
options?: {type?: InputTypes; withError?: boolean; withFocus?: boolean},
) => {
value: any;
onChange: any;
checked?: any;
error?: any;
onFocus?: any;
onBlur?: any;
onReorderItem?: any;
onRemoveItem?: any;
onInsertItem?: any;
};

export const useForm = <
Values = Record<string, unknown>,
TransformValues extends _TransformValues<Values> = (values: Values) => Values,
>(
options: UseFormInput<Values, TransformValues> = {},
) => {
const form = useMantineForm(options);

const getInputProps: GetInputProps<Record<string, unknown>> = (
path,
{type = 'input', withError = type === 'input', withFocus = true} = {},
) => {
const originalPayload = form.getInputProps(path, {type, withError, withFocus});
if (Array.isArray(originalPayload.value)) {
const originalPayload = form.getInputProps(path, {
type: type === 'collection' ? 'input' : type,
withError,
withFocus,
});
if (type === 'collection') {
return {
...originalPayload,
onReorderItem: (payload: Record<'from' | 'to', number>) => form.reorderListItem(path, payload),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Demo = () => {
description="These will have to be done by next week"
label="List of tasks"
newItem={{name: '', done: false}}
{...form.getInputProps('todoList')}
{...form.getInputProps('todoList', {type: 'collection'})}
>
{(_task, index) => (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const makeData = (len: number): Person[] =>
Array(len)
.fill(0)
.map(() => ({
id: faker.datatype.uuid(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
age: faker.datatype.number(40),
id: faker.string.uuid(),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
age: faker.number.int(40),
}));

const fuzzyFilter: FilterFn<Person> = (row, columnId, value) => rankItem(row.getValue(columnId), value).passed;
Expand Down

0 comments on commit 4f1917a

Please sign in to comment.