From 323ef7ace51acdc117187118bc2a3a8b7ac59ed6 Mon Sep 17 00:00:00 2001 From: Ivan Gabriele Date: Tue, 7 Feb 2023 03:47:38 +0100 Subject: [PATCH] feat(fields): set onChange value to undefined on disabled --- .talismanrc | 3 +++ src/fields/AutoComplete.tsx | 7 +++++-- src/fields/Checkbox.tsx | 4 ++++ src/fields/DatePicker/index.tsx | 7 +++++-- src/fields/DateRangePicker/TimeInput.tsx | 4 ++-- src/fields/DateRangePicker/index.tsx | 7 +++++-- src/fields/MultiCheckbox.tsx | 7 +++++++ src/fields/MultiRadio.tsx | 7 +++++++ src/fields/MultiSelect.tsx | 7 +++++-- src/fields/NumberInput.tsx | 3 +++ src/fields/Select.tsx | 7 +++++-- src/fields/TextInput.tsx | 3 +++ src/fields/Textarea.tsx | 3 +++ src/formiks/FormikAutoComplete.tsx | 17 +++++---------- src/formiks/FormikCheckbox.tsx | 19 ++++++----------- src/formiks/FormikDatePicker.tsx | 15 ++++--------- src/formiks/FormikDateRangePicker.tsx | 15 ++++--------- src/formiks/FormikMultiCheckbox.tsx | 15 ++++--------- src/formiks/FormikMultiRadio.tsx | 15 ++++--------- src/formiks/FormikMultiSelect.tsx | 15 ++++--------- src/formiks/FormikNumberInput.tsx | 15 ++++--------- src/formiks/FormikSelect.tsx | 15 ++++--------- src/formiks/FormikTextInput.tsx | 15 ++++--------- src/formiks/FormikTextarea.tsx | 15 ++++--------- ...ickOutside.ts => useClickOutsideEffect.ts} | 2 +- src/hooks/useFieldUndefineEffect.ts | 21 +++++++++++++++++++ src/index.ts | 4 ++-- src/types.ts | 11 ++++++++++ stories/fields/Checkbox.stories.tsx | 1 + stories/fields/MultiCheckbox.stories.tsx | 1 + stories/fields/MultiRadio.stories.tsx | 1 + stories/fields/MultiSelect.stories.tsx | 1 + stories/fields/NumberInput.stories.tsx | 1 + stories/fields/Select.stories.tsx | 1 + stories/formiks/FormikCheckbox.stories.tsx | 1 + stories/formiks/FormikDatePicker.stories.tsx | 1 + .../formiks/FormikDateRangePicker.stories.tsx | 1 + .../formiks/FormikMultiCheckbox.stories.tsx | 1 + stories/formiks/FormikMultiRadio.stories.tsx | 1 + stories/formiks/FormikMultiSelect.stories.tsx | 1 + stories/formiks/FormikNumberInput.stories.tsx | 1 + stories/formiks/FormikSelect.stories.tsx | 1 + stories/formiks/FormikTextInput.stories.tsx | 1 + stories/formiks/FormikTextarea.stories.tsx | 1 + 44 files changed, 155 insertions(+), 139 deletions(-) rename src/hooks/{useClickOutside.ts => useClickOutsideEffect.ts} (96%) create mode 100644 src/hooks/useFieldUndefineEffect.ts diff --git a/.talismanrc b/.talismanrc index 8dbab9407..27eb76ecb 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1 +1,4 @@ threshold: low +fileignoreconfig: + - filename: src/types.ts + checksum: e5e0f0c13cb4fe6f167758f40a6ee506b833c0919a1ec8f3b703ec431159e959 diff --git a/src/fields/AutoComplete.tsx b/src/fields/AutoComplete.tsx index 516d73e25..059e9293c 100644 --- a/src/fields/AutoComplete.tsx +++ b/src/fields/AutoComplete.tsx @@ -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' @@ -127,7 +128,9 @@ export function AutoComplete({ setDefaultControlledValue(defaultValue) }, [defaultValue]) - useClickOutside(boxRef, close, baseContainer) + useFieldUndefineEffect(originalProps.disabled, onChange) + + useClickOutsideEffect(boxRef, close, baseContainer) useEffect(() => { forceUpdate() diff --git a/src/fields/Checkbox.tsx b/src/fields/Checkbox.tsx index c3624958e..d939a0e40 100644 --- a/src/fields/Checkbox.tsx +++ b/src/fields/Checkbox.tsx @@ -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' @@ -28,6 +30,8 @@ export function Checkbox({ label, onChange, ...originalProps }: CheckboxProps) { [onChange] ) + useFieldUndefineEffect(originalProps.disabled, onChange) + return ( {label} diff --git a/src/fields/DatePicker/index.tsx b/src/fields/DatePicker/index.tsx index 2b4ea1e19..ec5254ce5 100644 --- a/src/fields/DatePicker/index.tsx +++ b/src/fields/DatePicker/index.tsx @@ -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' @@ -214,7 +215,9 @@ export function DatePicker({ forceUpdate() }, [forceUpdate]) - useClickOutside(boxRef, closeCalendarPicker, baseContainer) + useFieldUndefineEffect(disabled, onChange) + + useClickOutsideEffect(boxRef, closeCalendarPicker, baseContainer) return (
diff --git a/src/fields/DateRangePicker/TimeInput.tsx b/src/fields/DateRangePicker/TimeInput.tsx index a5fb00aab..68935e114 100644 --- a/src/fields/DateRangePicker/TimeInput.tsx +++ b/src/fields/DateRangePicker/TimeInput.tsx @@ -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' @@ -150,7 +150,7 @@ function TimeInputWithRef( onChange(nextTimeTuple) }, [closeRangedTimePicker, onChange]) - useClickOutside(boxRef, closeRangedTimePicker, baseContainer) + useClickOutsideEffect(boxRef, closeRangedTimePicker, baseContainer) return ( diff --git a/src/fields/MultiCheckbox.tsx b/src/fields/MultiCheckbox.tsx index 7c4749844..9c2265a23 100644 --- a/src/fields/MultiCheckbox.tsx +++ b/src/fields/MultiCheckbox.tsx @@ -3,6 +3,7 @@ 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' @@ -10,6 +11,7 @@ import type { Promisable } from 'type-fest' export type MultiCheckboxProps = { defaultValue?: string[] | undefined + disabled?: boolean | undefined isInline?: boolean | undefined isLabelHidden?: boolean | undefined isLight?: boolean | undefined @@ -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, @@ -49,6 +53,8 @@ export function MultiCheckbox({ [onChange] ) + useFieldUndefineEffect(disabled, onChange) + return (
@@ -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)} diff --git a/src/fields/MultiRadio.tsx b/src/fields/MultiRadio.tsx index fe4c0ac32..561a10f47 100644 --- a/src/fields/MultiRadio.tsx +++ b/src/fields/MultiRadio.tsx @@ -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 @@ -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, @@ -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`), @@ -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)} > diff --git a/src/fields/MultiSelect.tsx b/src/fields/MultiSelect.tsx index af3f61d31..389f067d6 100644 --- a/src/fields/MultiSelect.tsx +++ b/src/fields/MultiSelect.tsx @@ -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' @@ -96,7 +97,9 @@ export function MultiSelect({ [isOpen] ) - useClickOutside(boxRef, close, baseContainer) + useFieldUndefineEffect(originalProps.disabled, onChange) + + useClickOutsideEffect(boxRef, close, baseContainer) useEffect(() => { forceUpdate() diff --git a/src/fields/NumberInput.tsx b/src/fields/NumberInput.tsx index 8dc3453c6..021284271 100644 --- a/src/fields/NumberInput.tsx +++ b/src/fields/NumberInput.tsx @@ -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' @@ -43,6 +44,8 @@ export function NumberInput({ [onChange] ) + useFieldUndefineEffect(originalProps.disabled, onChange) + return (