Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: table search can detect text values on change #888

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TableSearchField } from './TableSearchField';
import { TableSearchOption } from './TableSearchOption';
import type { TableSearchProps } from './TableSearchProps';

export const TableSearch = ({ children, testId = 'table-search', onSubmit }: TableSearchProps) => {
export const TableSearch = ({ children, testId = 'table-search', onSubmit, onTextChange }: TableSearchProps) => {
const [optionValue, setOptionValue] = useState<string>();
const searchFieldRef = useRef<TextInputRef>(null);

Expand All @@ -21,7 +21,12 @@ export const TableSearch = ({ children, testId = 'table-search', onSubmit }: Tab
};

return (
<TableSearchProvider searchFieldRef={searchFieldRef} onOptionChange={changeOption} onSubmit={submit}>
<TableSearchProvider
searchFieldRef={searchFieldRef}
onOptionChange={changeOption}
onSubmit={submit}
onTextChange={onTextChange}
>
<HStack width="100%" alignVertical="center" spacing={4} data-testid={testId}>
{children}
</HStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,43 @@ type TableSearchContextValue = {
searchFieldRef: RefObject<TextInputRef> | null;
onSubmit?: (value: string) => void;
onOptionChange?: (value?: string) => void;
onTextChange?: ({ value, prevent }: { value: string; prevent: () => void }) => void;
};

const TableSearchContext = createContext<TableSearchContextValue>({
searchFieldRef: null,
onSubmit: () => {},
onOptionChange: () => {},
onTextChange: () => {},
});

type TableSearchProviderProps = {
children: ReactElement | ReactElement[];
searchFieldRef: RefObject<TextInputRef> | null;
onSubmit?: (value: string) => void;
onOptionChange?: (value?: string) => void;
onTextChange?: ({ value, prevent }: { value: string; prevent: () => void }) => void;
};

export const TableSearchProvider: FC<TableSearchProviderProps> = ({
children,
searchFieldRef,
onOptionChange,
onTextChange,
onSubmit,
}) => (
<TableSearchContext.Provider value={{ searchFieldRef, onOptionChange, onSubmit }}>
<TableSearchContext.Provider value={{ searchFieldRef, onOptionChange, onSubmit, onTextChange }}>
{children}
</TableSearchContext.Provider>
);

export const useTableSearch = () => {
const { searchFieldRef, onOptionChange, onSubmit } = useContext(TableSearchContext);
const { searchFieldRef, onOptionChange, onSubmit, onTextChange } = useContext(TableSearchContext);

return {
searchFieldRef,
onOptionChange,
onTextChange,
onSubmit,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { withTableSearchFieldVariation } from './TableSearchFieldProps';

export const TableSearchField = withTableSearchFieldVariation(
({ maxWidth, testId = 'table-search-field', ...restProps }) => {
const { searchFieldRef, onSubmit } = useTableSearch();
const { searchFieldRef, onSubmit, onTextChange } = useTableSearch();

return (
<Box width="100%" maxWidth={maxWidth} data-testid={testId}>
Expand All @@ -17,6 +17,7 @@ export const TableSearchField = withTableSearchFieldVariation(
clearable={true}
renderStart={() => <Icon.Search.Thin size={20} fill="onView2" />}
onSubmit={onSubmit}
onValueChange={onTextChange}
{...restProps}
/>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type TableSearchProps = {
children: ReactElement | ReactElement[];
testId?: string;
onSubmit?: (fieldValue: string, optionValue?: string) => void;
onTextChange?: ({ value, prevent }: { value: string; prevent: () => void }) => void;
};

export const withTableSearchVariation = withVariation<TableSearchProps>('TableSearch')();
Loading