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: refactor actions in button #12274

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions js/renovation/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ export default class Button extends JSXComponent<ButtonInput> {
@Ref() contentRef!: HTMLDivElement;
@Ref() submitInputRef!: HTMLInputElement;

@Effect()
contentReadyEffect() {
// NOTE: we should trigger this effect on change each
// property upon which onContentReady depends
// (for example, text, icon, etc)
const { onContentReady } = this.props;

onContentReady?.({ element: this.contentRef.parentNode });
}

onActive(event: Event) {
const { useInkRipple } = this.props;
const config = getInkRippleConfig(this.props);
Expand Down
42 changes: 30 additions & 12 deletions js/renovation/dist/button.j.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import registerComponent from '../../core/component_registrator';
import Widget from '../preact-wrapper/component';
import { extend } from '../../core/utils/extend';
import ButtonView from '../button.p';
import { wrapElement } from '../preact-wrapper/utils';
import { wrapElement, getInnerActionName } from '../preact-wrapper/utils';
import { useLayoutEffect } from 'preact/hooks';
import { getPublicElement } from '../../core/utils/dom';

const TEMPLATE_WRAPPER_CLASS = 'dx-template-wrapper';

const actions = {
onClick: {
excludeValidators: ['readOnly'],
afterExecute: function() {
const { useSubmitBehavior } = this.option();

useSubmitBehavior && setTimeout(() => this._submitInput().click());
} },
onContentReady: { excludeValidators: ['disabled', 'readOnly'] },
};

class Button extends Widget {
getView() {
return ButtonView;
Expand Down Expand Up @@ -42,17 +53,8 @@ class Button extends Widget {
};
}

props.onClick = this._createActionByOption('onClick', {
excludeValidators: ['readOnly'],
afterExecute: () => {
const { useSubmitBehavior } = this.option();

useSubmitBehavior && setTimeout(() => this._submitInput().click());
}
});

props.onContentReady = this._createActionByOption('onContentReady', {
excludeValidators: ['disabled', 'readOnly']
Object.keys(actions).forEach((name) => {
props[name] = this.option(getInnerActionName(name));
});

return props;
Expand All @@ -69,6 +71,22 @@ class Button extends Widget {
text: '',
});
}

_init() {
super._init();

Object.keys(actions).forEach((name) => {
this._addAction(name, actions[name]);
});
}

_optionChanged(option) {
if(actions[option.name]) {
this._addAction(option.name, actions[option.name]);
}

super._optionChanged();
}
}

registerComponent('Button', Button);
Expand Down
5 changes: 5 additions & 0 deletions js/renovation/preact-wrapper/component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Widget from '../../ui/widget/ui.widget';
import * as Preact from 'preact';
import { extend } from '../../core/utils/extend';
import { getInnerActionName } from './utils';

export default class PreactWrapper extends Widget {
getInstance() {
Expand Down Expand Up @@ -51,6 +52,10 @@ export default class PreactWrapper extends Widget {
this._invalidate();
}

_addAction(name, config) {
this.option(getInnerActionName(name), this._createActionByOption(name, config));
}

_refresh() {
this._renderComponent();
}
Expand Down
4 changes: 4 additions & 0 deletions js/renovation/preact-wrapper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export const wrapElement = ($element, $wrapper) => {

return children;
};

export const getInnerActionName = (actionName) => {
return actionName.charAt(2).toLowerCase() + actionName.substr(3) + 'Action';
};
5 changes: 0 additions & 5 deletions js/renovation/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ export default class Widget extends JSXComponent<WidgetInput> {
return () => dxClick.off(this.widgetRef, { namespace });
}

@Effect()
contentReadyEffect() {
this.props.onContentReady?.({});
}

@Effect()
focusEffect() {
const { disabled, focusStateEnabled, name } = this.props;
Expand Down