@box/blueprint-web 12.2.0 → 12.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,6 +79,7 @@ const RootInner = ({
79
79
  clearButtonAriaLabel,
80
80
  endComboboxIcon,
81
81
  idForLabel,
82
+ displaySingleSelectionAsChip = multiselect,
82
83
  ...comboboxProps
83
84
  } = rest;
84
85
  const isInput = useMemo(() => as === 'input', [as]);
@@ -152,6 +153,11 @@ const RootInner = ({
152
153
  const {
153
154
  setValue: setSelectedValue
154
155
  } = selectStore;
156
+ useEffect(() => {
157
+ if (inputValue.trim() === '' && !multiselect && selectedValue && !displaySingleSelectionAsChip && !clearOnBlur) {
158
+ setSelectedValue('');
159
+ }
160
+ }, [inputValue, multiselect, setSelectedValue, selectedValue, displaySingleSelectionAsChip, clearOnBlur]);
155
161
  const resetSelectedValue = useCallback(() => {
156
162
  setSelectedValue(multiselect ? [] : '');
157
163
  }, [multiselect, setSelectedValue]);
@@ -165,15 +171,28 @@ const RootInner = ({
165
171
  const focusInput = useCallback(() => {
166
172
  inputRef.current?.focus();
167
173
  }, []);
174
+ const onOptionClick = useCallback(displayedValue => {
175
+ // Check if inputValue is different from displayedValue,
176
+ // if so, update inputValue to displayedValue
177
+ // Othwerwise, clicking the option will do nothing.
178
+ if (inputValue.trim() !== displayedValue && !displaySingleSelectionAsChip) {
179
+ setInputValue(displayedValue);
180
+ }
181
+ }, [inputValue, setInputValue, displaySingleSelectionAsChip]);
182
+ const createClickHandler = useCallback(displayedValue => () => {
183
+ onOptionClick(displayedValue);
184
+ }, [onOptionClick]);
168
185
  const renderSelectItemOption = useCallback(option => {
169
186
  const value = getOptionValue(option);
187
+ const displayedValue = displayValue ? displayValue(option) : value;
170
188
  return jsxs(OptionWithIndicator, {
171
189
  ...(typeof option === 'object' ? option : {}),
172
190
  disabled: typeof option === 'object' && option.disabled,
191
+ onClick: createClickHandler(displayedValue),
173
192
  value: value,
174
193
  children: [jsx(Indicator, {}), displayValue ? displayValue(option) : value]
175
194
  });
176
- }, [displayValue]);
195
+ }, [displayValue, createClickHandler]);
177
196
  const renderOptionFn = renderOption || renderSelectItemOption;
178
197
  // eslint-disable-next-line react-hooks/exhaustive-deps
179
198
  const selectedValueMemoized = useMemo(() => selectedValue, [JSON.stringify(selectedValue)]);
@@ -182,7 +201,8 @@ const RootInner = ({
182
201
  if (Array.isArray(selectedValueMemoized) || showSingleSelectChip) {
183
202
  setInputValue('');
184
203
  } else if (selectedValueMemoized) {
185
- setInputValue(getDisplayValueFromOptionValue(selectedValueMemoized, options, displayValue));
204
+ const selectedDisplayValue = getDisplayValueFromOptionValue(selectedValueMemoized, options, displayValue);
205
+ setInputValue(selectedDisplayValue);
186
206
  // Also focus input for single-select variant
187
207
  focusInput();
188
208
  }
@@ -259,7 +279,7 @@ const RootInner = ({
259
279
  const reference = useForkRef(inputRef, ref);
260
280
  const showChipsGroup = Array.isArray(selectedValue) && selectedValue.length > 0;
261
281
  const showComboboxCancelButton = clearButtonAriaLabel && (inputValue.length > 0 || (Array.isArray(selectedValue) ? selectedValue.length > 0 : !!selectedValue));
262
- const showSingleSelectChip = showComboboxCancelButton && !Array.isArray(selectedValue) && !!selectedValue;
282
+ const showSingleSelectChip = displaySingleSelectionAsChip && !Array.isArray(selectedValue) && !!selectedValue;
263
283
  const Label = useLabelable(label, comboboxId);
264
284
  const inlineErrorId = useUniqueId('inline-error-');
265
285
  const ariaDescribedBy = clsx(rest['aria-describedby'], {
@@ -217,6 +217,13 @@ export interface ComboboxBaseProps<Multiple extends boolean, FreeInput extends b
217
217
  * @default true
218
218
  */
219
219
  hideOnEscape?: ComboboxPopoverProps['hideOnEscape'];
220
+ /**
221
+ * If `true`, displays the single selected value in the input field as a chip.
222
+ * This is independent of the clear button visibility.
223
+ *
224
+ * @default false
225
+ */
226
+ displaySingleSelectionAsChip?: boolean;
220
227
  }
221
228
  export type ComboboxTextArea = Pick<TextAreaProps, 'maxRows' | 'minRows' | 'maxLength' | 'aria-describedby'> & {
222
229
  as: 'textarea';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@box/blueprint-web",
3
- "version": "12.2.0",
3
+ "version": "12.3.0",
4
4
  "type": "module",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "publishConfig": {