Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit eb1ce18

Browse files
authored
Merge pull request #546 from ckeditor/i/6109
Feature: Introduce `FormHeaderView` UI component. Closes ckeditor/ckeditor5#6109.
2 parents 5a7aca7 + 88e676b commit eb1ce18

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

src/formheader/formheaderview.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
/**
7+
* @module ui/formheader/formheaderview
8+
*/
9+
10+
import View from '../view';
11+
12+
import '../../theme/components/formheader/formheader.css';
13+
14+
/**
15+
* The class component representing a form header view. It should be used in more advanced forms to
16+
* describe the main purpose of the form.
17+
*
18+
* By default the component contains a bolded label view that has to be set. The label is usually a short (at most 3-word) string.
19+
* The component can also be extended by any other elements, like: icons, dropdowns, etc.
20+
*
21+
* It is used i.a.
22+
* by {@link module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView}
23+
* and {@link module:special-characters/ui/specialcharactersnavigationview~SpecialCharactersNavigationView}.
24+
*
25+
* The latter is an example, where the component has been extended by {@link module:ui/dropdown/dropdownview~DropdownView} view.
26+
*
27+
* @extends module:ui/view~View
28+
*/
29+
export default class FormHeaderView extends View {
30+
/**
31+
* Creates an instance of the form header class.
32+
*
33+
* @param {module:utils/locale~Locale} locale The locale instance.
34+
* @param {Object} options
35+
* @param {String} [options.label] A label.
36+
* @param {String} [options.class] An additional class.
37+
*/
38+
constructor( locale, options = {} ) {
39+
super( locale );
40+
41+
const bind = this.bindTemplate;
42+
43+
/**
44+
* The label of the header.
45+
*
46+
* @observable
47+
* @member {String} #label
48+
*/
49+
this.set( 'label', options.label || '' );
50+
51+
/**
52+
* An additional CSS class added to the {@link #element}.
53+
*
54+
* @observable
55+
* @member {String} #class
56+
*/
57+
this.set( 'class', options.class || null );
58+
59+
/**
60+
* A collection of header items.
61+
*
62+
* @readonly
63+
* @member {module:ui/viewcollection~ViewCollection}
64+
*/
65+
this.children = this.createCollection();
66+
67+
this.setTemplate( {
68+
tag: 'div',
69+
attributes: {
70+
class: [
71+
'ck',
72+
'ck-form__header',
73+
bind.to( 'class' )
74+
]
75+
},
76+
children: this.children
77+
} );
78+
79+
const label = new View( locale );
80+
81+
label.setTemplate( {
82+
tag: 'span',
83+
attributes: {
84+
class: [
85+
'ck',
86+
'ck-form__header__label'
87+
]
88+
},
89+
children: [
90+
{ text: bind.to( 'label' ) }
91+
]
92+
} );
93+
94+
this.children.add( label );
95+
}
96+
}

tests/formheader/formheaderview.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
import View from '../../src/view';
7+
import ViewCollection from '../../src/viewcollection';
8+
import FormHeaderView from '../../src/formheader/formheaderview';
9+
10+
describe( 'FormHeaderView', () => {
11+
let view, locale;
12+
13+
beforeEach( () => {
14+
locale = { t: val => val };
15+
view = new FormHeaderView( locale );
16+
view.render();
17+
} );
18+
19+
afterEach( () => {
20+
view.element.remove();
21+
} );
22+
23+
describe( 'constructor()', () => {
24+
it( 'should set view#locale', () => {
25+
expect( view.locale ).to.equal( locale );
26+
} );
27+
28+
it( 'should create view#children collection', () => {
29+
expect( view.children ).to.be.instanceOf( ViewCollection );
30+
expect( view.children ).to.have.length( 1 );
31+
} );
32+
33+
it( 'should set view#label', () => {
34+
expect( view.label ).to.equal( '' );
35+
} );
36+
37+
it( 'should set view#class', () => {
38+
expect( view.class ).to.be.null;
39+
} );
40+
41+
it( 'should set the template', () => {
42+
expect( view.element.classList.contains( 'ck' ) ).to.be.true;
43+
expect( view.element.classList.contains( 'ck-form__header' ) ).to.be.true;
44+
} );
45+
46+
describe( 'options', () => {
47+
it( 'should set view#class when class was passed', () => {
48+
const view = new FormHeaderView( locale, {
49+
class: 'foo'
50+
} );
51+
52+
expect( view.class ).to.equal( 'foo' );
53+
54+
view.destroy();
55+
} );
56+
57+
it( 'should use a label text when passed', () => {
58+
const view = new FormHeaderView( locale, {
59+
label: 'foo'
60+
} );
61+
62+
view.render();
63+
64+
expect( view.element.firstChild.classList.contains( 'ck' ) ).to.be.true;
65+
expect( view.element.firstChild.classList.contains( 'ck-form__header__label' ) ).to.be.true;
66+
expect( view.element.firstChild.textContent ).to.equal( 'foo' );
67+
68+
view.destroy();
69+
} );
70+
} );
71+
72+
describe( 'template bindings', () => {
73+
it( 'should bind #class to the template', () => {
74+
view.class = 'foo';
75+
expect( view.element.classList.contains( 'foo' ) ).to.be.true;
76+
} );
77+
78+
it( 'should bind #label to the template', () => {
79+
view.label = 'bar';
80+
expect( view.element.firstChild.textContent ).to.equal( 'bar' );
81+
82+
view.label = 'baz';
83+
expect( view.element.firstChild.textContent ).to.equal( 'baz' );
84+
} );
85+
86+
it( 'should bind #children to the template', () => {
87+
const child = new View();
88+
child.setTemplate( { tag: 'div' } );
89+
90+
view.children.add( child );
91+
92+
expect( view.element.childNodes[ 1 ] ).to.equal( child.element );
93+
94+
view.destroy();
95+
} );
96+
} );
97+
} );
98+
} );
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4+
*/
5+
6+
:root {
7+
--ck-table-form-header-height: 38px;
8+
}
9+
10+
.ck.ck-form__header {
11+
display: flex;
12+
flex-direction: row;
13+
flex-wrap: nowrap;
14+
align-items: center;
15+
justify-content: space-between;
16+
padding: var(--ck-spacing-small) var(--ck-spacing-large);
17+
height: var(--ck-table-form-header-height);
18+
line-height: var(--ck-table-form-header-height);
19+
border-bottom: 1px solid var(--ck-color-base-border);
20+
21+
& .ck-form__header__label {
22+
font-weight: bold;
23+
}
24+
}

0 commit comments

Comments
 (0)