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: add StudioSearch component #14348

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ describe('TextEditor', () => {
renderTextEditor();

const search = '1';
const searchInput = screen.getByTestId('text-editor-search-default');
const searchInput = screen.getByRole('searchbox', {
name: textMock('text_editor.search_for_text'),
});
await user.type(searchInput, search);

expect(mockSetSearchParams).toHaveBeenCalledWith({ search });
Expand Down
18 changes: 5 additions & 13 deletions frontend/dashboard/pages/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useState } from 'react';
import classes from './Dashboard.module.css';
import type { ChangeEvent, KeyboardEvent } from 'react';
import { Textfield, Link } from '@digdir/designsystemet-react';
import { StudioButton } from '@studio/components';
import { XMarkIcon, PlusCircleIcon, PlusCircleFillIcon } from '@studio/icons';
import { Link } from '@digdir/designsystemet-react';
import { StudioSearch } from '@studio/components';
import { PlusCircleIcon, PlusCircleFillIcon } from '@studio/icons';
import { useDebounce } from '@studio/hooks';
import { CenterContainer } from '../../components/CenterContainer';
import { DataModelsReposList } from '../../components/DataModelsRepoList';
Expand Down Expand Up @@ -49,21 +49,13 @@ export const Dashboard = ({ user, organizations, disableDebounce }: DashboardPro
<div className={classes.createServiceContainer}>
<div className={classes.topBar}>
<div className={classes.searchFieldContainer}>
<Textfield
<StudioSearch
label={t('dashboard.search')}
value={searchText}
onChange={handleChangeSearch}
onKeyDown={handleKeyDown}
onClear={handleClearSearch}
/>
{searchText && (
<StudioButton
className={classes.clearSearchButton}
aria-label={t('dashboard.clear_search')}
onClick={handleClearSearch}
icon={<XMarkIcon />}
variant='tertiary'
/>
)}
</div>
<Link href={'/dashboard/' + selectedContext + '/new'} className={classes.newLink}>
<span>{t('dashboard.new_service')}</span>
Expand Down
2 changes: 1 addition & 1 deletion frontend/language/src/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"app_content_library.code_lists.no_content": "Dette biblioteket har ingen kodelister",
"app_content_library.code_lists.page_name": "Kodelister",
"app_content_library.code_lists.save_new_code_list": "Lagre",
"app_content_library.code_lists.search_placeholder": "Søk på kodelister",
"app_content_library.code_lists.search_label": "Søk på kodelister",
"app_content_library.code_lists.upload_code_list": "Last opp din egen kodeliste",
"app_content_library.images.info_box.description": "Du kan bruke bildene i biblioteket til å legge inn bilder i skjemaet. Du kan også laste opp et bilde med organisasjonens logo, og legge det som logobilde i innstillingene for appen.",
"app_content_library.images.info_box.title": "Hva kan du bruke bildene til?",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { type ForwardedRef } from 'react';
import { render, screen } from '@testing-library/react';
import type { StudioSearchProps } from './StudioSearch';
import { StudioSearch } from './StudioSearch';
import { testRefForwarding } from '../../test-utils/testRefForwarding';
import { testRootClassNameAppending } from '../../test-utils/testRootClassNameAppending';
import { testCustomAttributes } from '../../test-utils/testCustomAttributes';

describe('StudioSearch', () => {
it('should support forwarding the ref', () => {
testRefForwarding<HTMLInputElement>((ref) => renderTestSearch({}, ref), getSearchBox);
});

it('should append classname to root', () => {
testRootClassNameAppending((className) => renderTestSearch({ className }));
});

it('should allow custom attributes', () => {
testCustomAttributes(renderTestSearch, getSearchBox);
});

it('should render search field with label name when provided', () => {
const label = 'Search for something';
renderTestSearch({ label });
const search = screen.getByRole('searchbox', { name: label });
expect(search).toBeInTheDocument();
});
TomasEng marked this conversation as resolved.
Show resolved Hide resolved

it('should render search field with label name when ID is set through props', () => {
const label = 'Search for something';
const id = 'searchId';
renderTestSearch({ label, id });
const search = screen.getByRole('searchbox', { name: label });
expect(search).toBeInTheDocument();
});
});

const renderTestSearch = (
props: Partial<StudioSearchProps> = {},
ref?: ForwardedRef<HTMLInputElement>,
) => {
return render(<StudioSearch {...props} ref={ref} />);
};

function getSearchBox(): HTMLInputElement {
return screen.getByRole('searchbox');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { forwardRef, useId } from 'react';
import { Label, Search, type SearchProps } from '@digdir/designsystemet-react';
import type { WithoutAsChild } from '../../types/WithoutAsChild';

export type StudioSearchProps = WithoutAsChild<SearchProps>;

const StudioSearch = forwardRef<HTMLInputElement, StudioSearchProps>(
({ size = 'sm', label, id, ...rest }, ref) => {
const generatedId = useId();
const searchId = id ?? generatedId;
const showLabel = !!label;

return (
<>
{showLabel && <Label htmlFor={searchId}>{label}</Label>}
<Search {...rest} id={searchId} size={size} ref={ref} />
</>
);
},
);

StudioSearch.displayName = 'StudioSearch';

export { StudioSearch };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { StudioSearch } from './StudioSearch';
1 change: 1 addition & 0 deletions frontend/libs/studio-components/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export * from './StudioStatusRadioGroup';
export * from './StudioRecommendedNextAction';
export * from './StudioRedirectBox';
export * from './StudioResizableLayout';
export * from './StudioSearch';
export * from './StudioSectionHeader';
export * from './StudioSpinner';
export * from './StudioSwitch';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.actionsBar {
display: grid;
grid-template-columns: auto auto auto;
align-items: center;
align-items: flex-end;
gap: var(--fds-spacing-3);
justify-content: flex-start;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const codeListName2 = 'codeListName2';
describe('CodeListsActionsBar', () => {
afterEach(jest.clearAllMocks);

it('renders the search field with placeholder text', () => {
it('renders the search field with label', () => {
renderCodeListsActionsBar();
const searchFieldPlaceHolderText = screen.getByPlaceholderText(
textMock('app_content_library.code_lists.search_placeholder'),
);
expect(searchFieldPlaceHolderText).toBeInTheDocument();
const searchFieldLabelText = screen.getByRole('searchbox', {
name: textMock('app_content_library.code_lists.search_label'),
});
expect(searchFieldLabelText).toBeInTheDocument();
});

it('renders the file uploader button', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Search } from '@digdir/designsystemet-react';
import { StudioFileUploader } from '@studio/components';
import { StudioFileUploader, StudioSearch } from '@studio/components';
import classes from './CodeListsActionsBar.module.css';
import { useTranslation } from 'react-i18next';
import type { CodeListWithMetadata } from '../CodeListPage';
Expand Down Expand Up @@ -36,10 +35,9 @@ export function CodeListsActionsBar({

return (
<div className={classes.actionsBar}>
<Search
<StudioSearch
className={classes.searchField}
size='sm'
placeholder={t('app_content_library.code_lists.search_placeholder')}
label={t('app_content_library.code_lists.search_label')}
/>
<CreateNewCodeListModal onUpdateCodeList={onUpdateCodeList} codeListNames={codeListNames} />
<StudioFileUploader
Expand Down
11 changes: 6 additions & 5 deletions frontend/packages/text-editor/src/TextEditor.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@

.textEditorTopRow {
border-bottom: 1px solid #bcc7cc;
padding: 2rem;
padding: var(--fds-spacing-6);
display: flex;
align-items: center;
justify-content: space-between;
}

.filterAndSearch {
display: flex;
flex-direction: row;
align-items: flex-end;
gap: 1rem;
align-items: center;
gap: var(--fds-spacing-4);
}

.sortAlphabetically {
gap: 1rem;
gap: var(--fds-spacing-4);
display: flex;
align-items: center;
}

.textEditorBody {
margin-left: 2rem;
margin-left: var(--fds-spacing-6);
overflow: auto;
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/text-editor/src/TextEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ describe('TextEditor', () => {
renderTextEditor({});

const textEntries = screen.getAllByRole('textbox');
expect(textEntries[1]).toHaveValue(textValue1);
expect(textEntries[0]).toHaveValue(textValue1);

const sortAlphabeticallyButton = screen.getByText(textMock('text_editor.sort_alphabetically'));
await user.click(sortAlphabeticallyButton);

const sortedTranslations = screen.getAllByRole('textbox');
expect(sortedTranslations[1]).toHaveValue(textValue2);
expect(sortedTranslations[0]).toHaveValue(textValue2);
TomasEng marked this conversation as resolved.
Show resolved Hide resolved
});

it('signals correctly when a translation is changed', async () => {
Expand Down
9 changes: 5 additions & 4 deletions frontend/packages/text-editor/src/TextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import React, { useEffect, useMemo, useRef, useState } from 'react';
import classes from './TextEditor.module.css';
import type { LangCode, TextResourceEntryDeletion, TextResourceIdMutation } from './types';
import type { UpsertTextResourceMutation } from 'app-shared/hooks/mutations/useUpsertTextResourceMutation';
import { SearchField } from '@altinn/altinn-design-system';
TomasEng marked this conversation as resolved.
Show resolved Hide resolved
import { Chip } from '@digdir/designsystemet-react';
import { ArrowsUpDownIcon } from '@studio/icons';
import { StudioButton } from '@studio/components';
import { StudioButton, StudioSearch } from '@studio/components';
import { RightMenu } from './RightMenu';
import { getRandNumber, mapResourceFilesToTableRows } from './utils';
import { defaultLangCode } from './constants';
Expand Down Expand Up @@ -88,6 +87,8 @@ export const TextEditor = ({
};
const handleSearchChange = (event: any) => setSearchQuery(event.target.value);

const handleClearSearch = () => setSearchQuery('');

return (
<div className={classes.textEditor}>
<div className={classes.textEditorMain}>
Expand All @@ -108,11 +109,11 @@ export const TextEditor = ({
}
</Chip.Toggle>
<div>
<SearchField
id='text-editor-search'
<StudioSearch
label={t('text_editor.search_for_text')}
value={searchQuery}
onChange={handleSearchChange}
onClear={handleClearSearch}
/>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/testing/playwright/pages/DashboardPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class DashboardPage extends BasePage {
}

public async typeInSearchField(word: string): Promise<void> {
await this.page.getByLabel(this.textMock('dashboard.search')).fill(word);
await this.page.getByRole('searchbox').fill(word);
}

public async clickOnTestAppGiteaButton(appName: string): Promise<void> {
Expand Down
Loading