-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat #218 - Add MultiSelect to Form, fix MS bugs
- Loading branch information
1 parent
6fd334e
commit 9ee21aa
Showing
11 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { BaseFieldProps } from '../types' | ||
import cn from 'classnames' | ||
import FieldError from '../FieldError' | ||
import FieldLabel from '../FieldLabel' | ||
import { Controller, useFormContext } from 'react-hook-form' | ||
import FieldContext, { FieldContextProps } from '../FieldContext' | ||
import { getFormFieldDataTag, getRulesForArrVals } from '../utils' | ||
import { MultiSelect, MultiSelectProps } from 'components/Select/MultiSelect' | ||
import React, { FC, useContext } from 'react' | ||
|
||
export interface FormMultiSelectProps | ||
extends BaseFieldProps, | ||
Omit<MultiSelectProps, 'defaultValues' | 'onChange' | 'values'> {} | ||
|
||
const FormMultiSelect: FC<FormMultiSelectProps> = ({ | ||
containerClasses = [], | ||
fieldErrorClasses = [], | ||
fullWidth = false, | ||
label, | ||
labelSkeletonWidth, | ||
name, | ||
required, | ||
rules = {}, | ||
...rest | ||
}: FormMultiSelectProps) => { | ||
const { clearErrors, control, errors } = useFormContext() | ||
const { loading } = useContext<FieldContextProps>(FieldContext) | ||
|
||
const errorMsg = errors[name] ? errors[name].message : '' | ||
|
||
const onFocus = () => { | ||
if (errors[name]) clearErrors(name) | ||
} | ||
|
||
return ( | ||
<div className={cn(containerClasses)}> | ||
{label && ( | ||
<FieldLabel | ||
fullWidth={fullWidth} | ||
label={label} | ||
loading={loading} | ||
required={required} | ||
skeletonWidth={labelSkeletonWidth} | ||
/> | ||
)} | ||
<Controller | ||
control={control} | ||
name={name} | ||
render={({ onChange, value }) => ( | ||
<MultiSelect | ||
dataTag={getFormFieldDataTag(name)} | ||
error={errors[name]} | ||
fullWidth={fullWidth} | ||
loading={loading} | ||
onChange={onChange} | ||
onFocus={onFocus} | ||
values={value} | ||
{...rest} | ||
/> | ||
)} | ||
rules={getRulesForArrVals({ required, rules })} | ||
/> | ||
<FieldError | ||
classes={fieldErrorClasses} | ||
error={errorMsg} | ||
fullWidth={fullWidth} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default FormMultiSelect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
import cloneDeep from 'lodash/cloneDeep' | ||
import React from 'react' | ||
import { ValidationRules } from 'react-hook-form' | ||
import FieldLabel, { FieldLabelProps } from './FieldLabel' | ||
|
||
export const getFormFieldDataTag = (tag: string): string => `field-${tag}` | ||
|
||
export const renderFieldLabel = (props: Partial<FieldLabelProps>) => { | ||
if (props.label) return <FieldLabel {...(props as FieldLabelProps)} /> | ||
} | ||
|
||
interface Params { | ||
rules?: ValidationRules | ||
required?: boolean | ||
} | ||
export const getRulesForArrVals = ({ | ||
rules = {}, | ||
required = false | ||
}: Params): ValidationRules => { | ||
const newRules = cloneDeep(rules) | ||
|
||
if (required) { | ||
newRules.required = true | ||
|
||
const required = (values: string[]) => values.length > 0 || '' | ||
|
||
if (!('validate' in newRules)) { | ||
newRules.validate = required | ||
} else { | ||
newRules.validate = | ||
typeof newRules.validate === 'object' | ||
? { ...newRules.validate, required } | ||
: { | ||
required, | ||
validate: newRules.validate! | ||
// eslint-disable-next-line no-mixed-spaces-and-tabs | ||
} | ||
} | ||
} | ||
|
||
return newRules | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters