Skip to content

Commit

Permalink
WRQ-31515: Fixed Scroller, VirtualList, and VirtualGridList to set pr…
Browse files Browse the repository at this point in the history
…op value to default when undefined is passed for the prop value (#3268)

* WRQ-31515: Fixed Scroller and VirtualList to set prop value to default value when undefined is passed for the prop value

Enact-DCO-1.0-Signed-off-by: Juwon Jeong ([email protected])

* Added unit test

Enact-DCO-1.0-Signed-off-by: Juwon Jeong ([email protected])

* Apply review

Enact-DCO-1.0-Signed-off-by: Juwon Jeong ([email protected])
  • Loading branch information
juwonjeong authored Aug 29, 2024
1 parent d1c4064 commit fcb7e6a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The following is a curated list of changes in the Enact core module, newest changes on the top.

## [unreleased]

### Added

- `core/util` function `setDefaultProps` to set props that are missing or `undefined` to default values

## [4.9.0] - 2024-07-17

No significant changes.
Expand Down
28 changes: 27 additions & 1 deletion packages/core/util/tests/util-specs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {forwardRef, memo, lazy} from 'react';

import {cap, clamp, coerceArray, coerceFunction, extractAriaProps, isRenderable, memoize, mergeClassNameMaps, mapAndFilterChildren, shallowEqual} from '../util';
import {cap, clamp, coerceArray, coerceFunction, extractAriaProps, isRenderable, memoize, mergeClassNameMaps, mapAndFilterChildren, setDefaultProps, shallowEqual} from '../util';

describe('util', () => {
describe('cap', () => {
Expand Down Expand Up @@ -255,6 +255,32 @@ describe('util', () => {
});
});

describe('setDefaultProps', () => {
const props = {
// eslint-disable-next-line no-undefined
direction: undefined,
index: 0,
size: 'small'
};
const defaultProps = {
direction: 'below',
selected: true,
size: 'large'
};

test('should set props that are missing or `undefined` to default values', () => {
const expected = {
direction: 'below',
index: 0,
selected: true,
size: 'small'
};
const actual = setDefaultProps(props, defaultProps);

expect(expected).toEqual(actual);
});
});

describe('shallowEqual', () => {
const child = {
name: 'child'
Expand Down
25 changes: 25 additions & 0 deletions packages/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,30 @@ const mapAndFilterChildren = (children, callback, filter) => {
}
};

/**
* Sets props that are missing or `undefined` to default values
*
* @function
* @param {Obejct} props Props object
* @param {Obejct} defaultProps Default value object
*
* @returns {Object} Props with default values
* @memberof core/util
* @public
*/
const setDefaultProps = (props, defaultProps = {}) => {
const result = Object.assign({}, props);

for (const prop in defaultProps) {
// eslint-disable-next-line no-undefined
if (props[prop] === undefined) {
result[prop] = defaultProps[prop];
}
}

return result;
};

/**
* Performs shallow comparison for given objects.
*
Expand Down Expand Up @@ -317,5 +341,6 @@ export {
mergeClassNameMaps,
perfNow,
mapAndFilterChildren,
setDefaultProps,
shallowEqual
};
1 change: 1 addition & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The following is a curated list of changes in the Enact ui module, newest change
### Fixed

- `ui/Marquee.MarqueeController` to start animation properly when `marqueeOnFocus` is set to `true` and text changed
- `ui/Scroller` and `ui/VirtualList` to have default prop when `undefined` prop is passed

## [4.9.0] - 2024-07-17

Expand Down
3 changes: 2 additions & 1 deletion packages/ui/Scroller/Scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @exports ScrollerBasic
*/

import {setDefaultProps} from '@enact/core/util';
import PropTypes from 'prop-types';

import {ResizeContext} from '../Resizable';
Expand Down Expand Up @@ -51,7 +52,7 @@ const scrollerDefaultProps = {
const Scroller = (props) => {
// Hooks

const scrollerProps = Object.assign({}, scrollerDefaultProps, props);
const scrollerProps = setDefaultProps(props, scrollerDefaultProps);

const {
scrollContentHandle,
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/VirtualList/VirtualList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @exports VirtualListBasic
*/

import {setDefaultProps} from '@enact/core/util';
import PropTypes from 'prop-types';

import {ResizeContext} from '../Resizable';
Expand Down Expand Up @@ -50,7 +51,7 @@ const virtualListDefaultProps = {
const VirtualList = (props) => {
// Hooks

const virtualListProps = Object.assign({}, virtualListDefaultProps, props);
const virtualListProps = setDefaultProps(props, virtualListDefaultProps);

const {
scrollContentHandle,
Expand Down Expand Up @@ -326,7 +327,7 @@ const virtualGridListDefaultProps = {
* @public
*/
const VirtualGridList = (props) => {
const virtualGridListProps = Object.assign({}, virtualGridListDefaultProps, props);
const virtualGridListProps = setDefaultProps(props, virtualGridListDefaultProps);

const {
scrollContentHandle,
Expand Down

0 comments on commit fcb7e6a

Please sign in to comment.