-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Meta, Canvas, Controls, Primary } from '@storybook/blocks'; | ||
import * as SuggestionStories from './Suggestion.stories'; | ||
|
||
<Meta of={ToggleGroupStories} /> | ||
|
||
# ToggleGroup | ||
|
||
Med `ToggleGroup` kan brukerne velge alternativer som påvirker innholdet på en side. Komponenten består av en gruppe knapper som henger sammen, der kun én knapp er mulig å velge om gangen. | ||
|
||
<Primary /> | ||
<Controls /> |
10 changes: 10 additions & 0 deletions
10
packages/react/src/components/Suggestion/Suggestion.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Meta, StoryFn } from '@storybook/react'; | ||
import { Suggestion } from './Suggestion'; | ||
export default { | ||
title: 'Komponenter/Suggestion', | ||
component: Suggestion, | ||
} as Meta; | ||
|
||
export const Preview: StoryFn<typeof Suggestion> = (args) => { | ||
return <Suggestion {...args} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useMergeRefs } from '@floating-ui/react'; | ||
import cl from 'clsx/lite'; | ||
import { createContext, forwardRef, useId, useRef, useState } from 'react'; | ||
import type { DefaultProps } from '../../types'; | ||
|
||
type SuggestionContextType = { | ||
listId?: string; | ||
setListId?: (id: string) => void; | ||
}; | ||
|
||
export const SuggestionContext = createContext<SuggestionContextType>({}); | ||
|
||
export type SuggestionProps = | ||
DefaultProps & {} & React.HTMLAttributes<HTMLDivElement>; | ||
|
||
export const Suggestion = forwardRef<HTMLDivElement, SuggestionProps>( | ||
function Suggestion({ className, ...rest }, ref) { | ||
const [listId, setListId] = useState(useId()); | ||
const innerRef = useRef<HTMLElement>(null); | ||
const mergedRefs = useMergeRefs([innerRef, ref]); | ||
|
||
return ( | ||
<SuggestionContext.Provider value={{ listId, setListId }}> | ||
<div | ||
className={cl('ds-suggestion', className)} | ||
ref={mergedRefs} | ||
{...rest} | ||
/> | ||
</SuggestionContext.Provider> | ||
); | ||
}, | ||
); |
19 changes: 19 additions & 0 deletions
19
packages/react/src/components/Suggestion/SuggestionEmpty.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { HTMLAttributes } from 'react'; | ||
import { forwardRef } from 'react'; | ||
import type { DefaultProps } from '../../types'; | ||
|
||
export type SuggestionEmptyProps = HTMLAttributes<HTMLDivElement> & | ||
DefaultProps; | ||
export const SuggestionEmpty = forwardRef<HTMLDivElement, SuggestionEmptyProps>( | ||
function SuggestionEmpty(rest, ref) { | ||
return ( | ||
<div | ||
aria-disabled='true' | ||
ref={ref} | ||
role='option' | ||
tabIndex={0} | ||
{...rest} | ||
/> | ||
); | ||
}, | ||
); |
15 changes: 15 additions & 0 deletions
15
packages/react/src/components/Suggestion/SuggestionInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { forwardRef, useContext } from 'react'; | ||
import { Input, type InputProps } from '../Input'; | ||
import { SuggestionContext } from './Suggestion'; | ||
|
||
export type SuggestionInputProps = InputProps; | ||
|
||
export const SuggestionInput = forwardRef< | ||
HTMLInputElement, | ||
SuggestionInputProps | ||
>(function SuggestionList(rest, ref) { | ||
const { listId } = useContext(SuggestionContext); | ||
|
||
/* We need an empty placeholder for the clear button to be able to show/hide */ | ||
return <Input ref={ref} list={listId} placeholder='' {...rest} />; | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/react/src/components/Suggestion/SuggestionList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { HTMLAttributes } from 'react'; | ||
import { forwardRef, useContext, useEffect } from 'react'; | ||
import '@u-elements/u-datalist'; | ||
|
||
import type { DefaultProps } from '../../types'; | ||
import { SuggestionContext } from './Suggestion'; | ||
|
||
export type SuggestionListProps = HTMLAttributes<HTMLDataListElement> & | ||
DefaultProps; | ||
|
||
export const SuggestionList = forwardRef< | ||
HTMLDataListElement, | ||
SuggestionListProps | ||
>(function SuggestionList({ className, id, ...rest }, ref) { | ||
const { listId, setListId } = useContext(SuggestionContext); | ||
|
||
useEffect(() => { | ||
if (id && listId !== id) setListId?.(id); | ||
}, [listId, id, setListId]); | ||
|
||
return ( | ||
<u-datalist | ||
class={className} // Using "class" since React does not translate className on custom elements | ||
id={listId} | ||
ref={ref} | ||
{...rest} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Suggestion as SuggestionRoot } from './Suggestion'; | ||
import { SuggestionEmpty } from './SuggestionEmpty'; | ||
import { SuggestionInput } from './SuggestionInput'; | ||
import { SuggestionList } from './SuggestionList'; | ||
|
||
const Suggestion = Object.assign(SuggestionRoot, { | ||
List: SuggestionList, | ||
Input: SuggestionInput, | ||
Empty: SuggestionEmpty, | ||
}); | ||
|
||
export { Suggestion, SuggestionList, SuggestionInput, SuggestionEmpty }; | ||
export type { SuggestionProps } from './Suggestion'; | ||
export type { SuggestionListProps } from './SuggestionList'; | ||
export type { SuggestionInputProps } from './SuggestionInput'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters