-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(atomic): add radio-button function for lit components (#4864)
Add radio-button element for lit components https://coveord.atlassian.net/browse/KIT-3834 --------- Co-authored-by: Frederic Beaudoin <[email protected]>
- Loading branch information
1 parent
e84d61b
commit c813a3f
Showing
9 changed files
with
202 additions
and
21 deletions.
There are no files selected for viewing
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
packages/atomic/src/components/common/items-per-page/choices.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
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,129 @@ | ||
import {html, TemplateResult} from 'lit'; | ||
import {ifDefined} from 'lit-html/directives/if-defined.js'; | ||
import {classMap} from 'lit/directives/class-map.js'; | ||
import {ref, RefOrCallback} from 'lit/directives/ref.js'; | ||
import {createRipple} from '../../utils/ripple'; | ||
import { | ||
ButtonStyle, | ||
getClassNameForButtonStyle, | ||
getRippleColorForButtonStyle, | ||
} from './button-style'; | ||
|
||
export interface RadioButtonProps { | ||
groupName: string; | ||
selectWhenFocused?: boolean; | ||
onChecked?(): void; | ||
style?: ButtonStyle; | ||
key?: string | number; | ||
checked?: boolean; | ||
class?: string; | ||
text?: string; | ||
part?: string; | ||
ariaLabel?: string; | ||
ariaCurrent?: | ||
| 'page' | ||
| 'step' | ||
| 'location' | ||
| 'date' | ||
| 'time' | ||
| 'true' | ||
| 'false'; | ||
ref?: RefOrCallback; | ||
} | ||
|
||
// TODO: KIT-3822: add unit tests to this function | ||
export const radioButton = (props: RadioButtonProps): TemplateResult => { | ||
const classNames = { | ||
'btn-radio': true, | ||
selected: Boolean(props.checked), | ||
...(props.class && {[props.class]: true}), | ||
...(props.style && {[getClassNameForButtonStyle(props.style)]: true}), | ||
}; | ||
|
||
const onMouseDown = (e: MouseEvent) => { | ||
if (props.style) { | ||
const rippleColor = getRippleColorForButtonStyle(props.style); | ||
createRipple(e, {color: rippleColor}); | ||
} | ||
}; | ||
|
||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (props.selectWhenFocused !== false) { | ||
return; | ||
} | ||
const {key} = event; | ||
const radioGroup = (event.currentTarget as HTMLElement).parentNode; | ||
|
||
if (!radioGroup || !isArrowKey(key)) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
|
||
const buttons = getRadioButtons(radioGroup); | ||
const currentIndex = getCurrentIndex( | ||
buttons, | ||
event.currentTarget as HTMLInputElement | ||
); | ||
const newIndex = getNewIndex(key, currentIndex, buttons.length); | ||
|
||
if (buttons[newIndex]) { | ||
buttons[newIndex].focus(); | ||
} | ||
}; | ||
|
||
const isArrowKey = (key: string) => { | ||
return ['ArrowLeft', 'ArrowRight', 'ArrowDown', 'ArrowUp'].includes(key); | ||
}; | ||
|
||
const getRadioButtons = (radioGroup: ParentNode) => { | ||
return Array.from( | ||
radioGroup.querySelectorAll('[type="radio"]') | ||
) as HTMLInputElement[]; | ||
}; | ||
|
||
const getCurrentIndex = ( | ||
buttons: HTMLInputElement[], | ||
currentButton: HTMLInputElement | ||
) => { | ||
return buttons.findIndex((button) => button === currentButton); | ||
}; | ||
|
||
const getNewIndex = (key: string, currentIndex: number, length: number) => { | ||
switch (key) { | ||
case 'ArrowLeft': | ||
case 'ArrowUp': | ||
return (currentIndex - 1 + length) % length; | ||
case 'ArrowRight': | ||
case 'ArrowDown': | ||
return (currentIndex + 1) % length; | ||
default: | ||
return currentIndex; | ||
} | ||
}; | ||
|
||
const onChange = (e: Event) => { | ||
const input = e.currentTarget as HTMLInputElement; | ||
if (input.checked && props.onChecked) { | ||
props.onChecked(); | ||
} | ||
}; | ||
|
||
return html` | ||
<input | ||
type="radio" | ||
name=${props.groupName} | ||
class=${classMap(classNames)} | ||
value=${ifDefined(props.text)} | ||
part=${ifDefined(props.part)} | ||
aria-label=${ifDefined(props.ariaLabel ?? props.text)} | ||
aria-current=${ifDefined(props.ariaCurrent)} | ||
?checked=${Boolean(props.checked)} | ||
.key=${props.key} | ||
@change=${onChange} | ||
@keydown=${handleKeyDown} | ||
@mousedown=${onMouseDown} | ||
${ref(props.ref)} | ||
/> | ||
`; | ||
}; |
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
55 changes: 55 additions & 0 deletions
55
packages/atomic/src/components/common/stencil-button-style.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,55 @@ | ||
export type ButtonStyle = | ||
| 'primary' | ||
| 'outline-primary' | ||
| 'outline-neutral' | ||
| 'outline-error' | ||
| 'outline-bg-neutral' | ||
| 'outline-bg-error' | ||
| 'text-primary' | ||
| 'text-neutral' | ||
| 'text-transparent' | ||
| 'square-neutral'; | ||
|
||
/** | ||
* @deprecated Should only be used for Stencil components; for Lit components, use the button-style.ts | ||
* This file is required to be in a tsx file to be able to use it in Stencil components. | ||
*/ | ||
export function getClassNameForButtonStyle(buttonStyle: ButtonStyle) { | ||
switch (buttonStyle) { | ||
case 'primary': | ||
return 'btn-primary'; | ||
case 'outline-primary': | ||
return 'btn-outline-primary'; | ||
case 'outline-neutral': | ||
return 'btn-outline-neutral'; | ||
case 'outline-error': | ||
return 'btn-outline-error'; | ||
case 'outline-bg-neutral': | ||
return 'btn-outline-bg-neutral'; | ||
case 'outline-bg-error': | ||
return 'btn-outline-bg-error'; | ||
case 'text-primary': | ||
return 'btn-text-primary'; | ||
case 'text-neutral': | ||
return 'btn-text-neutral'; | ||
case 'text-transparent': | ||
return 'btn-text-transparent'; | ||
case 'square-neutral': | ||
return 'btn-square-neutral'; | ||
} | ||
} | ||
|
||
/** | ||
* @deprecated Should only be used for Stencil components; for Lit components, use the button-style.ts | ||
* This file is required to be in a tsx file to be able to use it in Stencil components. | ||
*/ | ||
export function getRippleColorForButtonStyle(buttonStyle: ButtonStyle) { | ||
switch (buttonStyle) { | ||
case 'primary': | ||
return 'primary'; | ||
case 'text-transparent': | ||
return 'neutral-light'; | ||
default: | ||
return 'neutral'; | ||
} | ||
} |
27 changes: 12 additions & 15 deletions
27
...ic/src/components/common/radio-button.tsx → ...omponents/common/stencil-radio-button.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
2 changes: 1 addition & 1 deletion
2
packages/atomic/src/components/common/suggestions/instant-item.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