|
1 |
| -import Choices from "@formio/choices.js"; |
2 |
| -import uniq from "lodash/uniq"; |
3 |
| -import { useEffect, useRef } from "react"; |
| 1 | +import { ComponentType } from "react"; |
4 | 2 |
|
5 |
| -import { registerComponent } from "../../../registries/components"; |
6 |
| -import { FormControl, FormControlProps } from "../form-control/FormControl"; |
| 3 | +import { getComponent, registerComponent } from "../../../registries/components"; |
| 4 | +import { type FormControl as DefaultFormControl } from "../form-control/FormControl"; |
| 5 | +import type { InputTagsProps } from "./InputTags.interface"; |
7 | 6 |
|
8 |
| -export interface InputTagsProps<T = any> extends Omit<FormControlProps, "description" | "prefix" | "suffix"> { |
9 |
| - value?: T; |
10 |
| - onChange?: (name: string, value: T) => void; |
11 |
| - placeholder?: string; |
12 |
| - |
13 |
| - [key: string]: any; |
14 |
| -} |
15 |
| - |
16 |
| -export function InputTags({ name, value = [], label, onChange, required, description, prefix, suffix, ...props }: InputTagsProps) { |
17 |
| - const ref: any = useRef(); |
18 |
| - |
19 |
| - useEffect(() => { |
20 |
| - const instance = new Choices(ref.current, { |
21 |
| - delimiter: ",", |
22 |
| - editItems: true, |
23 |
| - removeItemButton: true |
24 |
| - }); |
25 |
| - |
26 |
| - instance.setValue([].concat(value, [])); |
27 |
| - |
28 |
| - instance.passedElement.element.addEventListener("addItem", (event: any) => { |
29 |
| - onChange && onChange(name, uniq(value.concat(event.detail.value))); |
30 |
| - }); |
31 |
| - |
32 |
| - instance.passedElement.element.addEventListener("removeItem", (event: any) => { |
33 |
| - onChange && |
34 |
| - onChange( |
35 |
| - name, |
36 |
| - value.filter((v: string) => v !== event.detail.value) |
37 |
| - ); |
38 |
| - }); |
39 |
| - |
40 |
| - return () => { |
41 |
| - instance.destroy(); |
42 |
| - }; |
43 |
| - }, []); |
| 7 | +export function InputTags<Data = string>(props: InputTagsProps) { |
| 8 | + const { name, id = name, label, required, description, before, after, size, className, layout = "choicesjs", ...otherProps } = props; |
44 | 9 |
|
| 10 | + const FormControl = getComponent<typeof DefaultFormControl>("FormControl"); |
| 11 | + const Component = getComponent<ComponentType<InputTagsProps<Data>>>([`InputTags.${layout}`, "Input"]); |
| 12 | + console.log("VALUE", props.value); |
45 | 13 | return (
|
46 |
| - <FormControl name={name} label={label} required={required} description={description} prefix={prefix} suffix={suffix}> |
47 |
| - <input ref={ref} type='text' {...props} id={name} required={required} /> |
| 14 | + <FormControl |
| 15 | + id={id} |
| 16 | + name={name} |
| 17 | + label={label} |
| 18 | + required={required} |
| 19 | + description={description} |
| 20 | + before={before} |
| 21 | + after={after} |
| 22 | + size={size} |
| 23 | + className={className} |
| 24 | + > |
| 25 | + <Component {...(otherProps as any)} id={id} name={name} required={required} /> |
48 | 26 | </FormControl>
|
49 | 27 | );
|
50 | 28 | }
|
|
0 commit comments