Skip to content

Commit

Permalink
feat(fields): set onChange value to undefined on disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Feb 7, 2023
1 parent 3c67e1d commit 323ef7a
Show file tree
Hide file tree
Showing 44 changed files with 155 additions and 139 deletions.
3 changes: 3 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
threshold: low
fileignoreconfig:
- filename: src/types.ts
checksum: e5e0f0c13cb4fe6f167758f40a6ee506b833c0919a1ec8f3b703ec431159e959
7 changes: 5 additions & 2 deletions src/fields/AutoComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import styled from 'styled-components'
import { Field } from '../elements/Field'
import { FieldError } from '../elements/FieldError'
import { Label } from '../elements/Label'
import { useClickOutside } from '../hooks/useClickOutside'
import { useClickOutsideEffect } from '../hooks/useClickOutsideEffect'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'
import { useForceUpdate } from '../hooks/useForceUpdate'
import { normalizeString } from '../utils/normalizeString'

Expand Down Expand Up @@ -127,7 +128,9 @@ export function AutoComplete({
setDefaultControlledValue(defaultValue)
}, [defaultValue])

useClickOutside(boxRef, close, baseContainer)
useFieldUndefineEffect(originalProps.disabled, onChange)

useClickOutsideEffect(boxRef, close, baseContainer)

useEffect(() => {
forceUpdate()
Expand Down
4 changes: 4 additions & 0 deletions src/fields/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useCallback, useMemo } from 'react'
import { Checkbox as RsuiteCheckbox } from 'rsuite'
import styled from 'styled-components'

import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'

import type { CheckboxProps as RsuiteCheckboxProps } from 'rsuite'
import type { ValueType } from 'rsuite/esm/Checkbox'
import type { Promisable } from 'type-fest'
Expand All @@ -28,6 +30,8 @@ export function Checkbox({ label, onChange, ...originalProps }: CheckboxProps) {
[onChange]
)

useFieldUndefineEffect(originalProps.disabled, onChange)

return (
<StyledCheckbox key={key} id={originalProps.name} onChange={handleChange} {...originalProps}>
{label}
Expand Down
7 changes: 5 additions & 2 deletions src/fields/DatePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import styled from 'styled-components'

import { Fieldset } from '../../elements/Fieldset'
import { Legend } from '../../elements/Legend'
import { useClickOutside } from '../../hooks/useClickOutside'
import { useClickOutsideEffect } from '../../hooks/useClickOutsideEffect'
import { useFieldUndefineEffect } from '../../hooks/useFieldUndefineEffect'
import { useForceUpdate } from '../../hooks/useForceUpdate'
import { getLocalizedDayjs } from '../../utils/getLocalizedDayjs'
import { getUtcizedDayjs } from '../../utils/getUtcizedDayjs'
Expand Down Expand Up @@ -214,7 +215,9 @@ export function DatePicker({
forceUpdate()
}, [forceUpdate])

useClickOutside(boxRef, closeCalendarPicker, baseContainer)
useFieldUndefineEffect(disabled, onChange)

useClickOutsideEffect(boxRef, closeCalendarPicker, baseContainer)

return (
<Fieldset disabled={disabled} {...nativeProps}>
Expand Down
4 changes: 2 additions & 2 deletions src/fields/DateRangePicker/TimeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'
import styled from 'styled-components'

import { useClickOutside } from '../../hooks/useClickOutside'
import { useClickOutsideEffect } from '../../hooks/useClickOutsideEffect'
import { useForceUpdate } from '../../hooks/useForceUpdate'
import { Clock } from '../../icons'
import { NumberInput } from './NumberInput'
Expand Down Expand Up @@ -150,7 +150,7 @@ function TimeInputWithRef(
onChange(nextTimeTuple)
}, [closeRangedTimePicker, onChange])

useClickOutside(boxRef, closeRangedTimePicker, baseContainer)
useClickOutsideEffect(boxRef, closeRangedTimePicker, baseContainer)

return (
<Box
Expand Down
7 changes: 5 additions & 2 deletions src/fields/DateRangePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import styled, { css } from 'styled-components'

import { Fieldset } from '../../elements/Fieldset'
import { Legend } from '../../elements/Legend'
import { useClickOutside } from '../../hooks/useClickOutside'
import { useClickOutsideEffect } from '../../hooks/useClickOutsideEffect'
import { useFieldUndefineEffect } from '../../hooks/useFieldUndefineEffect'
import { useForceUpdate } from '../../hooks/useForceUpdate'
import { getLocalizedDayjs } from '../../utils/getLocalizedDayjs'
import { getUtcizedDayjs } from '../../utils/getUtcizedDayjs'
Expand Down Expand Up @@ -304,7 +305,9 @@ export function DateRangePicker({
forceUpdate()
}, [forceUpdate])

useClickOutside([endDateInputRef, startDateInputRef], closeRangeCalendarPicker, baseContainer)
useFieldUndefineEffect(disabled, onChange)

useClickOutsideEffect([endDateInputRef, startDateInputRef], closeRangeCalendarPicker, baseContainer)

return (
<Fieldset {...nativeProps}>
Expand Down
7 changes: 7 additions & 0 deletions src/fields/MultiCheckbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { useCallback, useMemo, useRef } from 'react'
import styled, { css } from 'styled-components'

import { Fieldset } from '../elements/Fieldset'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'
import { Checkbox } from './Checkbox'

import type { Option } from '../types'
import type { Promisable } from 'type-fest'

export type MultiCheckboxProps = {
defaultValue?: string[] | undefined
disabled?: boolean | undefined
isInline?: boolean | undefined
isLabelHidden?: boolean | undefined
isLight?: boolean | undefined
Expand All @@ -20,6 +22,8 @@ export type MultiCheckboxProps = {
}
export function MultiCheckbox({
defaultValue = [],
// eslint-disable-next-line @typescript-eslint/naming-convention
disabled = false,
isInline = false,
isLabelHidden = false,
isLight = false,
Expand Down Expand Up @@ -49,6 +53,8 @@ export function MultiCheckbox({
[onChange]
)

useFieldUndefineEffect(disabled, onChange)

return (
<Fieldset key={key} isHidden={isLabelHidden} isLight={isLight} legend={label}>
<ChecboxesBox $isInline={isInline}>
Expand All @@ -57,6 +63,7 @@ export function MultiCheckbox({
// eslint-disable-next-line react/no-array-index-key
key={`${name}-${index}`}
defaultChecked={defaultValue.includes(option.value)}
disabled={disabled}
label={option.label}
name={`${name}${index}`}
onChange={(isChecked: boolean) => handleChange(option.value, isChecked)}
Expand Down
7 changes: 7 additions & 0 deletions src/fields/MultiRadio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { Radio } from 'rsuite'
import styled, { css } from 'styled-components'

import { Fieldset } from '../elements/Fieldset'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'

import type { Option } from '../types'
import type { Promisable } from 'type-fest'

export type MultiRadioProps = {
defaultValue?: string | undefined
disabled?: boolean | undefined
isInline?: boolean | undefined
isLabelHidden?: boolean | undefined
isLight?: boolean | undefined
Expand All @@ -19,6 +21,8 @@ export type MultiRadioProps = {
}
export function MultiRadio({
defaultValue,
// eslint-disable-next-line @typescript-eslint/naming-convention
disabled = false,
isInline = false,
isLabelHidden = false,
isLight = false,
Expand All @@ -44,6 +48,8 @@ export function MultiRadio({
[onChange]
)

useFieldUndefineEffect(disabled, onChange)

// TODO There may be a better solution.
// A key change is not enough to force radio checked check changes
// on `defaultValue` property update (even when appending `defaultValue` to `key`),
Expand All @@ -60,6 +66,7 @@ export function MultiRadio({
// eslint-disable-next-line react/no-array-index-key
key={`${name}-${index}`}
defaultChecked={option.value === checkedOptionValue}
disabled={disabled}
name={name}
onChange={(_: any, isChecked: boolean) => handleChange(option.value, isChecked)}
>
Expand Down
7 changes: 5 additions & 2 deletions src/fields/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import styled from 'styled-components'
import { Field } from '../elements/Field'
import { FieldError } from '../elements/FieldError'
import { Label } from '../elements/Label'
import { useClickOutside } from '../hooks/useClickOutside'
import { useClickOutsideEffect } from '../hooks/useClickOutsideEffect'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'
import { useForceUpdate } from '../hooks/useForceUpdate'
import { normalizeString } from '../utils/normalizeString'

Expand Down Expand Up @@ -96,7 +97,9 @@ export function MultiSelect({
[isOpen]
)

useClickOutside(boxRef, close, baseContainer)
useFieldUndefineEffect(originalProps.disabled, onChange)

useClickOutsideEffect(boxRef, close, baseContainer)

useEffect(() => {
forceUpdate()
Expand Down
3 changes: 3 additions & 0 deletions src/fields/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components'

import { Field } from '../elements/Field'
import { Label } from '../elements/Label'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'

import type { InputProps } from 'rsuite'
import type { Promisable } from 'type-fest'
Expand Down Expand Up @@ -43,6 +44,8 @@ export function NumberInput({
[onChange]
)

useFieldUndefineEffect(originalProps.disabled, onChange)

return (
<Field>
<Label htmlFor={originalProps.name} isHidden={isLabelHidden}>
Expand Down
7 changes: 5 additions & 2 deletions src/fields/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import styled from 'styled-components'
import { Field } from '../elements/Field'
import { FieldError } from '../elements/FieldError'
import { Label } from '../elements/Label'
import { useClickOutside } from '../hooks/useClickOutside'
import { useClickOutsideEffect } from '../hooks/useClickOutsideEffect'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'
import { useForceUpdate } from '../hooks/useForceUpdate'
import { normalizeString } from '../utils/normalizeString'

Expand Down Expand Up @@ -94,7 +95,9 @@ export function Select({
[isOpen]
)

useClickOutside(boxRef, close, baseContainer)
useFieldUndefineEffect(originalProps.disabled, onChange)

useClickOutsideEffect(boxRef, close, baseContainer)

useEffect(() => {
forceUpdate()
Expand Down
3 changes: 3 additions & 0 deletions src/fields/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components'

import { Field } from '../elements/Field'
import { Label } from '../elements/Label'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'

import type { InputProps } from 'rsuite'
import type { Promisable } from 'type-fest'
Expand Down Expand Up @@ -41,6 +42,8 @@ export function TextInput({
[onChange]
)

useFieldUndefineEffect(originalProps.disabled, onChange)

return (
<Field>
<Label htmlFor={originalProps.name} isHidden={isLabelHidden}>
Expand Down
3 changes: 3 additions & 0 deletions src/fields/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components'

import { Field } from '../elements/Field'
import { Label } from '../elements/Label'
import { useFieldUndefineEffect } from '../hooks/useFieldUndefineEffect'

import type { MutableRefObject, TextareaHTMLAttributes } from 'react'
import type { Promisable } from 'type-fest'
Expand Down Expand Up @@ -45,6 +46,8 @@ export function Textarea({
onChange(normalizedNextValue)
}, [onChange])

useFieldUndefineEffect(originalProps.disabled, onChange)

return (
<Field>
<Label htmlFor={originalProps.name} isHidden={isLabelHidden}>
Expand Down
17 changes: 5 additions & 12 deletions src/formiks/FormikAutoComplete.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useField } from 'formik'
import { useEffect, useMemo } from 'react'
import { useMemo } from 'react'

import { AutoComplete } from '../fields/AutoComplete'

Expand All @@ -9,21 +9,14 @@ export type FormikAutoCompleteProps = Omit<AutoCompleteProps, 'defaultValue' | '
export function FormikAutoComplete({ name, ...originalProps }: FormikAutoCompleteProps) {
const [field, , helpers] = useField<string | undefined>(name)

// eslint-disable-next-line react-hooks/exhaustive-deps, @typescript-eslint/naming-convention
// eslint-disable-next-line react-hooks/exhaustive-deps
const defaultValue = useMemo(() => field.value, [])

useEffect(
() => () => {
helpers.setValue(undefined)
},

// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleChange = useMemo(() => helpers.setValue, [])

if (!defaultValue) {
return <AutoComplete name={name} onChange={helpers.setValue} {...originalProps} />
}

return <AutoComplete defaultValue={defaultValue} name={name} onChange={helpers.setValue} {...originalProps} />
return <AutoComplete defaultValue={defaultValue} name={name} onChange={handleChange} {...originalProps} />
}
19 changes: 6 additions & 13 deletions src/formiks/FormikCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useField } from 'formik'
import { useEffect, useMemo } from 'react'
import { useMemo } from 'react'

import { Checkbox } from '../fields/Checkbox'

Expand All @@ -9,17 +9,10 @@ export type FormikCheckboxProps = Omit<CheckboxProps, 'checked' | 'defaultChecke
export function FormikCheckbox({ name, ...originalProps }: FormikCheckboxProps) {
const [field, , helpers] = useField(name)

// eslint-disable-next-line react-hooks/exhaustive-deps, @typescript-eslint/naming-convention
const defaultChecked = useMemo(() => Boolean(field.value), [])
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleChange = useMemo(() => helpers.setValue, [])
// eslint-disable-next-line react-hooks/exhaustive-deps
const isDefaultChecked = useMemo(() => Boolean(field.value), [])

useEffect(
() => () => {
helpers.setValue(undefined)
},

// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)

return <Checkbox defaultChecked={defaultChecked} name={name} onChange={helpers.setValue} {...originalProps} />
return <Checkbox defaultChecked={isDefaultChecked} name={name} onChange={handleChange} {...originalProps} />
}
15 changes: 4 additions & 11 deletions src/formiks/FormikDatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useField } from 'formik'
import { useEffect, useMemo } from 'react'
import { useMemo } from 'react'

import { DatePicker } from '../fields/DatePicker'

Expand All @@ -24,15 +24,8 @@ export function FormikDatePicker({ name, ...originalProps }: FormikDatePickerPro

// eslint-disable-next-line react-hooks/exhaustive-deps
const defaultValue = useMemo(() => field.value, [])
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleChange = useMemo(() => helpers.setValue, [])

useEffect(
() => () => {
helpers.setValue(undefined)
},

// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)

return <UntypedDatePicker defaultValue={defaultValue} onChange={helpers.setValue} {...originalProps} />
return <UntypedDatePicker defaultValue={defaultValue} onChange={handleChange} {...originalProps} />
}
15 changes: 4 additions & 11 deletions src/formiks/FormikDateRangePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useField } from 'formik'
import { useEffect, useMemo } from 'react'
import { useMemo } from 'react'

import { DateRangePicker } from '../fields/DateRangePicker'

Expand Down Expand Up @@ -28,15 +28,8 @@ export function FormikDateRangePicker({ name, ...originalProps }: FormikDateRang

// eslint-disable-next-line react-hooks/exhaustive-deps
const defaultValue = useMemo(() => field.value, [])
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleChange = useMemo(() => helpers.setValue, [])

useEffect(
() => () => {
helpers.setValue(undefined)
},

// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)

return <UntypedDateRangePicker defaultValue={defaultValue} onChange={helpers.setValue} {...originalProps} />
return <UntypedDateRangePicker defaultValue={defaultValue} onChange={handleChange} {...originalProps} />
}
Loading

0 comments on commit 323ef7a

Please sign in to comment.