PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / DonationForms / resources / registrars / templates / fields / MultiSelect.tsx

MultiSelect.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonationForms/resources/registrars/templates/fields/MultiSelect.tsx

85 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import Select from 'react-select';
2 import {Controller} from 'react-hook-form';
3
4 import {MultiSelectProps} from '@givewp/forms/propTypes';
5 import styles from '../styles.module.scss';
6
7 export default function MultiSelect({
8 Label,
9 ErrorMessage,
10 fieldError,
11 description,
12 fieldType,
13 options,
14 inputProps,
15 }: MultiSelectProps) {
16 const {useFormContext, useWatch} = window.givewp.form.hooks;
17 const FieldDescription = window.givewp.form.templates.layouts.fieldDescription;
18 const {name} = inputProps;
19 const {control} = useFormContext();
20 const fieldValue = useWatch({name: inputProps.name});
21
22 return (
23 <fieldset
24 className={styles.multiSelectField}
25 {...(fieldType === 'checkbox'
26 ? {
27 role: 'group',
28 'aria-required': inputProps.required,
29 'aria-invalid': !!fieldError,
30 'aria-describedby': `givewp-field-error-${inputProps.name}`,
31 }
32 : {})}
33 >
34 <legend>
35 <Label />
36 {description && <FieldDescription description={description} />}
37 </legend>
38 {fieldType === 'dropdown' ? (
39 <Controller
40 name={name}
41 control={control}
42 render={({field: {onChange, value: fieldValue, ref}, fieldState: {invalid}}) => (
43 <Select
44 ref={ref}
45 options={options}
46 defaultValue={
47 fieldValue
48 ? options.filter(({value: optionValue}) => fieldValue?.includes(optionValue))
49 : null
50 }
51 isMulti={true}
52 isClearable={true}
53 isSearchable={false}
54 className="givewp-fields-multiSelect__input"
55 classNamePrefix="givewp-fields-multiSelect"
56 onChange={(newValue) => onChange(newValue.map(({value}) => value))}
57 aria-invalid={invalid ? 'true' : 'false'}
58 />
59 )}
60 />
61 ) : (
62 <div className="givewp-fields-checkbox__options">
63 {options.map(({value: optionValue, label}, index) => {
64 const optionId = inputProps.name + '_' + index;
65 return (
66 <div key={index} className="givewp-fields-checkbox__option-container">
67 <input
68 type="checkbox"
69 id={optionId}
70 value={optionValue}
71 {...inputProps}
72 checked={fieldValue?.includes(optionValue)}
73 />
74 <label htmlFor={optionId}>{label}</label>
75 </div>
76 );
77 })}
78 </div>
79 )}
80
81 <ErrorMessage />
82 </fieldset>
83 );
84 }
85