|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useCallback, useMemo, useState } from 'react'; |
2 | 2 | import { |
3 | 3 | Checkbox, |
4 | 4 | CheckboxProps, |
@@ -35,7 +35,9 @@ type CheckboxesProps = { |
35 | 35 | filterDOMProps.register('autoValue'); |
36 | 36 |
|
37 | 37 | function RenderCheckboxes(props: CheckboxesProps) { |
38 | | - const Group = props.fieldType === Array ? Checkbox : Radio; |
| 38 | + const Group = useMemo(() => (props.fieldType === Array ? Checkbox : Radio), [ |
| 39 | + props, |
| 40 | + ]); |
39 | 41 |
|
40 | 42 | return ( |
41 | 43 | <div {...filterDOMProps(props)}> |
@@ -91,57 +93,70 @@ function isSelectOptionObject( |
91 | 93 | } |
92 | 94 |
|
93 | 95 | function RenderSelect(props: SelectInputProps) { |
94 | | - const selectDefault = props.fieldType === Array ? [] : props.placeholder; |
95 | | - |
96 | 96 | const [expanded, setExpanded] = useState<boolean>(false); |
97 | | - const [selected, setSelected] = useState<string | string[]>(selectDefault); |
98 | | - |
99 | | - const handleSelect = ( |
100 | | - event: React.MouseEvent | React.ChangeEvent, |
101 | | - selection: string | SelectOptionObject |
102 | | - ) => { |
103 | | - const items = parseInput(selection, props.fieldType); |
104 | | - props.onChange(items); |
105 | | - setSelected(items); |
106 | | - setExpanded(false); |
107 | | - }; |
108 | | - |
109 | | - const parseInput = ( |
110 | | - selection: string | SelectOptionObject, |
111 | | - fieldType: typeof Array | any |
112 | | - ): string | string[] => { |
113 | | - const parsedSelection = isSelectOptionObject(selection) |
114 | | - ? selection.toString() |
115 | | - : selection; |
116 | | - |
117 | | - if (fieldType !== Array) { |
118 | | - return parsedSelection !== '' ? parsedSelection : ''; |
119 | | - } |
120 | | - |
121 | | - if (Array.isArray(selected)) { |
122 | | - if (selected.includes(parsedSelection)) { |
123 | | - return selected.filter((s) => s !== parsedSelection); |
| 97 | + const [selected, setSelected] = useState<string | string[]>([]); |
| 98 | + |
| 99 | + const parseInput = useCallback( |
| 100 | + ( |
| 101 | + selection: string | SelectOptionObject, |
| 102 | + fieldType: typeof Array | any |
| 103 | + ): string | string[] => { |
| 104 | + const parsedSelection = isSelectOptionObject(selection) |
| 105 | + ? selection.toString() |
| 106 | + : selection; |
| 107 | + |
| 108 | + if (fieldType !== Array) { |
| 109 | + return parsedSelection !== '' ? parsedSelection : ''; |
| 110 | + } |
| 111 | + |
| 112 | + if (Array.isArray(selected)) { |
| 113 | + if (selected.includes(parsedSelection)) { |
| 114 | + return selected.filter((s) => s !== parsedSelection); |
| 115 | + } |
| 116 | + return [parsedSelection, ...selected]; |
| 117 | + } |
| 118 | + return [parsedSelection, selected]; |
| 119 | + }, |
| 120 | + [selected] |
| 121 | + ); |
| 122 | + |
| 123 | + const handleSelect = useCallback( |
| 124 | + ( |
| 125 | + event: React.MouseEvent | React.ChangeEvent, |
| 126 | + selection: string | SelectOptionObject |
| 127 | + ) => { |
| 128 | + if (selection === props.placeholder) { |
| 129 | + setSelected([]); |
| 130 | + setExpanded(false); |
| 131 | + } else { |
| 132 | + const items = parseInput(selection, props.fieldType); |
| 133 | + props.onChange(items); |
| 134 | + setSelected(items); |
| 135 | + setExpanded(false); |
124 | 136 | } |
125 | | - return [parsedSelection, ...selected]; |
126 | | - } |
127 | | - return [parsedSelection, selected]; |
128 | | - }; |
| 137 | + }, |
| 138 | + [parseInput, props] |
| 139 | + ); |
129 | 140 |
|
130 | | - const selectedOptions = props.allowedValues!.map((value) => ( |
131 | | - <SelectOption key={value} value={value}> |
132 | | - {props.transform ? props.transform(value) : value} |
133 | | - </SelectOption> |
134 | | - )); |
| 141 | + const selectedOptions = useMemo( |
| 142 | + () => |
| 143 | + props.allowedValues!.map((value) => ( |
| 144 | + <SelectOption key={value} value={value}> |
| 145 | + {props.transform ? props.transform(value) : value} |
| 146 | + </SelectOption> |
| 147 | + )), |
| 148 | + [props] |
| 149 | + ); |
135 | 150 |
|
136 | 151 | if (props.placeholder) |
137 | 152 | selectedOptions.unshift( |
138 | 153 | <SelectOption |
139 | 154 | key={props.allowedValues!.length} |
140 | | - isDisabled |
141 | 155 | isPlaceholder |
142 | 156 | value={props.placeholder} |
143 | 157 | /> |
144 | 158 | ); |
| 159 | + |
145 | 160 | return wrapField( |
146 | 161 | props, |
147 | 162 | <Select |
|
0 commit comments