Skip to content

Commit

Permalink
some more adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes committed Dec 11, 2024
1 parent bf5bbe0 commit a156618
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/css/src/field.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* States
*/
&:has([aria-disabled='true'], :disabled) > * {
&:has(:disabled) > * {
cursor: not-allowed;
opacity: var(--ds-disabled-opacity);
}
Expand Down
24 changes: 19 additions & 5 deletions packages/css/src/suggestion.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@
}

& > * {
padding: var(--ds-spacing-2);
border-left: var(--ds-spacing-1) solid transparent;
outline: none;
grid-template-columns: 1.2em 1fr;
padding: var(--ds-spacing-2) var(--ds-spacing-3);
padding-inline-start: var(--ds-spacing-4);
border: none;
border-left: 5px solid transparent;
border-radius: var(--ds-border-radius-sm);
justify-content: start;
background: none;
text-align: left;
height: auto;
cursor: pointer;
font-family: inherit;
font-weight: 400;

/* TMP check to demonstrate selector */
&[aria-selected='true']::before {
content: '✔ ';
&[aria-selected='true'] {
padding-inline-start: var(--ds-spacing-1);

&::before {
content: '✔ ';
}
}

@media (hover: hover) and (pointer: fine) {
Expand Down
25 changes: 15 additions & 10 deletions packages/react/src/components/Suggestion/Suggestion.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Meta, StoryFn } from '@storybook/react';

Check failure on line 1 in packages/react/src/components/Suggestion/Suggestion.stories.tsx

View workflow job for this annotation

GitHub Actions / Storybook Test Report

packages/react/src/components/Suggestion/Suggestion.stories.tsx / Komponenter/Suggestion › Preview smoke-test

assert.fail(received, expected)
Raw output
assert.fail(received, expected)

Message:
  Found 1 accessibility violations: 
-----------------------------------
Expected the HTML found at $('#\:m4k0s0gcop7') to have no violations:
<input class="ds-input" type="text" list=":r0:" placeholder="" aria-expanded="false" id=":m4k0s0gcop7">

Received:
Elements must only use supported ARIA attributes (aria-allowed-attr)

Fix all of the following:
  ARIA attribute is not allowed: aria-expanded="false"

You can find more information on this issue here: 
https://dequeuniversity.com/rules/axe/4.10/aria-allowed-attr?application=axeAPI
-----------------------------------
    at TerminalReporterV2.<anonymous> (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/reporter/terminalReporterV2.js:64:34)
    at Generator.next (<anonymous>)
    at /home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/reporter/terminalReporterV2.js:8:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/reporter/terminalReporterV2.js:4:12)
    at TerminalReporterV2.report (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/reporter/terminalReporterV2.js:22:16)
    at /home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:97:20
    at Generator.next (<anonymous>)
    at /home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:31:71
    at new Promise (<anonymous>)
    at Object.<anonymous>.__awaiter (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:27:12)
    at reportViolations (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:96:52)
    at /home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:140:44
    at Generator.next (<anonymous>)
    at fulfilled (/home/runner/work/designsystemet/designsystemet/node_modules/axe-playwright/dist/index.js:28:58)
import { Field } from '../Field';
import { Label } from '../Label';
import { Suggestion } from './';
export default {
title: 'Komponenter/Suggestion',
Expand All @@ -7,16 +9,19 @@ export default {

export const Preview: StoryFn<typeof Suggestion> = (args) => {
return (
<Suggestion {...args}>
<Suggestion.List>
<Suggestion.Empty>Tomt</Suggestion.Empty>
<Suggestion.Option>Option 1</Suggestion.Option>
<Suggestion.Option>Option 2</Suggestion.Option>
<Suggestion.Option>Option 3</Suggestion.Option>
</Suggestion.List>
<Suggestion.Input />
<Suggestion.Clear />
</Suggestion>
<Field>
<Label>Velg en destinasjon</Label>
<Suggestion {...args}>
<Suggestion.List>
<Suggestion.Empty>Tomt</Suggestion.Empty>
<Suggestion.Option>Option 1</Suggestion.Option>
<Suggestion.Option>Option 2</Suggestion.Option>
<Suggestion.Option>Option 3</Suggestion.Option>
</Suggestion.List>
<Suggestion.Input />
<Suggestion.Clear />
</Suggestion>
</Field>
);
};

Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/components/Suggestion/Suggestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DefaultProps } from '../../types';
type SuggestionContextType = {
listId?: string;
setListId?: (id: string) => void;
inputRef?: React.RefObject<HTMLInputElement>;
};

export const SuggestionContext = createContext<SuggestionContextType>({});
Expand All @@ -16,11 +17,13 @@ export type SuggestionProps =
export const Suggestion = forwardRef<HTMLDivElement, SuggestionProps>(
function Suggestion({ className, ...rest }, ref) {
const [listId, setListId] = useState(useId());

const innerRef = useRef<HTMLElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const mergedRefs = useMergeRefs([innerRef, ref]);

return (
<SuggestionContext.Provider value={{ listId, setListId }}>
<SuggestionContext.Provider value={{ listId, setListId, inputRef }}>
<div
className={cl('ds-suggestion', className)}
ref={mergedRefs}
Expand Down
24 changes: 9 additions & 15 deletions packages/react/src/components/Suggestion/SuggestionClear.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { forwardRef } from 'react';
import { forwardRef, useContext } from 'react';
import { Button, type ButtonProps } from '../Button';
import { SuggestionContext } from './Suggestion';

/* We omit children since we render the icon with css */
export type SuggestionClearProps = Omit<ButtonProps, 'variant' | 'children'> & {
Expand All @@ -17,27 +18,20 @@ export const SuggestionClear = forwardRef<
{ 'aria-label': label = 'Tøm', onClick, ...rest },
ref,
) {
const { inputRef } = useContext(SuggestionContext);

const handleClear = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>,
) => {
const target = event.target;
onClick?.(event);

if (event.defaultPrevented) return;

let input: HTMLElement | null | undefined = null;

if (target instanceof HTMLElement)
input = target.closest('.ds-suggestion')?.querySelector('input');

if (!input) throw new Error('Input is missing');
if (!inputRef?.current) throw new Error('Input is missing');
/* narrow type to make TS happy */
if (!(input instanceof HTMLInputElement))
if (!(inputRef?.current instanceof HTMLInputElement))
throw new Error('Input is not an input element');

event.preventDefault();
setReactInputValue(input, '');
input.focus();
setReactInputValue(inputRef.current, '');
inputRef.current.focus();
onClick?.(event);
};

return (
Expand Down
15 changes: 12 additions & 3 deletions packages/react/src/components/Suggestion/SuggestionInput.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMergeRefs } from '@floating-ui/react';
import { forwardRef, useContext } from 'react';
import { Input, type InputProps } from '../Input';
import { SuggestionContext } from './Suggestion';
Expand All @@ -8,8 +9,16 @@ export const SuggestionInput = forwardRef<
HTMLInputElement,
SuggestionInputProps
>(function SuggestionList(rest, ref) {
const { listId } = useContext(SuggestionContext);
const { listId, inputRef } = 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} />;
const mergedRefs = useMergeRefs([inputRef, ref]);

return (
<Input
ref={mergedRefs}
list={listId}
placeholder='' // We need an empty placeholder for the clear button to be able to show/hide
{...rest}
/>
);
});

0 comments on commit a156618

Please sign in to comment.