Skip to content

Commit

Permalink
feat #195 - Fix error bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Jan 22, 2021
1 parent 55806e5 commit 2a2fd47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
28 changes: 20 additions & 8 deletions src/components/ChipInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ChipInputProps
extends Omit<BaseFormElementProps, 'onChange' | 'value'>,
Pick<InputProps, 'addonAfter' | 'addonBefore' | 'inputRef' | 'onFocus'>,
Pick<BaseFieldProps, 'fieldErrorClasses'> {
clearErrros?: () => void
defaultValues?: string[]
errorMsg?: string
onChange?: (addedValues: string[]) => void
Expand All @@ -41,18 +42,19 @@ export interface ChipInputProps
export const ChipInput: FC<ChipInputProps> = ({
addonAfter,
addonBefore,
onChange,
classes = [],
clearErrros,
dataTag,
defaultValues,
disabled = false,
error,
errorMsg = '',
fieldErrorClasses = [],
disabled = false,
inputRef,
onFocus,
fullWidth,
inputRef,
loading = false,
onFocus,
onChange,
placeholder,
undeleteableValues = [],
validate,
Expand All @@ -63,6 +65,7 @@ export const ChipInput: FC<ChipInputProps> = ({
)
const [inputValue, setInputValue] = useState('')
const [isInvalidValue, setIsInvalidValue] = useState(false)
const [localError, setLocalError] = useState(false)
const [localErrorMsg, setLocalErrorMsg] = useState('')

const componentClasses = useStyles({ fullWidth })
Expand Down Expand Up @@ -90,7 +93,11 @@ export const ChipInput: FC<ChipInputProps> = ({

const onInputChange = (event: ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value.toLowerCase())

setLocalErrorMsg('')
setLocalError(false)

if (clearErrros) clearErrros()
}

const onKeyDown = (e: KeyboardEvent<Element>) => {
Expand All @@ -103,7 +110,12 @@ export const ChipInput: FC<ChipInputProps> = ({
if (validate) {
validated = validate(inputValue)

if (typeof validated === 'string') setLocalErrorMsg(validated)
if (validated !== true) {
setLocalError(true)

if (typeof validated === 'string')
setLocalErrorMsg(validated)
}
}

if (!isInvalidValue && !disabled && validated === true)
Expand Down Expand Up @@ -134,10 +146,10 @@ export const ChipInput: FC<ChipInputProps> = ({
))

useEffect(() => {
!inputValue || addedValues.includes(inputValue) || error
!inputValue || addedValues.includes(inputValue) || localError || error
? setIsInvalidValue(true)
: setIsInvalidValue(false)
}, [addedValues, inputValue, error])
}, [addedValues, inputValue, localError, error])

if (values && !onChange)
throw new Error('Controlled chip inputs require an onChange prop')
Expand All @@ -151,7 +163,7 @@ export const ChipInput: FC<ChipInputProps> = ({
<Input
addonBefore={addonBefore}
disabled={disabled}
error={!!localErrorMsg || error}
error={localError || error}
fullWidth={fullWidth}
inputRef={inputRef}
loading={loading}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Template: Story<FormProps<UserModel>> = (args: FormProps<UserModel>) => (
required
rules={{
validate: (values: string[]) =>
values.length > 2 || 'Please provide at least two domains'
values.length > 1 || 'Please provide at least two domains'
}}
undeleteableValues={['@lorem.com']}
/>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Form/FormChipInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const FormChipInput: FC<FormChipInputProps> = ({

const errorMsg = errors[name] ? errors[name].message : ''

const onChipInputFocus = () => {
const clearChipInputErrors = () => {
if (errors[name]) clearErrors(name)
}

Expand Down Expand Up @@ -62,14 +62,15 @@ const FormChipInput: FC<FormChipInputProps> = ({
name={name}
render={({ onChange, value }) => (
<ChipInput
clearErrros={clearChipInputErrors}
dataTag={getFormFieldDataTag(name)}
error={errors[name]}
errorMsg={errorMsg}
fullWidth={fullWidth}
inputRef={inputRef}
loading={loading}
onChange={onChange}
onFocus={onChipInputFocus}
onFocus={clearChipInputErrors}
values={value}
{...rest}
/>
Expand Down

0 comments on commit 2a2fd47

Please sign in to comment.