From f38558408aa85a880c679b955c6c8472fc19d969 Mon Sep 17 00:00:00 2001 From: melloware Date: Mon, 21 Nov 2022 17:03:47 -0500 Subject: [PATCH] Fix #3685: TreeSelect inputRef and validation example --- components/doc/autocomplete/validationdoc.js | 10 +- components/doc/calendar/validationdoc.js | 10 +- components/doc/checkbox/validationdoc.js | 10 +- components/doc/colorpicker/validationdoc.js | 10 +- components/doc/dropdown/validationdoc.js | 10 +- components/doc/inputmask/validationdoc.js | 10 +- components/doc/inputnumber/validationdoc.js | 8 +- components/doc/inputtext/validationdoc.js | 10 +- components/doc/inputtextarea/validationdoc.js | 10 +- .../doc/multistatecheckbox/validationdoc.js | 8 +- components/doc/password/validationdoc.js | 10 +- components/doc/radiobutton/validationdoc.js | 10 +- components/doc/treeselect/validationdoc.js | 177 ++++++++++++++++++ .../doc/tristatecheckbox/validationdoc.js | 10 +- components/lib/treeselect/TreeSelect.js | 72 +++---- components/lib/treeselect/treeselect.d.ts | 59 +++--- pages/treeselect/index.js | 12 +- 17 files changed, 317 insertions(+), 129 deletions(-) create mode 100644 components/doc/treeselect/validationdoc.js diff --git a/components/doc/autocomplete/validationdoc.js b/components/doc/autocomplete/validationdoc.js index bdd0c6442b..8815d8cd9c 100644 --- a/components/doc/autocomplete/validationdoc.js +++ b/components/doc/autocomplete/validationdoc.js @@ -40,9 +40,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { AutoComplete } from "primereact/autocomplete"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -89,9 +89,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { AutoComplete } from "primereact/autocomplete"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -108,7 +108,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/calendar/validationdoc.js b/components/doc/calendar/validationdoc.js index 1209a6ff19..c97ac7bb46 100644 --- a/components/doc/calendar/validationdoc.js +++ b/components/doc/calendar/validationdoc.js @@ -35,9 +35,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Calendar } from "primereact/calendar"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -79,9 +79,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Calendar } from "primereact/calendar"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -93,7 +93,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/checkbox/validationdoc.js b/components/doc/checkbox/validationdoc.js index 0e4d84f9a4..130cf2cd96 100644 --- a/components/doc/checkbox/validationdoc.js +++ b/components/doc/checkbox/validationdoc.js @@ -35,9 +35,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Checkbox } from "primereact/checkbox"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -79,9 +79,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Checkbox } from "primereact/checkbox"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -93,7 +93,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/colorpicker/validationdoc.js b/components/doc/colorpicker/validationdoc.js index 667c173b37..bcdc520b69 100644 --- a/components/doc/colorpicker/validationdoc.js +++ b/components/doc/colorpicker/validationdoc.js @@ -35,9 +35,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { ColorPicker } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -79,9 +79,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { ColorPicker } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -93,7 +93,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/dropdown/validationdoc.js b/components/doc/dropdown/validationdoc.js index d10945801c..9c8a3812b1 100644 --- a/components/doc/dropdown/validationdoc.js +++ b/components/doc/dropdown/validationdoc.js @@ -50,9 +50,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Dropdown } from "primereact/dropdown"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -94,9 +94,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Dropdown } from "primereact/dropdown"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -108,7 +108,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/inputmask/validationdoc.js b/components/doc/inputmask/validationdoc.js index 96348ce055..a9289f24a2 100644 --- a/components/doc/inputmask/validationdoc.js +++ b/components/doc/inputmask/validationdoc.js @@ -35,9 +35,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputMask } from "primereact/inputmask"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -79,9 +79,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputMask } from "primereact/inputmask"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -93,7 +93,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/inputnumber/validationdoc.js b/components/doc/inputnumber/validationdoc.js index a633c77ec7..15d2695eca 100644 --- a/components/doc/inputnumber/validationdoc.js +++ b/components/doc/inputnumber/validationdoc.js @@ -68,9 +68,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputNumber } from "primereact/inputnumber"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -142,9 +142,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputNumber } from "primereact/inputnumber"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); diff --git a/components/doc/inputtext/validationdoc.js b/components/doc/inputtext/validationdoc.js index c38e08ff75..b0abc17cfc 100644 --- a/components/doc/inputtext/validationdoc.js +++ b/components/doc/inputtext/validationdoc.js @@ -35,9 +35,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputText } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -79,9 +79,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputText } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -93,7 +93,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/inputtextarea/validationdoc.js b/components/doc/inputtextarea/validationdoc.js index adaf9b867d..285d22b0db 100644 --- a/components/doc/inputtextarea/validationdoc.js +++ b/components/doc/inputtextarea/validationdoc.js @@ -36,9 +36,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputText } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -81,9 +81,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { InputText } from "primereact/inputtext"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -96,7 +96,7 @@ export default function InvalidDemo() { form.reset(); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/multistatecheckbox/validationdoc.js b/components/doc/multistatecheckbox/validationdoc.js index f73f737480..d137fe999c 100644 --- a/components/doc/multistatecheckbox/validationdoc.js +++ b/components/doc/multistatecheckbox/validationdoc.js @@ -41,9 +41,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { MultiStateCheckbox } from "primereact/multistatecheckbox"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -91,9 +91,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { MultiStateCheckbox } from "primereact/multistatecheckbox"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); diff --git a/components/doc/password/validationdoc.js b/components/doc/password/validationdoc.js index 0ab337b6ee..69f0e98fb2 100644 --- a/components/doc/password/validationdoc.js +++ b/components/doc/password/validationdoc.js @@ -36,9 +36,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Password } from "primereact/password"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -81,9 +81,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { Password } from "primereact/password"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -96,7 +96,7 @@ export default function InvalidDemo() { form.reset(); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/radiobutton/validationdoc.js b/components/doc/radiobutton/validationdoc.js index 14e4591134..fde33499fb 100644 --- a/components/doc/radiobutton/validationdoc.js +++ b/components/doc/radiobutton/validationdoc.js @@ -63,9 +63,9 @@ export function ValidationDoc(props) { javascript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { RadioButton } from "primereact/radiobutton"; -import { classNames } from '../../lib/utils/Utils'; export default function ValidationDemo() { const [formData, setFormData] = useState({}); @@ -131,9 +131,9 @@ export default function ValidationDemo() { typescript: ` import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; -import { Button } from '../../lib/button/Button'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; import { RadioButton } from "primereact/radiobutton"; -import { classNames } from '../../lib/utils/Utils'; export default function InvalidDemo() { const [formData, setFormData] = useState({}); @@ -145,7 +145,7 @@ export default function InvalidDemo() { setFormData(data); }; - const getFormErrorMessage = (name) => { + const getFormErrorMessage = (name: string) => { return errors[name] && {errors[name].message} }; diff --git a/components/doc/treeselect/validationdoc.js b/components/doc/treeselect/validationdoc.js new file mode 100644 index 0000000000..f3c67d66d0 --- /dev/null +++ b/components/doc/treeselect/validationdoc.js @@ -0,0 +1,177 @@ +import { useEffect, useState } from 'react'; +import { Controller, useForm } from 'react-hook-form'; +import { NodeService } from '../../../service/NodeService'; +import { Button } from '../../lib/button/Button'; +import { TreeSelect } from '../../lib/treeselect/TreeSelect'; +import { classNames } from '../../lib/utils/Utils'; +import { DocSectionCode } from '../common/docsectioncode'; +import { DocSectionText } from '../common/docsectiontext'; + +export function ValidationDoc(props) { + const [formData, setFormData] = useState({}); + const [nodes, setNodes] = useState(null); + const defaultValues = { node: null }; + const form = useForm({ defaultValues }); + const errors = form.formState.errors; + const nodeService = new NodeService(); + + useEffect(() => { + nodeService.getTreeNodes().then((data) => setNodes(data)); + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const onSubmit = (data) => { + setFormData(data); + }; + + const getFormErrorMessage = (name) => { + return errors[name] && {errors[name].message}; + }; + + const code = { + basic: ` + ( + <> + + + {getFormErrorMessage(field.name)} + + )} +/> + `, + javascript: ` +import { useState } from 'react'; +import { Controller, useForm } from 'react-hook-form'; +import { Button } from 'primereact/button'; +import { classNames } from 'primereact/utils'; +import { TreeSelect } from "primereact/treeselect"; + +export default function ValidationDemo() { + const [formData, setFormData] = useState({}); + const [nodes, setNodes] = useState(null); + const defaultValues = { node: null }; + const form = useForm({ defaultValues }); + const errors = form.formState.errors; + const nodeService = new NodeService(); + + useEffect(() => { + nodeService.getTreeNodes().then((data) => setNodes(data)); + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const onSubmit = (data) => { + setFormData(data); + }; + + const getFormErrorMessage = (name) => { + return errors[name] && {errors[name].message} + }; + + return ( +
+
+ ( + <> + + + {getFormErrorMessage(field.name)} + + )} + /> +
+