Skip to content

Commit

Permalink
RefreshButton takes target instead of model (#3871)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwexler authored Dec 26, 2024
1 parent 5fb6b28 commit 4629590
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
`@xh/hoist/cmp/loadingindicator`.
* `TreeMap` and `SplitTreeMap` are now cross-platform and can be used in mobile applications.
Their import paths have changed from `@xh/hoist/desktop/cmp/treemap` to `@xh/hoist/cmp/treemap`.

* The `RefreshButton` `model` prop has been renamed `target` for clarity and consistency.
### 🎁 New Features

* Major Improvements to ViewManager component
Expand Down
27 changes: 13 additions & 14 deletions desktop/cmp/button/RefreshButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,34 @@
*
* Copyright © 2024 Extremely Heavy Industries Inc.
*/
import {hoistCmp, HoistModel, RefreshContextModel, useContextModel} from '@xh/hoist/core';
import {hoistCmp, Loadable, RefreshContextModel, useContextModel} from '@xh/hoist/core';
import '@xh/hoist/desktop/register';
import {Icon} from '@xh/hoist/icon';
import {errorIf, withDefault} from '@xh/hoist/utils/js';
import {button, ButtonProps} from './Button';
import {apiRemoved} from '@xh/hoist/utils/js';

export type RefreshButtonProps = ButtonProps<HoistModel>;
export interface RefreshButtonProps extends ButtonProps {
/** Object to refresh when clicked. */
target?: Loadable;
}

/**
* Convenience Button preconfigured for use as a trigger for a refresh operation.
*
* If an onClick handler is provided it will be used. Otherwise this button will
* be linked to any model in props with LoadSupport enabled, or the contextual
* If an onClick handler is provided it will be used. Otherwise, this button will
* be linked to the target in props with LoadSupport enabled, or the contextual
* See {@link RefreshContextModel}.
*/
export const [RefreshButton, refreshButton] = hoistCmp.withFactory<RefreshButtonProps>({
displayName: 'RefreshButton',
model: false, // For consistency with all other buttons -- the model prop here could be replaced by 'target'
model: false,

render({model, onClick, ...props}, ref) {
const refreshContextModel = useContextModel(RefreshContextModel);
render({target, onClick, ...props}, ref) {
apiRemoved('model', {test: props.model, msg: 'Use target instead.'});

const refreshContextModel = useContextModel(RefreshContextModel);
if (!onClick) {
let target: HoistModel = model;
errorIf(
target && !target.loadSupport,
'Models provided to RefreshButton must enable LoadSupport.'
);
target = withDefault(target, refreshContextModel);
target ??= refreshContextModel;
onClick = target ? () => target.refreshAsync() : null;
}

Expand Down
2 changes: 1 addition & 1 deletion desktop/cmp/rest/impl/RestGridToolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const restGridToolbar = hoistCmp.factory({
omit: !model.gridModel.enableExport
}),
refreshButton({
model,
target: model,
omit: !model.showRefreshButton
})
);
Expand Down
2 changes: 1 addition & 1 deletion desktop/cmp/viewmanager/dialog/ManageDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const selectorPanel = hoistCmp.factory<ManageDialogModel>({
onFilterChange: f => (model.filter = f)
}),
filler(),
refreshButton({model})
refreshButton({target: model})
]
});
}
Expand Down
24 changes: 12 additions & 12 deletions mobile/cmp/button/RefreshButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
*
* Copyright © 2024 Extremely Heavy Industries Inc.
*/
import {hoistCmp, HoistModel, RefreshContextModel, useContextModel} from '@xh/hoist/core';
import {hoistCmp, Loadable, RefreshContextModel, useContextModel} from '@xh/hoist/core';
import {Icon} from '@xh/hoist/icon';
import {button, ButtonProps} from '@xh/hoist/mobile/cmp/button';
import '@xh/hoist/mobile/register';
import {errorIf} from '@xh/hoist/utils/js';
import {apiRemoved} from '@xh/hoist/utils/js';

export type RefreshButtonProps = ButtonProps<HoistModel>;
export interface RefreshButtonProps extends ButtonProps {
/** Object to refresh when clicked. */
target?: Loadable;
}

/**
* Convenience Button preconfigured for use as a trigger for a refresh operation.
Expand All @@ -21,18 +24,15 @@ export type RefreshButtonProps = ButtonProps<HoistModel>;
*/
export const [RefreshButton, refreshButton] = hoistCmp.withFactory<RefreshButtonProps>({
displayName: 'RefreshButton',
model: false, // For consistency with all other buttons -- the model prop here could be replaced by 'target'
model: false,

render({model, icon = Icon.sync(), onClick, ...props}) {
const refreshContextModel = useContextModel(RefreshContextModel);
render({target, icon = Icon.sync(), onClick, ...props}) {
apiRemoved('model', {test: props.model, msg: 'Use target instead.'});

const refreshContextModel = useContextModel(RefreshContextModel);
if (!onClick) {
errorIf(
model && !model.loadSupport,
'Models provided to RefreshButton must enable LoadSupport.'
);
model = model ?? refreshContextModel;
onClick = model ? () => model.refreshAsync() : null;
target ??= refreshContextModel;
onClick = target ? () => target.refreshAsync() : null;
}

return button({icon, onClick, ...props});
Expand Down

0 comments on commit 4629590

Please sign in to comment.