Skip to content

Commit 3d48a29

Browse files
authored
Merge pull request #1017 from navikt/textarea-react-component
Textarea react component
2 parents 8aad8b8 + 3b64521 commit 3d48a29

File tree

13 files changed

+138
-33
lines changed

13 files changed

+138
-33
lines changed

.prettierignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ package-lock.json
22

33
# Ignore yaml files because it contains handlebars logic
44
**/.nais/*.yaml
5-
**/.nais/*.yml
5+
**/.nais/*.yml
6+
7+
## Ignore all EJS files:
8+
**/*.ejs

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"test:coverage": "yarn workspaces run test:coverage",
3636
"cypress:bygger": "yarn workspace @navikt/bygger-frontend cypress",
3737
"cypress:fyllut": "yarn workspace @navikt/fyllut-frontend cypress",
38+
"cypress:bygger:open": "yarn workspace @navikt/bygger-frontend cypress:open",
39+
"cypress:fyllut:open": "yarn workspace @navikt/fyllut-frontend cypress:open",
3840
"check-types": "yarn workspaces run check-types",
3941
"mocks:fyllut": "cd mocks && yarn mocks:fyllut",
4042
"mocks:fyllut:no-cli": "cd mocks && yarn mocks:fyllut:no-cli",

packages/bygger/cypress/e2e/form-builder.spec.cy.ts

+13
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ describe('Form Builder', () => {
139139
cy.findByRole('checkbox', { name: val2 }).should('exist');
140140
});
141141

142+
it('Edit JSON - Change label', () => {
143+
cy.findByRole('group', { name: 'Flervalg' })
144+
.should('exist')
145+
.closest("[data-testid='builder-component']")
146+
.within(() => {
147+
cy.clickBuilderComponentButton('Rediger JSON');
148+
});
149+
cy.get('div').contains('"label": "Flervalg"').click({ force: true });
150+
cy.focused().type('{end}{leftArrow}{leftArrow} (endret)', { force: true });
151+
cy.get('[data-testid="editorSaveButton"]').click();
152+
cy.get('label').contains('Flervalg (endret)');
153+
});
154+
142155
// TODO: Add test for radio group when it gets the new data values.
143156
});
144157
});

packages/bygger/src/formio-overrides/webform-builder-overrides.js

+33
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@ WebformBuilder.prototype.editComponent = function (component, parent, isNew, isJ
5858
}
5959
}
6060
originalEditComponent.call(this, component, parent, isNew, isJsonEdit, original, flags);
61+
62+
if (isJsonEdit) {
63+
this.editForm.form = {
64+
...this.editForm.form,
65+
components: [
66+
{
67+
type: 'formioTextArea',
68+
as: 'json',
69+
editor: 'ace',
70+
input: true,
71+
key: 'componentJson',
72+
label: 'JSON definisjon',
73+
validate: {
74+
required: true,
75+
},
76+
},
77+
{
78+
type: 'checkbox',
79+
key: 'showFullSchema',
80+
label: 'Full definisjon',
81+
},
82+
],
83+
};
84+
85+
// Need to run this again to attach the edit components controls. In earlier FormioJS versions this was not necessary
86+
this.attachEditComponentControls(
87+
component,
88+
parent,
89+
isNew,
90+
original,
91+
NavFormioJs.Components.components[component.type],
92+
);
93+
}
6194
};
6295

6396
WebformBuilder.prototype.destroy = function (...args) {

packages/bygger/src/translations/global/ApplicationTextTranslationEditPanel.test.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('ApplicationTextTranslationEditPanel', () => {
4242
});
4343

4444
it('renders all grensesnitt inputs', () => {
45-
expect(screen.getAllByRole('textbox')).toHaveLength(67);
45+
expect(screen.getAllByRole('textbox')).toHaveLength(69);
4646
});
4747

4848
it('renders originalText with translatedText as value', () => {

packages/shared-components/src/formio/components/base/editForm/shared/editFormAceEditor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component } from '@navikt/skjemadigitalisering-shared-domain';
22

33
const editFormAceEditor = (type: 'html' | 'javascript' | 'json'): Component => {
44
return {
5-
type: 'textarea',
5+
type: 'formioTextArea',
66
key: 'aceEditor',
77
label: '',
88
rows: 5,

packages/shared-components/src/formio/components/base/editForm/shared/editFormWysiwygEditor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const editFormWysiwygEditor = (heading: boolean = true): Component => {
99
toolbar.push('bold', 'link', 'bulletedList', 'numberedList');
1010

1111
return {
12-
type: 'textarea',
12+
type: 'formioTextArea',
1313
key: 'wysiwygEditor',
1414
label: '',
1515
rows: 5,

packages/shared-components/src/formio/components/core/textarea/TextArea.ts

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Textarea as NavTextarea } from '@navikt/ds-react';
2+
import { TEXTS } from '@navikt/skjemadigitalisering-shared-domain';
3+
import BaseComponent from '../../base/BaseComponent';
4+
import textAreaBuilder from './TextArea.builder';
5+
import textAreaForm from './TextArea.form';
6+
7+
const { navTextarea: TRANSLATIONS } = TEXTS.grensesnitt;
8+
9+
class TextArea extends BaseComponent {
10+
static schema() {
11+
return BaseComponent.schema({
12+
label: 'Tekstområde',
13+
type: 'textarea',
14+
key: 'textarea',
15+
fieldSize: 'input--xxl',
16+
input: true,
17+
dataGridLabel: true,
18+
clearOnHide: true,
19+
autoExpand: true,
20+
editor: '',
21+
});
22+
}
23+
24+
static editForm() {
25+
return textAreaForm();
26+
}
27+
28+
static get builderInfo() {
29+
return textAreaBuilder();
30+
}
31+
32+
getMinRows() {
33+
return this.component?.rows;
34+
}
35+
36+
getMaxRows() {
37+
return !this.component?.autoExpand ? this.getMinRows() : undefined;
38+
}
39+
40+
getMaxLength() {
41+
const maxLength = this.component?.validate?.maxLength;
42+
return maxLength;
43+
}
44+
45+
renderReact(element) {
46+
element.render(
47+
<>
48+
<NavTextarea
49+
id={this.getId()}
50+
defaultValue={this.getValue()}
51+
maxLength={this.getMaxLength()}
52+
minRows={this.getMinRows()}
53+
maxRows={this.getMaxRows()}
54+
ref={(ref) => this.setReactInstance(ref)}
55+
onChange={(event) => this.updateValue(event.currentTarget.value, { modified: true })}
56+
label={this.getLabel()}
57+
hideLabel={this.getHideLabel()}
58+
description={this.getDescription()}
59+
className={this.getClassName()}
60+
autoComplete={this.getAutoComplete()}
61+
readOnly={this.getReadOnly()}
62+
spellCheck={this.getSpellCheck()}
63+
error={this.getError()}
64+
i18n={{
65+
counterLeft: this.t(TRANSLATIONS.counterLeft),
66+
counterTooMuch: this.t(TRANSLATIONS.counterTooMuch),
67+
}}
68+
/>
69+
</>,
70+
);
71+
}
72+
}
73+
74+
export default TextArea;

packages/shared-components/src/formio/components/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import FormioTextArea from 'formiojs/components/textarea/TextArea';
12
import Activities from './core/activities/Activities';
23
import Alert from './core/alert/Alert';
34
import Button from './core/button/Button';
@@ -46,6 +47,7 @@ const customComponents = {
4647
datagrid: DataGrid,
4748
textfield: TextField,
4849
textarea: TextArea,
50+
formioTextArea: FormioTextArea,
4951
number: Number,
5052
navSkjemagruppe: FormGroup,
5153
selectboxes: SelectBoxes,

packages/shared-components/src/formio/template/templates/navdesign/field/form.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% if (ctx.component.type === "textfield" || ctx.component.type === "orgNr" || ctx.component.type === "bankAccount" || ctx.component.type === "htmlelement" || ctx.component.type === "radiopanel" || ctx.component.type === "activities") { %}
1+
{% if (ctx.component.type === "textfield" || ctx.component.type === "textarea" || ctx.component.type === "orgNr" || ctx.component.type === "bankAccount" || ctx.component.type === "htmlelement" || ctx.component.type === "radiopanel" || ctx.component.type === "activities") { %}
22
{{ctx.element}}
33
{% } else { %}
44
{% if (ctx.component.description && ctx.component.type !== "datagrid" && ctx.component.descriptionPosition === "above") { %}

packages/shared-domain/src/form/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export interface Component {
160160
valueComponent?: Component;
161161
isInline?: boolean;
162162
textDisplay?: 'form' | 'formPdf' | 'pdf';
163+
autoExpand?: boolean;
163164
}
164165

165166
export interface ComponentProperties {
@@ -187,6 +188,7 @@ export interface ComponentValidate {
187188
patternMessage?: string;
188189
min?: number;
189190
max?: number;
191+
maxLength?: number;
190192
}
191193

192194
export interface ComponentConditional {

packages/shared-domain/src/texts/grensesnitt.js

+4
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ export const grensesnitt = {
8181
inputHasFocus: '{{label}} har fokus.',
8282
onFilterSearchResult: 'for søkeordet {{inputValue}}.',
8383
},
84+
navTextarea: {
85+
counterLeft: 'tegn igjen',
86+
counterTooMuch: 'tegn for mye',
87+
},
8488
};

0 commit comments

Comments
 (0)