-
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
11 changed files
with
364 additions
and
116 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
import { StudioTable } from '../../StudioTable'; | ||
import { type FocusEvent, ForwardedRef, ReactElement, useCallback } from 'react'; | ||
import React from 'react'; | ||
import { BaseInputCell } from './BaseInputCell'; | ||
import { | ||
StudioTextResourceInput, | ||
StudioTextResourceInputProps, | ||
} 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 { | ||
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 }) { | ||
if (isSomethingSelected(currentTarget)) return false; | ||
switch (key) { | ||
case 'ArrowUp': | ||
return isCaretAtStart(currentTarget) && !isCombobox(currentTarget); | ||
case 'ArrowDown': | ||
return isCaretAtEnd(currentTarget) && !isCombobox(currentTarget); | ||
case 'ArrowLeft': | ||
return isCaretAtStart(currentTarget); | ||
case 'ArrowRight': | ||
return isCaretAtEnd(currentTarget); | ||
} | ||
} | ||
|
||
shouldMoveFocusOnEnterKey = () => false; | ||
} |
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 = screen.getByRole('combobox'); | ||
expect(isCombobox(element)).toBe(true); | ||
}); | ||
|
||
it('Returns false when the element is not a combobox', () => { | ||
render(<input type='text' />); | ||
const element = 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
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.