From 13f08e481de00af68e64f9a5af0fb64961304b4b Mon Sep 17 00:00:00 2001 From: FelixBlaisThon <52010042+FelixBlaisThon@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:00:43 -0500 Subject: [PATCH] Ad UI 9399 fix get input props (#3602) * 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 --- .../collection/__tests__/Collection.spec.tsx | 45 ++++++++++++++----- .../src/form/__tests__/useForm.spec.tsx | 18 ++++++++ packages/mantine/src/form/useForm.ts | 34 ++++++++++++-- .../form/collection/Collection.demo.tsx | 2 +- .../layout/Table/TableClientSide.demo.tsx | 8 ++-- 5 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 packages/mantine/src/form/__tests__/useForm.spec.tsx diff --git a/packages/mantine/src/components/collection/__tests__/Collection.spec.tsx b/packages/mantine/src/components/collection/__tests__/Collection.spec.tsx index f83fca8a34..96b34ace17 100644 --- a/packages/mantine/src/components/collection/__tests__/Collection.spec.tsx +++ b/packages/mantine/src/components/collection/__tests__/Collection.spec.tsx @@ -9,7 +9,7 @@ describe('Collection', () => { const Fixture = () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( - newItem="" {...form.getInputProps('fruits')}> + newItem="" {...form.getInputProps('fruits', {type: 'collection'})}> {(name) => {name}} ); @@ -29,7 +29,7 @@ describe('Collection', () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( <> - + {(name) => {name}}
{JSON.stringify(form.values)}
@@ -58,7 +58,11 @@ describe('Collection', () => { const Fixture = () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( - + {(name) => {name}} ); @@ -88,7 +92,7 @@ describe('Collection', () => { return ( <> - + {(name) => {name}} @@ -107,7 +111,7 @@ describe('Collection', () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( <> - + {(name) => {name}}
{JSON.stringify(form.values)}
@@ -133,7 +137,11 @@ describe('Collection', () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( <> - + {(name) => {name}}
{JSON.stringify(form.values)}
@@ -155,7 +163,12 @@ describe('Collection', () => { const Fixture = () => { const form = useForm({initialValues: {fruits: ['banana']}}); return ( - + {(name) => {name}} ); @@ -175,7 +188,12 @@ describe('Collection', () => { const Fixture = () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( - + {(name) => {name}} ); @@ -198,7 +216,12 @@ describe('Collection', () => { const Fixture = () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( - + {(name) => {name}} ); @@ -228,7 +251,7 @@ describe('Collection', () => { const form = useForm({initialValues: {fruits: ['banana', 'orange']}}); return ( <> - + {(name) => {name}} @@ -249,7 +272,7 @@ describe('Collection', () => { return ( { + 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'); + }); +}); diff --git a/packages/mantine/src/form/useForm.ts b/packages/mantine/src/form/useForm.ts index fcc1eae555..4353acae30 100644 --- a/packages/mantine/src/form/useForm.ts +++ b/packages/mantine/src/form/useForm.ts @@ -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 = >( + 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, + TransformValues extends _TransformValues = (values: Values) => Values, +>( + options: UseFormInput = {}, +) => { const form = useMantineForm(options); const getInputProps: GetInputProps> = ( 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), diff --git a/packages/website/src/examples/form/collection/Collection.demo.tsx b/packages/website/src/examples/form/collection/Collection.demo.tsx index c33a250001..001bb703d6 100644 --- a/packages/website/src/examples/form/collection/Collection.demo.tsx +++ b/packages/website/src/examples/form/collection/Collection.demo.tsx @@ -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) => ( <> diff --git a/packages/website/src/examples/layout/Table/TableClientSide.demo.tsx b/packages/website/src/examples/layout/Table/TableClientSide.demo.tsx index 31ca1ca781..69bd235af4 100644 --- a/packages/website/src/examples/layout/Table/TableClientSide.demo.tsx +++ b/packages/website/src/examples/layout/Table/TableClientSide.demo.tsx @@ -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 = (row, columnId, value) => rankItem(row.getValue(columnId), value).passed;