From aad78601c5fe1c6d597a498805e017522822c137 Mon Sep 17 00:00:00 2001 From: Avram Walden Date: Tue, 2 Apr 2024 13:20:30 -0700 Subject: [PATCH] feat: fixes input typings --- app/frontend/Components/Flash/Flash.tsx | 4 +- app/frontend/Components/Form/Form.tsx | 13 +-- .../Components/Form/Inputs/Checkbox.tsx | 21 +++-- .../Components/Form/Inputs/CurrencyInput.tsx | 15 ++- .../Components/Form/Inputs/DateTime.tsx | 26 ++++-- .../Components/Form/Inputs/HiddenInput.tsx | 13 +-- .../Components/Form/Inputs/MultiSelect.tsx | 38 ++++---- .../Components/Form/Inputs/NumberInput.tsx | 32 +++---- .../Components/Form/Inputs/PasswordInput.tsx | 17 ++-- .../Components/Form/Inputs/RadioButtons.tsx | 22 +++-- .../Components/Form/Inputs/RichText.tsx | 24 +++-- .../Form/Inputs/SearchableDropdown.tsx | 93 ------------------- .../Components/Form/Inputs/Select.tsx | 55 ++++++----- .../Components/Form/Inputs/Switch.tsx | 61 +++++++----- .../Components/Form/Inputs/TextInput.tsx | 23 +++-- .../Components/Form/Inputs/Textarea.tsx | 72 +++++++------- app/frontend/Components/Form/Inputs/index.ts | 28 ++++++ app/frontend/Components/Form/index.ts | 15 +-- app/frontend/Components/Inputs/Checkbox.tsx | 6 +- .../Components/Inputs/CurrencyInput.tsx | 4 +- app/frontend/Components/Inputs/DateTime.tsx | 6 +- .../Components/Inputs/MultiSelect.tsx | 6 +- .../Components/Inputs/NumberInput.tsx | 6 +- .../Components/Inputs/PasswordInput.tsx | 6 +- .../Components/Inputs/RadioButtons.tsx | 6 +- app/frontend/Components/Inputs/RichText.tsx | 9 +- .../Components/Inputs/SearchableDropdown.tsx | 69 -------------- app/frontend/Components/Inputs/Switch.tsx | 14 ++- app/frontend/Components/Inputs/TextInput.tsx | 6 +- app/frontend/Components/Inputs/Textarea.tsx | 14 ++- app/frontend/Components/Inputs/index.ts | 1 - .../Components/RichTextEditor/index.tsx | 6 +- .../Table/Cell/HeadCellWithContext.tsx | 4 +- .../Components/Table/ColumnPicker.tsx | 5 +- app/frontend/Components/Table/Row/BodyRow.tsx | 4 +- app/frontend/Pages/Controls/Form.tsx | 5 +- app/frontend/lib/hooks/usePageProps.ts | 1 - app/frontend/types/inertia.d.ts | 48 ---------- .../types/serializers/Users/Share.d.ts | 4 +- app/serializers/users/share_serializer.rb | 1 + config/credentials.yml.enc | 1 - 41 files changed, 329 insertions(+), 475 deletions(-) delete mode 100644 app/frontend/Components/Form/Inputs/SearchableDropdown.tsx create mode 100644 app/frontend/Components/Form/Inputs/index.ts delete mode 100644 app/frontend/Components/Inputs/SearchableDropdown.tsx delete mode 100644 app/frontend/types/inertia.d.ts delete mode 100644 config/credentials.yml.enc diff --git a/app/frontend/Components/Flash/Flash.tsx b/app/frontend/Components/Flash/Flash.tsx index d94be66..58a612f 100644 --- a/app/frontend/Components/Flash/Flash.tsx +++ b/app/frontend/Components/Flash/Flash.tsx @@ -1,9 +1,9 @@ import React, { useEffect } from 'react' -import { usePage } from '@inertiajs/react' import { showNotification } from '@mantine/notifications' +import { usePageProps } from '@/lib/hooks' const Flash = () => { - const { flash } = usePage().props + const { flash } = usePageProps() useEffect(() => { let key: keyof FlashMessage diff --git a/app/frontend/Components/Form/Form.tsx b/app/frontend/Components/Form/Form.tsx index bf096a8..6af5be9 100644 --- a/app/frontend/Components/Form/Form.tsx +++ b/app/frontend/Components/Form/Form.tsx @@ -1,24 +1,17 @@ import React from 'react' import { Box } from '@mantine/core' import cx from 'clsx' -import { Form as InertiaForm, type FormProps, type NestedObject } from 'use-inertia-form' +import { Form as InertiaForm, type FormProps as FormProps, type NestedObject } from 'use-inertia-form' import * as classes from './Form.css' - -interface IFormProps extends FormProps { - grid?: boolean -} - -export interface ExtendableFormProps extends Omit, 'data'> {} - const Form = ( - { children, data, grid = true, className, railsAttributes = true, ...props }: IFormProps, + { children, data, className, railsAttributes = true, ...props }: FormProps, ) => { return ( diff --git a/app/frontend/Components/Form/Inputs/Checkbox.tsx b/app/frontend/Components/Form/Inputs/Checkbox.tsx index 3194049..a9e4a14 100644 --- a/app/frontend/Components/Form/Inputs/Checkbox.tsx +++ b/app/frontend/Components/Form/Inputs/Checkbox.tsx @@ -1,14 +1,16 @@ import React, { forwardRef } from 'react' import Field from '../Field' -import CheckboxInput, { type ICheckboxProps } from '@/Components/Inputs/Checkbox' +import CheckboxInput, { type CheckboxProps } from '@/Components/Inputs/Checkbox' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface IFormCheckboxProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface FormCheckboxProps + extends + Omit, + Omit, 'onFocus'> {} -const FormCheckboxComponent = forwardRef(( +const FormCheckboxComponent = forwardRef(( { name, onChange, @@ -18,19 +20,20 @@ const FormCheckboxComponent = forwardRef(( className, model, field = true, - ...props + ...props }, ref, ) => { const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model }) const handleChange = (e: React.ChangeEvent) => { - setValue(e.target.checked) - if(onChange) onChange(e, form) + const checked = e.target.checked + setValue(checked) + onChange?.(checked, form) } const handleBlur = () => { - if(onBlur) onBlur(value, form) + onBlur?.(value, form) } return ( diff --git a/app/frontend/Components/Form/Inputs/CurrencyInput.tsx b/app/frontend/Components/Form/Inputs/CurrencyInput.tsx index 517c448..4075e6d 100644 --- a/app/frontend/Components/Form/Inputs/CurrencyInput.tsx +++ b/app/frontend/Components/Form/Inputs/CurrencyInput.tsx @@ -1,13 +1,11 @@ import React, { forwardRef } from 'react' -import CurrencyInput, { type ICurrencyInputProps } from '@/Components/Inputs/CurrencyInput' +import CurrencyInput, { type CurrencyInputProps } from '@/Components/Inputs/CurrencyInput' import Field from '../Field' -import cx from 'clsx' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface INumberInputProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface INumberInputProps extends Omit, BaseFormInputProps {} const FormInput = forwardRef(( { @@ -15,9 +13,9 @@ const FormInput = forwardRef(( model, onChange, onBlur, + onFocus, id, required, - compact = false, field = true, ...props }, @@ -41,25 +39,24 @@ const FormInput = forwardRef(( return ( ( { children } ) } - condition={ field } > onFocus?.(e.target.value, form) } error={ error } ref={ ref } { ...props } diff --git a/app/frontend/Components/Form/Inputs/DateTime.tsx b/app/frontend/Components/Form/Inputs/DateTime.tsx index a76a1bf..811a3dc 100644 --- a/app/frontend/Components/Form/Inputs/DateTime.tsx +++ b/app/frontend/Components/Form/Inputs/DateTime.tsx @@ -1,11 +1,19 @@ import React from 'react' import Field from '../Field' -import DateTimeInput, { type IDateTimeProps } from '@/Components/Inputs/DateTime' -import { useInertiaInput } from 'use-inertia-form' +import DateTimeInput, { type DateTimeInputProps } from '@/Components/Inputs/DateTime' +import { UseFormProps, useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface IDateTimeFormProps extends Omit, IInertiaInputProps { - field?: boolean +interface IDateTimeFormProps + extends + Omit, + Omit { + + name: string + onChange: (date: Date, form: UseFormProps) => void + onBlur: (date: Date, form: UseFormProps) => void + onFocus: (date: Date, form: UseFormProps) => void } const DateTime = ({ @@ -13,6 +21,7 @@ const DateTime = ({ required, onChange, onBlur, + onFocus, id, model, field = true, @@ -23,11 +32,15 @@ const DateTime = ({ const handleChange = (date: Date) => { setValue(date) - if(onChange) onChange(date, form) + onChange?.(date, form) } const handleBlur = () => { - if(onBlur) onBlur(value, form) + onBlur?.(value, form) + } + + const handleFocus = () => { + onFocus?.(value, form) } return ( @@ -49,6 +62,7 @@ const DateTime = ({ value={ value } onChange={ handleChange } onBlur={ handleBlur } + onFocus={ handleFocus } required={ required } error={ error } { ...props } diff --git a/app/frontend/Components/Form/Inputs/HiddenInput.tsx b/app/frontend/Components/Form/Inputs/HiddenInput.tsx index 1006ce8..db47b6d 100644 --- a/app/frontend/Components/Form/Inputs/HiddenInput.tsx +++ b/app/frontend/Components/Form/Inputs/HiddenInput.tsx @@ -2,14 +2,15 @@ import React, { forwardRef } from 'react' import { HiddenInput } from '@/Components/Inputs' import { useInertiaInput } from 'use-inertia-form' import { InputProps } from 'react-html-props' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface ITextInputProps extends Omit, IInertiaInputProps { - name: string - model?: string -} +interface FormHiddenInputProps + extends + Omit, + Omit {} -const FormInput = forwardRef(( - { name, model, onChange, onBlur, id, ...props }, +const FormInput = forwardRef(( + { name, model, onChange, id, ...props }, ref, ) => { const { form, inputName, inputId, value, setValue } = useInertiaInput({ name, model }) diff --git a/app/frontend/Components/Form/Inputs/MultiSelect.tsx b/app/frontend/Components/Form/Inputs/MultiSelect.tsx index 5e1b7d1..9b4404c 100644 --- a/app/frontend/Components/Form/Inputs/MultiSelect.tsx +++ b/app/frontend/Components/Form/Inputs/MultiSelect.tsx @@ -1,25 +1,32 @@ import React from 'react' import { NestedObject, UseFormProps, useInertiaInput } from 'use-inertia-form' -import { IFormInputProps } from '.' import { ConditionalWrapper } from '@/Components' import Field from '../Field' -import MultiSelect, { type IMultiSelectProps } from '@/Components/Inputs/MultiSelect' +import MultiSelect, { type MultiSelectProps } from '@/Components/Inputs/MultiSelect' +import { type ComboboxData } from '@mantine/core' +import { type BaseFormInputProps } from '.' -type OmittedDropdownTypes = 'name'|'onBlur'|'onChange'|'onDropdownOpen'|'onDropdownClose' -export interface IFormDropdownProps - extends Omit, - IFormInputProps { - onDropdownOpen?: (form: UseFormProps) => void - onDropdownClose?: (form: UseFormProps) => void +type OmittedOverwrittenTypes = 'onFocus'|'onBlur'|'onChange'|'onClear'|'onDropdownOpen'|'onDropdownClose'|'onOptionSubmit' +export interface FormMultiSelectProps + extends Omit, + Omit { + + onChange?: (values: string[], options: ComboboxData, form: UseFormProps) => void + onBlur?: (values: string[], options: ComboboxData, form: UseFormProps) => void + onFocus?: (values: string[], options: ComboboxData, form: UseFormProps) => void + onClear?: (options: ComboboxData, form: UseFormProps) => void + onDropdownOpen?: (options: ComboboxData, form: UseFormProps) => void + onDropdownClose?: (options: ComboboxData, form: UseFormProps) => void + onOptionSubmit?: (values: string[], options: ComboboxData, form: UseFormProps) => void } const MultiSelectComponent = ( { + name, options = [], label, required, id, - name, errorKey, model, field = true, @@ -28,30 +35,31 @@ const MultiSelectComponent = ( onDropdownOpen, onDropdownClose, ...props - }: IFormDropdownProps, + }: FormMultiSelectProps, ) => { const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model, errorKey }) const handleBlur = () => { - if(onBlur) onBlur(value, form) + onBlur?.(value, options || [], form) } const handleChange = (values: string[]) => { setValue(values) - onChange?.(values, form) + onChange?.(values, options || [], form) } const handleDropdownOpen = () => { - if(onDropdownOpen) onDropdownOpen(form) + onDropdownOpen?.(options || [],form) } const handleDropdownClose = () => { - if(onDropdownClose) onDropdownClose(form) + onDropdownClose?.(options || [],form) } return ( ( ( { children } ) } - condition={ field } > ( onChange={ handleChange } onDropdownClose={ handleDropdownClose } onDropdownOpen={ handleDropdownOpen } - wrapper={ false } { ...props } /> diff --git a/app/frontend/Components/Form/Inputs/NumberInput.tsx b/app/frontend/Components/Form/Inputs/NumberInput.tsx index 9726174..24907bb 100644 --- a/app/frontend/Components/Form/Inputs/NumberInput.tsx +++ b/app/frontend/Components/Form/Inputs/NumberInput.tsx @@ -1,62 +1,54 @@ import React, { forwardRef } from 'react' -import NumberInput, { type INumberInputProps } from '@/Components/Inputs/NumberInput' +import NumberInput, { type NumberInputProps } from '@/Components/Inputs/NumberInput' import Field from '../Field' -import cx from 'clsx' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface INumberFormInputProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface FormNumberInputProps extends Omit, BaseFormInputProps {} -const FormInput = forwardRef(( +const FormInput = forwardRef(( { name, model, onChange, onBlur, + onFocus, id, required, - compact = false, field = true, ...props }, ref, ) => { - const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model }) + const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model }) - const handleChange = (e: number) => { - const v = e - setValue(v) + const handleChange = (val: string|number) => { + setValue(Number(val)) - if(onChange) onChange(v, form) - } - - const handleBlur = (e: React.FocusEvent) => { - if(onBlur) onBlur(value as number, form) + onChange?.(Number(val), form) } return ( ( { children } ) } - condition={ field } > onBlur?.(value, form) } + onFocus={ () => onFocus?.(value, form) } error={ error } ref={ ref } { ...props } diff --git a/app/frontend/Components/Form/Inputs/PasswordInput.tsx b/app/frontend/Components/Form/Inputs/PasswordInput.tsx index 2a40b25..75982b8 100644 --- a/app/frontend/Components/Form/Inputs/PasswordInput.tsx +++ b/app/frontend/Components/Form/Inputs/PasswordInput.tsx @@ -1,23 +1,21 @@ import React, { forwardRef } from 'react' -import PasswordInput, { type IPasswordInputProps } from '@/Components/Inputs/PasswordInput' +import PasswordInput, { type PasswordInputProps } from '@/Components/Inputs/PasswordInput' import Field from '../Field' -import cx from 'clsx' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface IPasswordFormInputProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface FormPasswordInputProps extends Omit, BaseFormInputProps {} -const FormInput = forwardRef(( +const FormInput = forwardRef(( { name, model, onChange, onBlur, + onFocus, id, required, - compact = false, field = true, ...props }, @@ -41,25 +39,24 @@ const FormInput = forwardRef(( return ( ( { children } ) } - condition={ field } > onFocus?.(e.target.value, form) } error={ error } ref={ ref } { ...props } diff --git a/app/frontend/Components/Form/Inputs/RadioButtons.tsx b/app/frontend/Components/Form/Inputs/RadioButtons.tsx index 183c74f..f19079f 100644 --- a/app/frontend/Components/Form/Inputs/RadioButtons.tsx +++ b/app/frontend/Components/Form/Inputs/RadioButtons.tsx @@ -1,12 +1,11 @@ import React from 'react' -import RadioButtons, { type IRadioButtonsProps } from '@/Components/Inputs/RadioButtons' +import RadioButtons, { type RadioButtonsProps } from '@/Components/Inputs/RadioButtons' import Field from '../Field' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface IFormRadioButtonsProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface FormRadioButtonsProps extends Omit, BaseFormInputProps {} const FormRadioButtons = ({ options, @@ -15,24 +14,29 @@ const FormRadioButtons = ({ model, onChange, onBlur, + onFocus, required, field = true, ...props -}: IFormRadioButtonsProps) => { +}: FormRadioButtonsProps) => { const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model }) const handleChange = (v: string) => { setValue(v) - - if(onChange) onChange(v, form) + onChange?.(v, form) } const handleBlur = (e: React.FocusEvent) => { - if(onBlur) onBlur(value, form) + onBlur?.(value, form) + } + + const handleFocus = (e: React.FocusEvent) => { + onFocus?.(value, form) } return ( ( ) } - condition={ field } > diff --git a/app/frontend/Components/Form/Inputs/RichText.tsx b/app/frontend/Components/Form/Inputs/RichText.tsx index dfe9ffc..fc9bf6e 100644 --- a/app/frontend/Components/Form/Inputs/RichText.tsx +++ b/app/frontend/Components/Form/Inputs/RichText.tsx @@ -1,13 +1,12 @@ import React from 'react' import Field from '../Field' -import RichTextInput, { type IRichTextProps } from '@/Components/Inputs/RichText' +import RichTextInput, { type RichTextInputProps } from '@/Components/Inputs/RichText' import cx from 'clsx' import { useInertiaInput } from 'use-inertia-form' import ConditionalWrapper from '@/Components/ConditionalWrapper' +import { type BaseFormInputProps, type InputConflicts } from '.' -interface IRichTextFormProps extends Omit, IInertiaInputProps { - field?: boolean -} +interface FormRichTextInputProps extends Omit, BaseFormInputProps {} const RichText = ({ label, @@ -16,22 +15,29 @@ const RichText = ({ id, onChange, onBlur, + onFocus, model, field = true, ...props -}: IRichTextFormProps) => { +}: FormRichTextInputProps) => { const { form, inputName, inputId, value, setValue, error } = useInertiaInput({ name, model }) - const handleChange = (v, delta, sources, editor) => { + const handleChange = (v: string) => { setValue(v) - if(onChange) onChange(v, form) + onChange?.(v, form) } + const handleBlur = () => { - if(onBlur) onBlur(value, form ) + onBlur?.(value, form ) + } + + const handleFocus = () => { + onFocus?.(value, form ) } return ( ( ) } - condition={ field } > <> { label &&