Skip to content

Commit

Permalink
fix: scroll keyboard selected element into view
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
nikku committed Mar 24, 2022
1 parent 6092bfa commit 7b5bc68
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 3 deletions.
142 changes: 142 additions & 0 deletions example/.camunda/element-templates/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,148 @@
}
}
]
}, {
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "REST Task 1",
"id": "io.camunda.examples.RestTask1",
"description": "A REST API invocation task.",
"appliesTo": [
"bpmn:ServiceTask"
],
"properties": [
{
"type": "Hidden",
"value": "http",
"binding": {
"type": "zeebe:taskDefinition:type"
}
},
{
"label": "REST Endpoint URL",
"description": "Specify the url of the REST API to talk to.",
"type": "String",
"binding": {
"type": "zeebe:taskHeader",
"key": "url"
},
"constraints": {
"notEmpty": true,
"pattern": {
"value": "^https?://.*",
"message": "Must be http(s) URL."
}
}
},
{
"label": "REST Method",
"description": "Specify the HTTP method to use.",
"type": "Dropdown",
"value": "get",
"choices": [
{ "name": "GET", "value": "get" },
{ "name": "POST", "value": "post" },
{ "name": "PATCH", "value": "patch" },
{ "name": "DELETE", "value": "delete" }
],
"binding": {
"type": "zeebe:taskHeader",
"key": "method"
}
},
{
"label": "Request Body",
"description": "Data to send to the endpoint.",
"value": "",
"type": "String",
"optional": true,
"binding": {
"type": "zeebe:input",
"name": "body"
}
},
{
"label": "Result Variable",
"description": "Name of variable to store the response data in.",
"value": "response",
"type": "String",
"optional": true,
"binding": {
"type": "zeebe:output",
"source": "= body"
}
}
]
}, {
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "REST Task 2",
"id": "io.camunda.examples.RestTask2",
"description": "A REST API invocation task.",
"appliesTo": [
"bpmn:ServiceTask"
],
"properties": [
{
"type": "Hidden",
"value": "http",
"binding": {
"type": "zeebe:taskDefinition:type"
}
},
{
"label": "REST Endpoint URL",
"description": "Specify the url of the REST API to talk to.",
"type": "String",
"binding": {
"type": "zeebe:taskHeader",
"key": "url"
},
"constraints": {
"notEmpty": true,
"pattern": {
"value": "^https?://.*",
"message": "Must be http(s) URL."
}
}
},
{
"label": "REST Method",
"description": "Specify the HTTP method to use.",
"type": "Dropdown",
"value": "get",
"choices": [
{ "name": "GET", "value": "get" },
{ "name": "POST", "value": "post" },
{ "name": "PATCH", "value": "patch" },
{ "name": "DELETE", "value": "delete" }
],
"binding": {
"type": "zeebe:taskHeader",
"key": "method"
}
},
{
"label": "Request Body",
"description": "Data to send to the endpoint.",
"value": "",
"type": "String",
"optional": true,
"binding": {
"type": "zeebe:input",
"name": "body"
}
},
{
"label": "Result Variable",
"description": "Name of variable to store the response data in.",
"value": "response",
"type": "String",
"optional": true,
"binding": {
"type": "zeebe:output",
"source": "= body"
}
}
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
Expand Down
20 changes: 17 additions & 3 deletions src/element-template-chooser/ElementTemplateChooser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState
} from 'preact/hooks';
Expand Down Expand Up @@ -91,6 +92,7 @@ function TemplateComponent(props) {
} = props;

const inputRef = useRef();
const resultsRef = useRef();

const [ value, setValue ] = useState('');

Expand Down Expand Up @@ -124,10 +126,22 @@ function TemplateComponent(props) {


// focus input on initial mount
useEffect(() => {
useLayoutEffect(() => {
inputRef.current.focus();
}, []);

// scroll to keyboard selected result
useLayoutEffect(() => {

const containerEl = resultsRef.current;

const selectedEl = containerEl.querySelector('.selected');

if (selectedEl) {
selectedEl.scrollIntoViewIfNeeded();
}
}, [ keyboardSelectedTemplate ]);

useEffect(() => {
setSelectedTemplate(mouseSelectedTemplate || keyboardSelectedTemplate);
}, [ keyboardSelectedTemplate, mouseSelectedTemplate ]);
Expand Down Expand Up @@ -200,11 +214,11 @@ function TemplateComponent(props) {
/>
</div>
<ul class="cmd-change-menu__results">
<ul class="cmd-change-menu__results" ref=${ resultsRef }>
${templates.map(template => html`
<li
key=${template.id}
class=${ clsx('cmd-change-menu__entry', { selected: template === keyboardSelectedTemplate }) }
class=${ clsx('cmd-change-menu__entry', { selected: !mouseSelectedTemplate && template === keyboardSelectedTemplate }) }
onMouseEnter=${ () => setMouseSelectedTemplate(template) }
onMouseLeave=${ () => setMouseSelectedTemplate(null) }
onClick=${ () => onClose(template) }
Expand Down

0 comments on commit 7b5bc68

Please sign in to comment.