Skip to content

Commit

Permalink
fix: add onChange and inputValue for bookmarksection textfield (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanes authored Jan 9, 2025
1 parent d47277c commit e07105e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
1 change: 0 additions & 1 deletion lib/components/Autocomplete/Autocomplete.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ export const ScopesResultsAndSuggestions: Story = {
},
items: [
...ScopesAndMixedResults.args.items,

{
id: '2a',
groupId: '2',
Expand Down
36 changes: 21 additions & 15 deletions lib/components/Bookmarks/BookmarksListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { type QueryItemProps, QueryLabel } from './QueryLabel';

export interface BookmarksListItemProps extends ListItemInputProps {
id: string;
/** Input value */
inputValue?: string;
/** On change */
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
/** Loading */
loading?: boolean;
/** Expanded */
Expand All @@ -37,11 +41,6 @@ export interface BookmarksListItemProps extends ListItemInputProps {
as?: React.ElementType;
}

interface FormData {
title?: string;
params?: QueryItemProps[];
}

export const BookmarksListItem = ({
size = 'sm',
icon = 'bookmark',
Expand All @@ -54,18 +53,12 @@ export const BookmarksListItem = ({
onToggle,
saveButton,
removeButton,
inputValue,
onChange,
as,
...rest
}: BookmarksListItemProps) => {
const [formData, setFormData] = useState<FormData>({ title, params });

const onChange = (event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
setFormData((prevState) => ({
...prevState,
[name]: value,
}));
};
const [internalValue, setInternalValue] = useState<string>('');

if (expanded && !loading) {
return (
Expand All @@ -82,7 +75,20 @@ export const BookmarksListItem = ({
{expanded && (
<Section padding="lg" spacing="lg">
<QueryLabel params={params} />
{titleField && <TextField {...titleField} name="title" value={formData?.title || ''} onChange={onChange} />}
{titleField && (
<TextField
{...titleField}
name="title"
value={typeof inputValue === 'string' ? inputValue : internalValue}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
if (typeof onChange === 'function') {
onChange(e);
} else {
setInternalValue(e.target.value);
}
}}
/>
)}
{(saveButton || removeButton) && (
<Flex as="footer" direction="row" spacing="sm">
{saveButton && <Button {...saveButton} />}
Expand Down
36 changes: 33 additions & 3 deletions lib/components/Bookmarks/BookmarksSection.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react';
import { useState } from 'react';
import { type ChangeEvent, useMemo, useState } from 'react';
import { BookmarksSection, type BookmarksSectionProps } from './BookmarksSection';

const meta = {
Expand Down Expand Up @@ -83,6 +83,32 @@ export const ExpandedItem: Story = {

export const ControlledState = (args: BookmarksSectionProps) => {
const [expandedId, setExpandedId] = useState<string>('');
const [inputValue, setInputValue] = useState<Record<string, string>>({});

const bookmarkProps: BookmarksSectionProps = useMemo(
() => ({
...args,
items: args.items.map((item) => ({
...item,
inputValue: (typeof inputValue[item.id] === 'undefined' ? item.title : inputValue[item.id]) ?? '',
onChange: (e: ChangeEvent<HTMLInputElement>) => {
setInputValue((prevState) => ({
...prevState,
[item.id]: e.target.value,
}));
},
onToggle: () => onToggle(item.id),
saveButton: {
...item.saveButton,
onClick: () => {
alert(`Lagre søk med tittel ${inputValue[item.id]}`);
},
},
expanded: expandedId === item.id,
})),
}),
[args, expandedId, inputValue],
);

const onToggle = (id: string) => {
setExpandedId((prevState) => {
Expand All @@ -93,15 +119,15 @@ export const ControlledState = (args: BookmarksSectionProps) => {
});
};

return <BookmarksSection {...args} expandedId={expandedId} onToggle={onToggle} />;
return <BookmarksSection {...bookmarkProps} expandedId={expandedId} onToggle={onToggle} />;
};

export const LoadingState: Story = {
args: {
loading: true,
title: 'Henter lagrede søk ...',
description: '',
items: [{ id: '1', title: 'Loading the bookmark' }],
items: [{ id: '1', title: 'Loading the bookmark', inputValue: '', onChange: () => {} }],
},
};

Expand Down Expand Up @@ -133,6 +159,8 @@ export const AsLink: Story = {
removeButton: {
label: 'Slett',
},
inputValue: '',
onChange: () => {},
as: (props) => <a {...props} href="#bookmark-1" />,
params: [
{ type: 'search', label: 'Skatt' },
Expand All @@ -141,6 +169,8 @@ export const AsLink: Story = {
},
{
id: 'bookmark-2',
inputValue: '',
onChange: () => {},
as: (props) => <a {...props} href="#bookmark-2" />,
params: [
{ type: 'search', label: 'Skatt' },
Expand Down
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
"name": "@altinn/altinn-components",
"version": "0.13.0",
"main": "dist/index.js",
"files": [
"dist/",
"!dist/stories/",
"!dist/components/*/*.stories.js"
],
"files": ["dist/", "!dist/stories/", "!dist/components/*/*.stories.js"],
"types": "dist/types/lib/index.d.ts",
"type": "module",
"description": "Reusable react components",
"sideEffects": [
"*.css"
],
"sideEffects": ["*.css"],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"storybook": "storybook dev -p 6006",
Expand Down

0 comments on commit e07105e

Please sign in to comment.