Skip to content

Commit 0ed9e6d

Browse files
authored
Merge pull request #253 from db-ui/fix-input-defaultvalue
fix: input default value
2 parents 3627dbc + 8805099 commit 0ed9e6d

File tree

8 files changed

+71
-11
lines changed

8 files changed

+71
-11
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/components/src/components/input/input.lite.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export default function DBInput(props: DBInputProps) {
2222
const state = useStore<DBInputState>({
2323
mId: DEFAULT_ID,
2424
_isValid: undefined,
25+
_value: '',
26+
_placeholder: ' ', // placeholder can't be empty
27+
_label: 'LABEL SHOULD BE SET',
2528
handleChange: (event) => {
2629
if (props.onChange) {
2730
props.onChange(event);
@@ -30,6 +33,9 @@ export default function DBInput(props: DBInputProps) {
3033
props.change(event);
3134
}
3235

36+
// using controlled components for react forces us to using state for value
37+
state._value = event.target.value;
38+
3339
if (textInputRef?.validity?.valid != state._isValid) {
3440
state._isValid = textInputRef?.validity?.valid;
3541
if (props.validityChange) {
@@ -61,9 +67,22 @@ export default function DBInput(props: DBInputProps) {
6167
} else {
6268
state.mId = 'input-' + uuid();
6369
}
70+
71+
if (props.value) {
72+
state._value = props.value;
73+
}
74+
6475
if (props.stylePath) {
6576
state.stylePath = props.stylePath;
6677
}
78+
79+
if (props.placeholder) {
80+
state._placeholder = props.placeholder;
81+
}
82+
83+
if (props.label) {
84+
state._label = props.label;
85+
}
6786
});
6887

6988
return (
@@ -81,11 +100,11 @@ export default function DBInput(props: DBInputProps) {
81100
id={state.mId}
82101
name={props.name}
83102
type={props.type || 'text'}
84-
placeholder={props.placeholder}
103+
placeholder={state._placeholder}
85104
aria-labelledby={state.mId + '-label'}
86105
disabled={props.disabled}
87106
required={props.required}
88-
value={props.value}
107+
value={state._value}
89108
maxLength={props.maxLength}
90109
minLength={props.minLength}
91110
pattern={props.pattern}
@@ -97,7 +116,7 @@ export default function DBInput(props: DBInputProps) {
97116
htmlFor={state.mId}
98117
aria-hidden="true"
99118
id={state.mId + '-label'}>
100-
<span>{props.label}</span>
119+
<span>{state._label}</span>
101120
</label>
102121
<Show when={props.description}>
103122
<p className="description">{props.description}</p>

packages/components/src/components/input/input.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
}
229229

230230
& ~ .icon-input-state {
231+
// TODO replace with svg icon
231232
@include icon(glyph(search), 20, "filled", "before", false);
232233
}
233234

packages/components/src/components/input/model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export type DBInputProps = DBInputDefaultProps &
5454
export type DBInputDefaultState = {
5555
mId?: string;
5656
_isValid: boolean | undefined;
57+
_value: any;
58+
_placeholder: string;
59+
_label: string;
5760
};
5861

5962
export type DBInputState = DBInputDefaultState &

showcases/angular-current-showcase/src/app/app.component.html

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@ <h1>Angular</h1>
66
<DBButton text="Test" icon="account" (click)="onClick()"></DBButton>
77
<DBIcon icon="account"></DBIcon>
88

9-
<section class="db-ui-expressive">
10-
<DBInput></DBInput>
11-
</section>
9+
<form class="db-ui-expressive" (ngSubmit)="onSubmitForm($event)">
10+
<DBInput
11+
[(ngModel)]="formData.username"
12+
name="username"
13+
label="Textinput"
14+
placeholder="Platzhalter"
15+
description="Beschreibung"
16+
value="{{ formData.username }}"
17+
iconBefore="edit"
18+
ngDefaultControl
19+
></DBInput>
20+
<input
21+
[(ngModel)]="formData.password"
22+
name="password"
23+
value="test"
24+
/>
25+
<DBButton variant="secondary">Senden</DBButton>
26+
</form>
1227

1328
<section class="db-ui-regular">
14-
<DBInput></DBInput>
29+
<DBInput label="Label"></DBInput>
1530
</section>
1631

1732
<section class="db-ui-functional">

showcases/angular-current-showcase/src/app/app.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ export class AppComponent {
1717
{ name: 'tab-bar-2', label: '2-Tab2', content: 'Content 2-2' }
1818
];
1919

20+
formData = {
21+
username: 'Test',
22+
password: '1234'
23+
};
24+
2025
onClick() {
2126
// eslint-disable-next-line no-console
22-
console.log('click button');
27+
console.log('click event');
28+
}
29+
30+
onSubmitForm(event: any): void {
31+
event.preventDefault();
32+
// eslint-disable-next-line no-console
33+
console.log('on submit Form', this.formData);
2334
}
2435
}

showcases/angular-current-showcase/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
23
import { BrowserModule } from '@angular/platform-browser';
34

45
import {
@@ -18,7 +19,8 @@ import { AppComponent } from './app.component';
1819
DBIconModule,
1920
DBDividerModule,
2021
DBCardModule,
21-
DBInputModule
22+
DBInputModule,
23+
FormsModule
2224
],
2325
providers: [],
2426
schemas: [NO_ERRORS_SCHEMA],

showcases/react-showcase/src/app.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const App = () => {
1515
console.log('click event test');
1616
};
1717

18+
const onSubmit = (event: any) => {
19+
event.preventDefault();
20+
// eslint-disable-next-line no-console
21+
console.log('Submit Form', event);
22+
};
23+
1824
return (
1925
<DBPage
2026
type="fixedHeaderFooter"
@@ -57,14 +63,15 @@ const App = () => {
5763
gap: '1rem',
5864
margin: '1rem 0'
5965
}}>
60-
<section className="db-ui-expressive">
66+
<form className="db-ui-expressive" onSubmit={onSubmit}>
6167
<DBInput
6268
description="Das ist die Beschreibung"
6369
label="Startbahnhof eingeben"
6470
placeholder="irgendein Text"
6571
iconBefore="edit"
6672
variant="error"
6773
value="hello"
74+
name="testInput"
6875
/>
6976

7077
<DBInput
@@ -83,7 +90,8 @@ const App = () => {
8390
type="datetime-local"
8491
id="input-expr-date"
8592
/>
86-
</section>
93+
<DBButton variant="secondary">Test</DBButton>
94+
</form>
8795

8896
<section className="db-ui-regular">
8997
<DBInput

0 commit comments

Comments
 (0)