From ff8100262892a148a08cf6e9f184bf7a89f68250 Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Thu, 31 Aug 2023 11:50:28 -0400 Subject: [PATCH] feat: add clear button to relation widget --- .../common/autocomplete/Autocomplete.tsx | 69 ++++++++++++++----- .../src/widgets/relation/RelationControl.tsx | 5 ++ .../core/src/widgets/select/SelectControl.tsx | 4 +- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/packages/core/src/components/common/autocomplete/Autocomplete.tsx b/packages/core/src/components/common/autocomplete/Autocomplete.tsx index fa09cee4..e77d0440 100644 --- a/packages/core/src/components/common/autocomplete/Autocomplete.tsx +++ b/packages/core/src/components/common/autocomplete/Autocomplete.tsx @@ -1,9 +1,11 @@ import { Combobox, Transition } from '@headlessui/react'; import { Check as CheckIcon } from '@styled-icons/material/Check'; +import { Close as CloseIcon } from '@styled-icons/material/Close'; import { KeyboardArrowDown as KeyboardArrowDownIcon } from '@styled-icons/material/KeyboardArrowDown'; -import React, { forwardRef, Fragment, useCallback } from 'react'; +import React, { Fragment, forwardRef, useCallback } from 'react'; import classNames from '@staticcms/core/lib/util/classNames.util'; +import IconButton from '../button/IconButton'; import type { ReactNode, Ref } from 'react'; @@ -27,13 +29,23 @@ export interface AutocompleteProps { value: T | T[] | null; options: T[] | Option[]; disabled?: boolean; + required?: boolean; displayValue: (item: T | T[] | null) => string; onQuery: (query: string) => void; - onChange: AutocompleteChangeEventHandler; + onChange: AutocompleteChangeEventHandler; } const Autocomplete = function ( - { label, value, options, disabled, displayValue, onQuery, onChange }: AutocompleteProps, + { + label, + value, + options, + disabled, + required, + displayValue, + onQuery, + onChange, + }: AutocompleteProps, ref: Ref, ) { const handleChange = useCallback( @@ -56,6 +68,10 @@ const Autocomplete = function ( [onChange, value], ); + const clear = useCallback(() => { + onChange(undefined); + }, [onChange]); + return (
@@ -108,23 +124,44 @@ const Autocomplete = function ( displayValue={displayValue} onChange={event => onQuery(event.target.value)} /> - - + + + )} + aria-hidden="true" + /> + + {!required && !Array.isArray(value) ? ( + + + ) : null} +
> [onChange, uniqueOptionsByValue], ); + const isRequired = useMemo(() => field.required ?? true, [field.required]); + + console.log('field.required', field.required); + return ( > value={selectedValue} options={uniqueOptions} disabled={disabled} + required={isRequired} displayValue={item => { if (!item || Array.isArray(item)) { return ''; diff --git a/packages/core/src/widgets/select/SelectControl.tsx b/packages/core/src/widgets/select/SelectControl.tsx index 9778bd62..fb5da176 100644 --- a/packages/core/src/widgets/select/SelectControl.tsx +++ b/packages/core/src/widgets/select/SelectControl.tsx @@ -77,7 +77,9 @@ const SelectControl: FC