Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renovation: add onVisibilityChange callback functionality #11830

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
LazyLahtak marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand"
],
"runtimeArgs": [
"--harmony"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}"
},
]
}
30 changes: 15 additions & 15 deletions js/renovation/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,16 @@ export class ButtonInput extends WidgetInput {

// tslint:disable-next-line: max-classes-per-file
@Component({
name: 'Button',
components: [],
viewModel: viewModelFunction,
name: 'Button',
view: viewFunction,
viewModel: viewModelFunction,
})

export default class Button extends JSXComponent<ButtonInput> {
@Ref() contentRef!: HTMLDivElement;
@Ref() submitInputRef!: HTMLInputElement;

@Effect()
submitEffect() {
const namespace = 'UIFeedback';
const { onSubmit } = this.props;

click.on(this.submitInputRef, (e) => {
onSubmit?.(e);
e.stopPropagation();
}, { namespace });

return () => click.off(this.submitInputRef, { namespace });
}

onActive(event: Event) {
const { useInkRipple } = this.props;
const config = getInkRippleConfig(this.props);
Expand Down Expand Up @@ -184,4 +171,17 @@ export default class Button extends JSXComponent<ButtonInput> {
this.onWidgetClick(e);
}
}

@Effect()
submitEffect() {
const namespace = 'UIFeedback';
const { onSubmit } = this.props;

click.on(this.submitInputRef, (e) => {
onSubmit?.(e);
e.stopPropagation();
}, { namespace });

return () => click.off(this.submitInputRef, { namespace });
}
}
146 changes: 73 additions & 73 deletions js/renovation/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import { each } from '../core/utils/iterator';
import { extend } from '../core/utils/extend';
import { focusable } from '../ui/widget/selectors';
import { isFakeClickEvent } from '../events/utils';
import { hasWindow } from '../core/utils/window';

const getStyles = ({ width, height }) => {
const computedWidth = typeof width === 'function' ? width() : width;
const computedHeight = typeof height === 'function' ? height() : height;

return {
width: computedWidth ?? void 0,
height: computedHeight ?? void 0,
width: computedWidth ?? void 0,
};
};

Expand Down Expand Up @@ -68,6 +67,7 @@ const getCssClasses = (model: Partial<Widget> & Partial<WidgetInput>) => {
model._active && className.push('dx-state-active');
model._hovered && isHoverable && !model._active && className.push('dx-state-hover');
model.rtlEnabled && className.push('dx-rtl');
model.onVisibilityChange && className.push('dx-visibility-change-handler');
model.elementAttr?.class && className.push(model.elementAttr.class);

return className.join(' ');
Expand All @@ -93,19 +93,21 @@ export const viewModelFunction = ({
tabIndex,
visible,
width,
onVisibilityChange,
},

widgetRef,
}: Widget) => {
const styles = getStyles({ width, height });
const attrsWithoutClass = getAttributes({
elementAttr,
accessKey: focusStateEnabled && !disabled && accessKey,
elementAttr,
});
const arias = getAria({ ...aria, disabled, hidden: !visible });
const cssClasses = getCssClasses({
disabled, visible, _focused, _active, _hovered, rtlEnabled,
elementAttr, hoverStateEnabled, focusStateEnabled, className,
_active, _focused, _hovered, className,
disabled, elementAttr, focusStateEnabled, hoverStateEnabled,
onVisibilityChange, rtlEnabled, visible,
});

return {
Expand Down Expand Up @@ -143,11 +145,11 @@ export const viewFunction = (viewModel: any) => {
export class WidgetInput {
@OneWay() _feedbackHideTimeout?: number = 400;
@OneWay() _feedbackShowTimeout?: number = 30;
@OneWay() _visibilityChanged?: (args: any) => undefined;
@OneWay() accessKey?: string | null = null;
@OneWay() activeStateEnabled?: boolean = false;
@OneWay() activeStateUnit?: string;
@OneWay() aria?: any = {};
@Slot() children?: any;
@OneWay() className?: string | undefined = '';
@OneWay() clickArgs?: any = {};
@OneWay() disabled?: boolean = false;
Expand All @@ -158,26 +160,24 @@ export class WidgetInput {
@OneWay() hoverStateEnabled?: boolean = false;
@OneWay() name?: string = '';
@OneWay() onActive?: (e: any) => any = (() => undefined);
@Event() onClick?: (e: any) => void = (() => { });
@OneWay() onDimensionChanged?: () => any = (() => undefined);
@OneWay() onInactive?: (e: any) => any = (() => undefined);
@OneWay() onKeyPress?: (e: any, options: any) => any = (() => undefined);
@OneWay() onKeyboardHandled?: (args: any) => any | undefined;
@OneWay() onKeyPress?: (e: any, options: any) => any = (() => undefined);
@OneWay() onVisibilityChange?: (args: boolean) => undefined;
@OneWay() rtlEnabled?: boolean = config().rtlEnabled;
@OneWay() tabIndex?: number = 0;
@OneWay() visible?: boolean = true;
@OneWay() width?: string | number | null = null;

@Slot() children?: any;

@Event() onClick?: (e: any) => void = (() => { });
}

// tslint:disable-next-line: max-classes-per-file
@Component({
name: 'Widget',
components: [],
viewModel: viewModelFunction,
name: 'Widget',
view: viewFunction,
viewModel: viewModelFunction,
})

export default class Widget extends JSXComponent<WidgetInput> {
Expand All @@ -188,38 +188,6 @@ export default class Widget extends JSXComponent<WidgetInput> {
@Ref()
widgetRef!: HTMLDivElement;

@Effect()
visibilityEffect() {
const { name, _visibilityChanged, visible } = this.props;
const namespace = `${name}VisibilityChange`;

if (_visibilityChanged !== undefined && hasWindow()) {
visibility.on(this.widgetRef,
() => visible && _visibilityChanged!(true),
() => visible && _visibilityChanged!(false),
{ namespace },
);

return () => visibility.off(this.widgetRef, { namespace });
}

return null;
}

@Effect()
resizeEffect() {
const namespace = `${this.props.name}VisibilityChange`;
const { onDimensionChanged } = this.props;

if (onDimensionChanged) {
resize.on(this.widgetRef, onDimensionChanged, { namespace });

return () => resize.off(this.widgetRef, { namespace });
}

return null;
}

@Effect()
accessKeyEffect() {
const namespace = 'UIFeedback';
Expand All @@ -241,26 +209,6 @@ export default class Widget extends JSXComponent<WidgetInput> {
return null;
}

@Effect()
hoverEffect() {
const namespace = 'UIFeedback';
const { activeStateUnit, hoverStateEnabled, disabled } = this.props;
const selector = activeStateUnit;
const isHoverable = hoverStateEnabled && !disabled;

if (isHoverable) {
hover.on(this.widgetRef,
() => !this._active && (this._hovered = true),
() => this._hovered = false,
{ selector, namespace },
);

return () => hover.off(this.widgetRef, { selector, namespace });
}

return null;
}

@Effect()
activeEffect() {
const {
Expand All @@ -280,10 +228,10 @@ export default class Widget extends JSXComponent<WidgetInput> {
this._active = false;
onInactive?.(event);
}, {
showTimeout: _feedbackShowTimeout,
hideTimeout: _feedbackHideTimeout,
selector,
namespace,
selector,
showTimeout: _feedbackShowTimeout,
},
);

Expand All @@ -293,6 +241,16 @@ export default class Widget extends JSXComponent<WidgetInput> {
return null;
}

@Effect()
clickEffect() {
const { name, clickArgs } = this.props;
const namespace = name;

dxClick.on(this.widgetRef, () => this.props.onClick!(clickArgs), { namespace });

return () => dxClick.off(this.widgetRef, { namespace });
}

@Effect()
focusEffect() {
const { disabled, focusStateEnabled, name } = this.props;
Expand All @@ -304,8 +262,8 @@ export default class Widget extends JSXComponent<WidgetInput> {
e => !e.isDefaultPrevented() && (this._focused = true),
e => !e.isDefaultPrevented() && (this._focused = false),
{
namespace,
isFocusable: el => focusable(null, el),
namespace,
},
);

Expand All @@ -316,13 +274,23 @@ export default class Widget extends JSXComponent<WidgetInput> {
}

@Effect()
clickEffect() {
const { name, clickArgs } = this.props;
const namespace = name;
hoverEffect() {
const namespace = 'UIFeedback';
const { activeStateUnit, hoverStateEnabled, disabled } = this.props;
const selector = activeStateUnit;
const isHoverable = hoverStateEnabled && !disabled;

dxClick.on(this.widgetRef, () => this.props.onClick!(clickArgs), { namespace });
if (isHoverable) {
hover.on(this.widgetRef,
() => !this._active && (this._hovered = true),
() => this._hovered = false,
{ selector, namespace },
);

return () => dxClick.off(this.widgetRef, { namespace });
return () => hover.off(this.widgetRef, { selector, namespace });
}

return null;
}

@Effect()
Expand All @@ -338,4 +306,36 @@ export default class Widget extends JSXComponent<WidgetInput> {

return null;
}

@Effect()
resizeEffect() {
const namespace = `${this.props.name}VisibilityChange`;
const { onDimensionChanged } = this.props;

if (onDimensionChanged) {
resize.on(this.widgetRef, onDimensionChanged, { namespace });

return () => resize.off(this.widgetRef, { namespace });
}

return null;
}

@Effect()
visibilityEffect() {
const { name, onVisibilityChange } = this.props;

const namespace = `${name}VisibilityChange`;
if (onVisibilityChange) {
visibility.on(this.widgetRef,
() => onVisibilityChange!(true),
() => onVisibilityChange!(false),
{ namespace },
);

return () => visibility.off(this.widgetRef, { namespace });
}

return null;
}
}
14 changes: 14 additions & 0 deletions testing/jest/button.tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ describe('Button', () => {
});
});

describe('visible', () => {
it('should pass the default value into Widget component', () => {
const tree = render();

expect(tree.find(Widget).prop('visible')).toBe(true);
});

it('should pass the custom value into Widget component', () => {
const tree = render({ visible: false });

expect(tree.find(Widget).prop('visible')).toBe(false);
});
});

describe('focusStateEnabled', () => {
it('should pass a default value into Widget component', () => {
const button = render();
Expand Down
9 changes: 7 additions & 2 deletions testing/jest/utils/events-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ export const EVENT = {
click: 'click',
dxClick: 'dxclick',
focus: 'focusin',
hiding: 'dxhiding',
hoverEnd: 'dxhoverend',
hoverStart: 'dxhoverstart',
inactive: 'dxinactive'
inactive: 'dxinactive',
shown: 'dxshown',
};

export const defaultEvent = {
Expand Down Expand Up @@ -77,5 +79,8 @@ eventsEngine.on = (...args) => {
});
};

eventsEngine.off = (el, event) => eventHandlers[event] = [];
eventsEngine.off = (...args) => {
const event = args[1].split('.')[0];
eventHandlers[event] = [];
};
keyboard.off = id => delete keyboardHandlers[id];
Loading