Skip to content

Commit

Permalink
add default value check
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-hardman committed Oct 16, 2020
1 parent b2d99f0 commit 594febe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/components/Form/Form.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ export const applyThemeForm = () => (
{ label: "Brie", value: "brie" },
]}
/>

<FormTagSelector
name="tagging"
label="Doing a tagging"
className={styles.formInput}
defaultValue={["a", "b"]}
/>
</ThemedFormContents>
<ThemedFormControls loading />
<ThemedFormControls />
</Form>
);
8 changes: 5 additions & 3 deletions src/components/FormElements/FormElements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export const FormTagSelector: React.FC<TagSelectorProps> = ({
name,
inputClassName,
tagClassName,
defaultValue
defaultValue,
}) => {
const { setValue, register } = useFormContext("TagSelector");

Expand Down Expand Up @@ -351,8 +351,10 @@ export const FormTagSelector: React.FC<TagSelectorProps> = ({

useEffect(() => {
register(name);
setTags(defaultValue as string[]);
setValue(name, defaultValue as string[]);
if (Array.isArray(defaultValue)) {
setTags(defaultValue);
setValue(name, defaultValue);
}
}, []);

const Tags: React.FC = () => (
Expand Down

0 comments on commit 594febe

Please sign in to comment.