Enzyme simulate onClick event on a given position - onclick

I am writing a test case to simulate the user clicking on a Textarea on a given caret position.
I tried to simulate 'click' event with a caret position using selectionStart and selectionEnd as:
it('onClick', () => {
const event = { target: { selectionStart: 3, selectionEnd: 3 } };
wrapper.find('textarea').simulate('click', event);
})
This does not work and i feel this is a incorrect way of setting the cursor position inside a element with the onCLick event.
Any suggestions on how to simulate a click event with cursor position for a textArea ..?

You can change selectionStart and selectionEnd in textarea's dom element:
it('onClick', () => {
const wrapper = mount(<MyComp></MyComp>) // <-- Note that shallow doesn't work
const textareaWrapper = component3.find('textarea') // find textarea
const textarea = textareaWrapper.getDOMNode() // get textarea's dom element
textarea.selectionStart = 3 // set cursor position start
textarea.selectionEnd = 3 // set cursor position end
textareaWrapper.simulate('click') // simulate click
...
})
Ps: you should mount your element in order to access the dom elements.

Related

Using react-Beautiful-dnd, how can you change draggable dimensions before the drag begins (onBeforeCapture)

This is the code I've found to retrieve the correct draggable element and edit it's dimensions, during the onBeforeCapture() responder. Changing dimensions during this responder is in accordance with the documentation. This seems to work in only changing the dimensions, the other problems is that I am using the renderClone method, and so the draggable is just dragging with a huge offset that is not close to the correct mouse position. Also dnd is treating the drop placeholder as if the draggable is the original large size. Is there any way to correct for this mouse position, and placeholder dimensions? I've looked into adding mouseDown/mouseUp handlers on the inner element of the draggable but that doesn't seem to work either.
const cardSize = 140;
const draggableAttr = "data-rbd-drag-handle-draggable-id";
const getAttr = (key: string, value: string) => `[${key}=${value}]`;
const draggableQuery = getAttr(draggableAttr, update.draggableId);
const draggable = document.querySelector(draggableQuery);
draggable.setAttribute("style", `width: ${cardSize}px; height: ${cardSize}px;`);
I noticed onBeforeCapture was triggering after onDragEnd (therefore resizing the draggable improperly), so I created some state to remember the last beforeCaptureResult and return if it is equivalent to the current result.

React select div height when DOM is loaded

I am having issues with react Plotly displaying correctly when a graph loaded in a hidden tab is selected. It seems this is a known issue where every tab but the default tab will not resize appropriately because it doesn't know the hidden graph's dimensions.
To get around this I would like to dynamically update the tab height to be equal to the height of the default tab. Something like this: Change div height when tab is selected.
The issue is, I am unable to select the tab height value on DOM load. I thought I could add a componentDidMount function with an evenlistener for window load like such:
constructor(props) {
super(props)
this.state = {
value: 0
};
this.handleLoad = this.handleLoad.bind(this);
}
componentDidMount() {
window.addEventListener('load', this.handleLoad);
}
handleLoad() {
console.log("firstTab height: " + $('#firstTab').offsetHeight)
}
This issue is, the console log is outputting firstTab height: undefined.
When i inspect the web page and put the command $('#firstTab').offsetHeight into the console I am able to come up with a height of 697 px.
I don't want to hardcode the tab pixel size. Can anyone guide me as to why I am failing to grab the element height on window load?
Try using clientHeight property.
componentDidMount() {
const height = document.getElementById('firstTab').clientHeight;
}
I think instead of using event listener, you can do something like this.
componentDidMount() {
const height = this.firstTab.clientHeight;
console.log(height) // => gives you height
}
render() {
return (
<div
id="firstTab"
ref={ (firsTabElement) => { this.divElement = firstTabElement } }
>
Tab Content
</div>
)
}
After the first render if you are not hiding or putting any condition to remove the firstTab from the DOM. It will be available to you in componentDidMount lifecycle.

customize shape of kendo tooltip

I would like to customize the shape of Kendo Tooltips for a grid.
I saw the example on kendo site, it has the arrow outside the box, and the box has a nice rounded shape.
Working on css, using .k-tooltip I can change width, height, background. But I get a square box with the arrow inside which sometimes overrides part of the text content.
I thought that callout would help but I was not able to get anything.
How can I change shape, image and position of the arrows, shape of the box ?
Moreover, how can I trigger the tooltip only when part of the text in a grid cell is visible ?
Thanks a lot for any hint
regards
Marco
I think "arrow" you mean callout. You can turn off callout by:
$(document).ready(function() {
$("#target").kendoTooltip({
callout: false
});
});
About your question "Moreover, how can I trigger the tooltip only when part of the text in a grid cell is visible?"
If I understand you correctly you would like to show tooltip only when there is text with ellipsis (partially visible in the cell), but you don't want to show a tooltip if there is a full text is visible or if there is no text in the cell. If that is the case, you can do this way:
function initializeTooltip(element, filter) {
return element.kendoTooltip({
autoHide: true,
filter: filter,
callout: false,
content: function (e) {
var target = e.target,
tooltip = e.sender,
tooltipText = "";
if (isEllipsisActive(target[0])) {
tooltipText = $(target).text();
}
tooltip.popup.unbind("open");
tooltip.popup.bind("open", function (arg) {
tooltip.refreshTooltip = false;
if (!isEllipsisActive(target[0])) {
arg.preventDefault();
} else if (tooltipText !== $(target).text()) {
tooltip.refreshTooltip = true;
}
});
return tooltipText;
},
show: function () {
if (this.refreshTooltip) {
this.refresh();
}
}
}).data("kendoTooltip");
};
// determanes if text has ellipsis
function isEllipsisActive(e) {
return e.offsetWidth < e.scrollWidth;
}
$(function () {
initializeTooltip($("#yourGridId"), ".tooltip");
});
tooltip in this case is class name of the column that you would like to use tooltip for, but you can call that class anyway you wish. In case if you are using Kendo ASP.NET MVC it will look something like this
c.Bound(p => p.ClientName)
.Title("Client")
.HtmlAttributes(new {#class = "tooltip"});

Affecting KineticJs.Image by css

Hello i'm working on a project that requires canvas manipulation. I need to draw an image and have to move it within the canvas. Which was not so hard to accomplish.. However i need to change my cursor into "move" when hovering the image like
img{
cursor:move;
}
I couldn't find any way to do this. Any suggestion??
Thanks in advance..
When you drag an Kinetic.Image, you get dragstart and dragend events.
You can change the cursor type in those event handlers:
// starting to drag -- display the move cursor
image1.on('dragstart', function () {
document.body.style.cursor = 'move';
});
// done dragging -- display the regular cursor
image1.on('dragend', function () {
document.body.style.cursor = 'default';
});
Basically you need an element which you can style on the page but with display: none, then put that element onto the canvas like this:
var image = document.getElementById('image');
context.drawImage(image, 0, 0);

Scrolling a browser window with modal window, Drupal 6

I'm using Drupal 6 + dialog.module, Ctools(Axaj), jQuery 1.3.2, jQuery UI
When i'm open a modal dialog window the scrollbar of the browser is locked. I can scroll that if i use mouse scroll-wheel, but not by clicking or key-up, key-down functions.
What is the trouble? I can't find any block function in code like this:
sites/all/modules/dialog/dialog.js
Drupal.CTools.AJAX.commands.dialog_display = function(command) {
var $el = Drupal.Dialog.dialog,
o = {},
overrides = {};
// Ensure that the dialog wasn't closed before the request completed.
if ($el) {
$el.html(command.output).dialog('show');
// Merge all of the options together: defaults, overrides, and options
// specified by the command, then apply them.
overrides = {
// Remove any previously added buttons.
'buttons': {},
'title': command.title,
//'maxHeight': Math.floor($(window).height() * .8), // HACK // DISABLED
'minHeight' : 700, // HACK // I've set it, and dialog window automagically adjusted - we need central part with content have height = auto, so there is no scroll bar, and hence no problems with autocomplete popups in dialog windows
};
o = $.extend({}, Drupal.settings.Dialog.defaults, overrides, command.options);
$.each(o, function (i, v) { $el.dialog('option', i, v); });
if ($el.height() > o.maxHeight) {
$el.dialog('option', 'height', o.maxHeight);
$el.dialog('option', 'position', o.position);
// This is really ugly, but dialog gives us no way to call _size() in a
// sane way!
$el.data('dialog')._size();
}
Drupal.attachBehaviors($el);
}
};
And i didn'y find anything interesting in css files. What's the trouble?

Resources