-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-formio): add support Choicesjs and React-select layout to …
…InputTags
- Loading branch information
Showing
20 changed files
with
486 additions
and
154 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
9 changes: 9 additions & 0 deletions
9
packages/react-formio/src/molecules/forms/input-tags/InputTags.interface.ts
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,9 @@ | ||
import type { InputHTMLAttributes } from "react"; | ||
|
||
import type { FormControlProps } from "../form-control/FormControl"; | ||
|
||
export interface InputTagsProps<Data = string> extends FormControlProps<Data[], InputHTMLAttributes<HTMLInputElement>> { | ||
layout?: "html5" | "react" | "choicesjs"; | ||
delimiter?: string; | ||
customProperties?: Record<string, any>; | ||
} |
64 changes: 21 additions & 43 deletions
64
packages/react-formio/src/molecules/forms/input-tags/InputTags.tsx
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,6 @@ | ||
import "../form-control/FormControl"; | ||
import "./components/ChoicesTags"; | ||
import "./components/ReactTags"; | ||
import "../input-text/InputText"; | ||
export * from "./InputTags"; | ||
export * from "./InputTags.interface"; |
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
72 changes: 72 additions & 0 deletions
72
packages/react-formio/src/molecules/forms/input-tags/components/ChoicesTags.tsx
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 Choices from "@formio/choices.js"; | ||
import { useEffect, useRef } from "react"; | ||
import { useDebouncedCallback } from "use-debounce"; | ||
|
||
import { registerComponent } from "../../../../registries/components"; | ||
import { cleanFormControlProps } from "../../form-control/FormControl"; | ||
import type { InputTagsProps } from "../InputTags.interface"; | ||
|
||
export function useChoiceTags<Data = string>(props: InputTagsProps<Data>) { | ||
const { value, onChange, name = "", delimiter, customProperties, ...otherProps } = props; | ||
const ref = useRef<HTMLInputElement | null>(null); | ||
const instanceRef = useRef<Choices | null>(null); | ||
|
||
const onAdd = useDebouncedCallback((add: Data) => { | ||
const values = ((value || []) as Data[]).concat(add); | ||
|
||
onChange?.(name, [...values]); | ||
}, 100); | ||
|
||
const onDelete = useDebouncedCallback((remove: Data) => { | ||
const values = (value || []).filter((v) => v !== remove); | ||
|
||
onChange?.(name, [...values]); | ||
}); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
const instance = new Choices(ref.current!, { | ||
duplicateItemsAllowed: false, | ||
...customProperties, | ||
delimiter, | ||
editItems: true, | ||
removeItemButton: true | ||
}); | ||
|
||
instance.setValue((value || []) as string[]); | ||
|
||
instanceRef.current = instance; | ||
|
||
instance.passedElement.element.addEventListener("addItem", (event: { detail: { value: unknown } }) => { | ||
onAdd(event.detail.value as Data); | ||
}); | ||
|
||
instance.passedElement.element.addEventListener("removeItem", (event: { detail: { value: unknown } }) => { | ||
onDelete(event.detail.value as Data); | ||
}); | ||
} | ||
|
||
return () => { | ||
if (instanceRef.current) { | ||
instanceRef.current.destroy(); | ||
} | ||
}; | ||
}, [delimiter]); | ||
Check warning on line 54 in packages/react-formio/src/molecules/forms/input-tags/components/ChoicesTags.tsx GitHub Actions / lint
|
||
|
||
return { | ||
otherProps: { | ||
...otherProps, | ||
name | ||
}, | ||
ref, | ||
instanceRef | ||
}; | ||
} | ||
|
||
export function ChoicesTags<Data = string>(props: InputTagsProps<Data>) { | ||
const { ref, otherProps } = useChoiceTags<Data>(props); | ||
|
||
return <input type='text' {...cleanFormControlProps(otherProps)} ref={ref} />; | ||
} | ||
|
||
registerComponent("InputTags.choicesjs", ChoicesTags); |
Oops, something went wrong.