-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-7905: AltRadio #22
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,27 @@ | ||
| @use '../functions' as *; | ||
| @use '../variables' as *; | ||
| @use '../mixins/inputs' as inputs-mixins; | ||
| @use '../mixins/utils' as utils-mixins; | ||
|
|
||
| .ids-alt-radio { | ||
| &__source { | ||
| @include utils-mixins.hidden; | ||
| } | ||
|
|
||
| &__tile { | ||
| @include inputs-mixins.input-base; | ||
|
|
||
| & { | ||
| cursor: pointer; | ||
| padding: calculateRem(8px) calculateRem(16px); | ||
| } | ||
|
|
||
| &--checked { | ||
| background-color: var(--ids-input-checked-bg-color, #{$color-neutral-50}); | ||
| } | ||
|
|
||
| &--focused { | ||
| @include inputs-mixins.input-focus; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,43 @@ | ||
| @use '../functions' as *; | ||
| @use '../variables' as *; | ||
|
|
||
| @mixin input-focus { | ||
| border-color: var(--ids-input-focus-border-color, #{$color-primary-80}); | ||
| box-shadow: var(--ids-input-focus-box-shadow, #{$box-shadow-focus-primary}); | ||
| } | ||
|
|
||
| @mixin input-base { | ||
| border-radius: $border-radius-medium; | ||
| background: var(--ids-input-default-bg-color, #{$color-neutral-10}); | ||
| border: calculateRem(1px) solid var(--ids-input-default-border-color, #{$color-neutral-80}); | ||
| color: var(--ids-input-default-text-color, #{$color-neutral-240}); | ||
| outline: 0; | ||
|
|
||
| &--error { | ||
| background: var(--ids-input-error-bg-color, #{$color-error-20}); | ||
| border-color: var(--ids-input-error-border-color, #{$color-error-80}); | ||
| color: var(--ids-input-error-text-color, #{$color-error-90}); | ||
| } | ||
|
|
||
| &--disabled, | ||
| &.disabled, | ||
| &[disabled], | ||
| &:disabled { | ||
| color: var(--ids-input-disabled-text-color, #{$color-neutral-150}); | ||
| background: var(--ids-input-disabled-bg-color, #{$color-neutral-40}); | ||
| border-color: var(--ids-input-disabled-border-color, #{$color-neutral-80}); | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| &:hover { | ||
| border-color: var(--ids-input-hover-border-color, #{$color-primary-80}); | ||
| } | ||
|
|
||
| &:active { | ||
| border-color: var(--ids-input-active-border-color, #{$color-primary-90}); | ||
| } | ||
|
|
||
| &:focus { | ||
| @include input-focus; | ||
| } | ||
| } |
This file contains hidden or 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,8 @@ | ||
| @use '../functions' as *; | ||
|
|
||
| @mixin hidden { | ||
| width: 0; | ||
| height: 0; | ||
| overflow: hidden; | ||
| opacity: 0; | ||
| } |
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
packages/components/src/inputs/AltRadio/AltRadio.stories.tsx
This file contains hidden or 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,87 @@ | ||
| import React from 'react'; | ||
|
|
||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
| import { action } from 'storybook/actions'; | ||
|
|
||
| import { AltRadioStateful } from './AltRadio'; | ||
|
|
||
| const meta: Meta<typeof AltRadioStateful> = { | ||
| component: AltRadioStateful, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| tags: ['autodocs', 'foundation', 'inputs'], | ||
| argTypes: { | ||
| className: { | ||
| control: 'text', | ||
| }, | ||
| tileClassName: { | ||
| control: 'text', | ||
| }, | ||
| title: { | ||
| control: 'text', | ||
| }, | ||
| }, | ||
| args: { | ||
| label: '1:1', | ||
| name: 'default-input', | ||
| onBlur: action('on-blur'), | ||
| onChange: action('on-change'), | ||
| onFocus: action('on-focus'), | ||
| onInput: action('on-input'), | ||
| }, | ||
| decorators: [ | ||
| (Story) => { | ||
| return ( | ||
| <form name="default-form"> | ||
| <Story /> | ||
| </form> | ||
| ); | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof AltRadioStateful>; | ||
|
|
||
| export const Empty: Story = { | ||
| name: 'Empty', | ||
| }; | ||
|
|
||
| export const EmptyDisabled: Story = { | ||
| name: 'Empty (Disabled)', | ||
| args: { | ||
| disabled: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const EmptyError: Story = { | ||
| name: 'Empty (Error)', | ||
| args: { | ||
| error: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const Checked: Story = { | ||
| name: 'Checked', | ||
| args: { | ||
| checked: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const CheckedDisabled: Story = { | ||
| name: 'Checked (Disabled)', | ||
| args: { | ||
| disabled: true, | ||
| checked: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const CheckedError: Story = { | ||
| name: 'Checked (Error)', | ||
| args: { | ||
| error: true, | ||
| checked: true, | ||
| }, | ||
| }; |
50 changes: 50 additions & 0 deletions
50
packages/components/src/inputs/AltRadio/AltRadio.test.stories.tsx
This file contains hidden or 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,50 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
| import { expect, fn, userEvent, within } from 'storybook/test'; | ||
|
|
||
| import { AltRadioStateful } from './AltRadio'; | ||
|
|
||
| const meta: Meta<typeof AltRadioStateful> = { | ||
| component: AltRadioStateful, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| tags: ['!dev'], | ||
| args: { | ||
| label: '1:1', | ||
| name: 'default-input', | ||
| onBlur: fn(), | ||
| onChange: fn(), | ||
| onFocus: fn(), | ||
| onInput: fn(), | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof AltRadioStateful>; | ||
|
|
||
| export const Default: Story = { | ||
| name: 'Default', | ||
| play: async ({ canvasElement, step, args }) => { | ||
| const canvas = within(canvasElement); | ||
| const input = canvas.getByRole('button'); | ||
|
|
||
| await step('Radio Button handles focus event', async () => { | ||
| await expect(args.onFocus).not.toHaveBeenCalled(); | ||
|
|
||
| await userEvent.click(input); | ||
|
|
||
| await expect(args.onFocus).toHaveBeenCalledOnce(); | ||
| await expect(args.onChange).toHaveBeenCalledOnce(); | ||
| await expect(args.onInput).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| await step('Radio Button handles blur event', async () => { | ||
| await expect(args.onBlur).not.toHaveBeenCalled(); | ||
|
|
||
| await userEvent.click(canvasElement); | ||
|
|
||
| await expect(args.onBlur).toHaveBeenCalledOnce(); | ||
| }); | ||
| }, | ||
| }; |
This file contains hidden or 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,70 @@ | ||
| import React, { useRef, useState } from 'react'; | ||
|
|
||
| import BaseChoiceInput from '@ids-internal/partials/BaseChoiceInput'; | ||
| import { createCssClassNames } from '@ibexa/ids-core/helpers/cssClassNames'; | ||
| import withStateChecked from '@ids-internal/hoc/withStateChecked'; | ||
|
|
||
| import { AltRadioProps } from './AltRadio.types'; | ||
|
|
||
| const AltRadio = ({ className = '', label, tileClassName = '', title = '', ...inputProps }: AltRadioProps) => { | ||
| const { checked = false, disabled = false, error = false, onBlur, onChange, onFocus, onInput } = inputProps; | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
| const [isFocused, setIsFocused] = useState(false); | ||
| const altRadioClassName = createCssClassNames({ | ||
| 'ids-alt-radio': true, | ||
| [className]: !!className, | ||
| }); | ||
| const altRadioTileClassName = createCssClassNames({ | ||
| 'ids-alt-radio__tile': true, | ||
| 'ids-alt-radio__tile--checked': checked, | ||
| 'ids-alt-radio__tile--disabled': disabled, | ||
| 'ids-alt-radio__tile--error': error, | ||
| 'ids-alt-radio__tile--focused': isFocused, | ||
| [tileClassName]: !!tileClassName, | ||
| }); | ||
| const onTileClick = () => { | ||
| inputRef.current?.focus(); | ||
|
|
||
| if (!checked) { | ||
| onChange?.(true); | ||
| onInput?.(true); | ||
| } | ||
| }; | ||
| const onInputFocus = (event: React.FocusEvent<HTMLInputElement>) => { | ||
| setIsFocused(true); | ||
| onFocus?.(event); | ||
| }; | ||
| const onInputBlur = (event: React.FocusEvent<HTMLInputElement>) => { | ||
| setIsFocused(false); | ||
| onBlur?.(event); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={altRadioClassName} title={title}> | ||
| <div className="ids-alt-radio__source"> | ||
| <BaseChoiceInput | ||
| {...inputProps} | ||
| onBlur={onInputBlur} | ||
| onFocus={onInputFocus} | ||
| ref={(node) => { | ||
| inputRef.current = node; | ||
|
|
||
| if (typeof inputProps.ref === 'function') { | ||
| inputProps.ref(node); | ||
| } else if (inputProps.ref) { | ||
| inputProps.ref.current = node; // eslint-disable-line no-param-reassign | ||
| } | ||
| }} | ||
| type="radio" | ||
| /> | ||
| </div> | ||
| <div className={altRadioTileClassName} onClick={onTileClick} role="button"> | ||
GrabowskiM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {label} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default AltRadio; | ||
|
|
||
| export const AltRadioStateful = withStateChecked<AltRadioProps>(AltRadio); | ||
This file contains hidden or 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,8 @@ | ||
| import { ReactNode } from 'react'; | ||
|
|
||
| import { BaseChoiceInputProps } from '@ids-internal/partials/BaseChoiceInput'; | ||
|
|
||
| export interface AltRadioProps extends Omit<BaseChoiceInputProps, 'type'> { | ||
| label: ReactNode; | ||
| tileClassName?: string; | ||
| } |
This file contains hidden or 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,6 @@ | ||
| import AltRadio, { AltRadioStateful } from './AltRadio'; | ||
| import { AltRadioProps } from './AltRadio.types'; | ||
|
|
||
| export default AltRadio; | ||
| export { AltRadioStateful }; | ||
| export type { AltRadioProps }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.