-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(palette): add keyboard support for palette entries
Closes #503
- Loading branch information
Niklas Kiefer
committed
Oct 10, 2023
1 parent
feefc13
commit 8690082
Showing
4 changed files
with
132 additions
and
44 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
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
56 changes: 56 additions & 0 deletions
56
packages/form-js-editor/src/features/palette/components/PaletteEntry.js
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,56 @@ | ||
import { | ||
iconsByType | ||
} from '../../../render/components/icons'; | ||
|
||
import { useService } from '../../../render/hooks'; | ||
|
||
export default function PaletteEntry(props) { | ||
const { | ||
type, | ||
label | ||
} = props; | ||
|
||
const modeling = useService('modeling'); | ||
const formEditor = useService('formEditor'); | ||
|
||
const Icon = iconsByType(type); | ||
|
||
const onKeyDown = (event) => { | ||
if (event.code === 'Enter') { | ||
|
||
const { fieldType: type } = event.target.dataset; | ||
|
||
const { schema } = formEditor._getState(); | ||
|
||
// add new form field to last position | ||
modeling.addFormField({ type }, schema, schema.components.length); | ||
} | ||
}; | ||
|
||
return ( | ||
<button | ||
class="fjs-palette-field fjs-drag-copy fjs-no-drop" | ||
data-field-type={ type } | ||
title={ `Create ${getIndefiniteArticle(type)} ${label} element` } | ||
onKeyDown={ onKeyDown } | ||
> | ||
{ | ||
Icon ? <Icon class="fjs-palette-field-icon" width="36" height="36" viewBox="0 0 54 54" /> : null | ||
} | ||
<span class="fjs-palette-field-text">{ label }</span> | ||
</button> | ||
); | ||
} | ||
|
||
|
||
// helpers /////////// | ||
|
||
function getIndefiniteArticle(type) { | ||
if ([ | ||
'image' | ||
].includes(type)) { | ||
return 'an'; | ||
} | ||
|
||
return 'a'; | ||
} |
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