Skip to content

Commit

Permalink
fix: add react-select default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jlison committed Sep 22, 2019
1 parent 00959de commit 7dc255c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default ({
elementConfig={elementConfig}
valid={valid}
formHooks={formHooks}
defaultValues={defaultValues}
/>
);
});
Expand Down
12 changes: 11 additions & 1 deletion src/components/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Props extends DefaultInputProps {
}

/** Render a generic single select input */
export default ({elementConfig, formHooks}: Props) => {
export default ({defaultValues, elementConfig, formHooks}: Props) => {
const {
className,
isMulti,
Expand All @@ -26,6 +26,7 @@ export default ({elementConfig, formHooks}: Props) => {
placeholder,
required,
} = elementConfig;
const initialValue = defaultValues ? defaultValues[name] : undefined;
const {formState, register, setValue} = formHooks;
const {touched} = formState;
const {t} = useTranslation();
Expand Down Expand Up @@ -64,6 +65,15 @@ export default ({elementConfig, formHooks}: Props) => {
register({name, required});
}, [name, register, required]);

/**
* React's use effect.
*/
useEffect(() => {
if (initialValue) {
setReactSelectValue({selectedOption: [initialValue]});
}
}, [initialValue]);

return (
<FormGroup className="rbf-group">
<Select
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/FormConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface DefaultInputProps {
formHooks: Hooks;
elementConfig: FormConfig;
valid?: {[key: string]: any};
defaultValues?: {[key: string]: any};
}

export interface FormHooks extends Omit<DefaultInputProps, 'elementConfig'> {
Expand Down

0 comments on commit 7dc255c

Please sign in to comment.