-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add text resource picker to input table
- Loading branch information
Showing
9 changed files
with
232 additions
and
10 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
78 changes: 78 additions & 0 deletions
78
frontend/libs/studio-components/src/components/StudioInputTable/Cell/CellTextResource.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,78 @@ | ||
import { StudioTable } from '../../StudioTable'; | ||
import type { ForwardedRef, ReactElement, FocusEvent } from 'react'; | ||
import React, { useCallback } from 'react'; | ||
import { BaseInputCell } from './BaseInputCell'; | ||
import type { StudioTextResourceInputProps } from '../../StudioTextResourceInput/StudioTextResourceInput'; | ||
import { StudioTextResourceInput } from '../../StudioTextResourceInput/StudioTextResourceInput'; | ||
import cn from 'classnames'; | ||
import classes from './Cell.module.css'; | ||
import { useEventProps } from './useEventProps'; | ||
import { isCaretAtEnd, isCaretAtStart, isSomethingSelected } from '../dom-utils/caretUtils'; | ||
import { isCombobox } from '../dom-utils/isCombobox'; | ||
|
||
export type CellTextResourceInputProps = StudioTextResourceInputProps & { | ||
className?: string; | ||
}; | ||
|
||
export class CellTextResource extends BaseInputCell<HTMLInputElement, CellTextResourceInputProps> { | ||
render( | ||
{ className: givenClass, onFocus, ...rest }: CellTextResourceInputProps, | ||
ref: ForwardedRef<HTMLInputElement>, | ||
): ReactElement { | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
/* Eslint misinterprets this as a class component, while it's really just a functional component within a class */ | ||
|
||
const handleFocus = useCallback( | ||
(event: FocusEvent<HTMLInputElement>): void => { | ||
onFocus?.(event); | ||
event.currentTarget.select(); | ||
}, | ||
[onFocus], | ||
); | ||
|
||
const eventProps = useEventProps<HTMLInputElement>({ onFocus: handleFocus, ...rest }); | ||
|
||
const className = cn(classes.textResourceCell, givenClass); | ||
|
||
return ( | ||
<StudioTable.Cell className={className}> | ||
<StudioTextResourceInput | ||
currentIdClass={classes.currentTextId} | ||
inputClass={classes.textInput} | ||
toggleClass={classes.toggle} | ||
{...rest} | ||
{...eventProps} | ||
ref={ref} | ||
/> | ||
</StudioTable.Cell> | ||
); | ||
} | ||
|
||
shouldMoveFocusOnArrowKey({ key, currentTarget }): boolean { | ||
if (isSomethingSelected(currentTarget)) return false; | ||
switch (key) { | ||
case 'ArrowUp': | ||
return this.shouldMoveFocusOnArrowUpKey(currentTarget); | ||
case 'ArrowDown': | ||
return this.shouldMoveFocusOnArrowDownKey(currentTarget); | ||
case 'ArrowLeft': | ||
return this.shouldMoveFocusOnArrowLeftKey(currentTarget); | ||
case 'ArrowRight': | ||
return this.shouldMoveFocusOnArrowRightKey(currentTarget); | ||
} | ||
} | ||
|
||
private shouldMoveFocusOnArrowUpKey = (element: HTMLInputElement): boolean => | ||
!isCombobox(element) && isCaretAtStart(element); | ||
|
||
private shouldMoveFocusOnArrowDownKey = (element: HTMLInputElement): boolean => | ||
!isCombobox(element) && isCaretAtEnd(element); | ||
|
||
private shouldMoveFocusOnArrowLeftKey = (element: HTMLInputElement): boolean => | ||
isCaretAtStart(element); | ||
|
||
private shouldMoveFocusOnArrowRightKey = (element: HTMLInputElement): boolean => | ||
isCaretAtEnd(element); | ||
|
||
shouldMoveFocusOnEnterKey = ({ currentTarget }): boolean => !isCombobox(currentTarget); | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...tend/libs/studio-components/src/components/StudioInputTable/dom-utils/isCombobox.test.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,17 @@ | ||
import { isCombobox } from './isCombobox'; | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
describe('isCombobox', () => { | ||
it('Returns true when the element is a combobox', () => { | ||
render(<input type='text' role='combobox' />); | ||
const element: HTMLInputElement = screen.getByRole('combobox'); | ||
expect(isCombobox(element)).toBe(true); | ||
}); | ||
|
||
it('Returns false when the element is not a combobox', () => { | ||
render(<input type='text' />); | ||
const element: HTMLInputElement = screen.getByRole('textbox'); | ||
expect(isCombobox(element)).toBe(false); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
frontend/libs/studio-components/src/components/StudioInputTable/dom-utils/isCombobox.ts
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,3 @@ | ||
export function isCombobox(element: HTMLInputElement): boolean { | ||
return element.getAttribute('role') === 'combobox'; | ||
} |
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
Oops, something went wrong.