Skip to content

Commit 6bcc85d

Browse files
committed
Create Neo.component.Image #7244
1 parent 58b8cb9 commit 6bcc85d

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

.github/ISSUE/ticket-add-release-notes-to-kb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
GH ticket id: #7241
44

55
**Assignee:** Gemini
6-
**Status:** To Do
6+
**Status:** Done
77

88
## Description
99

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Ticket: Create Neo.component.Image
2+
3+
GH ticket id: #7244
4+
5+
**Assignee:** Gemini
6+
**Status:** Done
7+
8+
## Description
9+
10+
Create a new class, `Neo.component.Image`, to provide a classic Object-Oriented Programming (OOP) interface for the `<img>` HTML tag. This component will serve as a foundational building block and demonstrate best practices for reactive configs.
11+
12+
## Rationale
13+
14+
While an `<img>` tag can be created directly in a VDOM tree, a dedicated component class offers significant advantages:
15+
16+
- **Reactivity:** It provides reactive `src` and `alt` configs, allowing these properties to be updated via data binding.
17+
- **Lazy Loading:** The reactive `src` config is a key enabler for advanced behaviors like lazy loading images (e.g., based on an Intersection Observer) by updating the `src` at runtime.
18+
- **Encapsulation:** It encapsulates image-specific logic and provides a clear, reusable API.
19+
20+
## Scope of Work
21+
22+
1. Create a new file: `src/component/Image.mjs`.
23+
2. The new `Image` class should extend `Neo.component.Base`.
24+
3. The `static config` should include:
25+
- `className`: `Neo.component.Image`
26+
- `ntype`: `image`
27+
- `tag`: `'img'` (This will inherit reactivity from `Neo.component.Base`)
28+
- `alt_`: `null` (reactive)
29+
- `src_`: `null` (reactive)
30+
4. Implement `afterSetAlt()` and `afterSetSrc()` hooks to update the component's VDOM when the `alt` and `src` configs are changed.
31+
32+
## Acceptance Criteria
33+
34+
- The `Neo.component.Image` class is created at the specified path.
35+
- The component correctly renders an `<img>` tag.
36+
- Changing the `src` and `alt` configs programmatically updates the rendered `<img>` tag's attributes.

src/component/Image.mjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Component from './Base.mjs';
2+
3+
/**
4+
* @summary A component to render an `<img>` tag with reactive `src` and `alt` attributes.
5+
*
6+
* This component provides a simple, object-oriented wrapper for the native `<img>` element,
7+
* allowing its `src` and `alt` text to be updated dynamically via the config system.
8+
*
9+
* This is a key component for data-binding scenarios, such as displaying an image from a store record
10+
* or implementing lazy-loading by updating the `src` config at runtime.
11+
*
12+
* @example
13+
* // Basic usage
14+
* Neo.create({
15+
* module: Image,
16+
* alt : 'A beautiful landscape',
17+
* src : 'https://www.neomjs.com/resources/images/logo.png'
18+
* });
19+
*
20+
* @class Neo.component.Image
21+
* @extends Neo.component.Base
22+
*/
23+
class Image extends Component {
24+
static config = {
25+
/**
26+
* @member {String} className='Neo.component.Image'
27+
* @protected
28+
*/
29+
className: 'Neo.component.Image',
30+
/**
31+
* @member {String} ntype='image'
32+
* @protected
33+
*/
34+
ntype: 'image',
35+
/**
36+
* The alt attribute for the image.
37+
* @member {String|null} alt_=null
38+
* @reactive
39+
*/
40+
alt_: null,
41+
/**
42+
* The src attribute for the image.
43+
* @member {String|null} src_=null
44+
* @reactive
45+
*/
46+
src_: null,
47+
/**
48+
* The HTML tag for this component.
49+
* @member {String} tag='img'
50+
* @reactive
51+
*/
52+
tag: 'img'
53+
}
54+
55+
/**
56+
* Hook that fires after the `alt` config has been changed.
57+
* Updates the `alt` attribute in the component's VDOM.
58+
* @param {String|null} value The new value
59+
* @param {String|null} oldValue The old value
60+
* @protected
61+
*/
62+
afterSetAlt(value, oldValue) {
63+
this.changeVdomRootKey('alt', value)
64+
}
65+
66+
/**
67+
* Hook that fires after the `src` config has been changed.
68+
* Updates the `src` attribute in the component's VDOM.
69+
* @param {String|null} value The new value
70+
* @param {String|null} oldValue The old value
71+
* @protected
72+
*/
73+
afterSetSrc(value, oldValue) {
74+
this.changeVdomRootKey('src', value)
75+
}
76+
}
77+
78+
export default Neo.setupClass(Image);

0 commit comments

Comments
 (0)