Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit 91c0353

Browse files
committed
fix managed inputs
1 parent 48f0bcd commit 91c0353

3 files changed

Lines changed: 61 additions & 32 deletions

File tree

kitchen-sink/components/pages/FormsPage.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const pStyle = {margin: '1em 0'};
1111
export interface IFormsPageState {
1212
birthDate: string;
1313
radioSelected: number;
14+
name: string;
1415
}
1516

1617
export class FormsPage extends React.Component<any, IFormsPageState> {
@@ -21,7 +22,8 @@ export class FormsPage extends React.Component<any, IFormsPageState> {
2122

2223
this.state = {
2324
birthDate: '2014-04-30',
24-
radioSelected: 1
25+
radioSelected: 1,
26+
name: ''
2527
}
2628

2729
this.fw7 = getFramework7();
@@ -34,6 +36,20 @@ export class FormsPage extends React.Component<any, IFormsPageState> {
3436
});
3537
}
3638

39+
private onBirthDateChange(event, value) {
40+
this.setState({
41+
...this.state,
42+
birthDate: value
43+
});
44+
}
45+
46+
private onNameChange(event) {
47+
this.setState({
48+
...this.state,
49+
name: event.target.value
50+
});
51+
}
52+
3753
render() {
3854
return (
3955
<Page>
@@ -43,7 +59,7 @@ export class FormsPage extends React.Component<any, IFormsPageState> {
4359
<List form>
4460
<ListItem>
4561
<FormLabel>Name</FormLabel>
46-
<FormInput type="text" placeholder="Name" />
62+
<FormInput type="text" placeholder="Name" onChange={this.onNameChange.bind(this)} value={this.state.name}/>
4763
</ListItem>
4864
<ListItem>
4965
<FormLabel>Password</FormLabel>
@@ -63,7 +79,7 @@ export class FormsPage extends React.Component<any, IFormsPageState> {
6379
</ListItem>
6480
<ListItem>
6581
<FormLabel>Birth date</FormLabel>
66-
<FormInput type="date" placeholder="Birth date" value={this.state.birthDate} />
82+
<FormInput type="date" placeholder="Birth date" value={this.state.birthDate} onChange={this.onBirthDateChange.bind(this)} />
6783
</ListItem>
6884
<ListItem>
6985
<FormLabel>Date time</FormLabel>
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
import * as React from 'react';
2-
3-
export interface IManagedFormInputState {
4-
currentValue?: string
5-
}
6-
7-
export class ManagedFormInput extends React.Component<any, IManagedFormInputState> {
8-
constructor(props: any) {
9-
super(props);
10-
11-
this.state = {
12-
currentValue: this.props.value || ""
13-
}
14-
}
15-
16-
render() {
17-
return (
18-
<input {...this.props} onChange={this.onInput.bind(this)} value={this.state.currentValue}/>
19-
);
20-
}
21-
22-
private onInput(event) {
23-
if (this.props.type === "radio" || this.props.type === "checkbox") {
24-
this.props.onChange();
25-
} else {
26-
this.setState({
27-
currentValue: event.target.value
28-
});
29-
}
2+
import {SelectableInput} from './SelectableInput';
3+
4+
export const ManagedFormInput = (props: any) => {
5+
switch(props.type.toLowerCase()) {
6+
case 'checkbox':
7+
case 'radio':
8+
return <SelectableInput type={props.type} checked={props.checked} onChange={props.onChange} />;
9+
default:
10+
return <input type={props.type} onClick={props.onClick} onChange={props.onChange} value={props.value} placeholder={props.placeholder} />;
3011
}
31-
};
12+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
3+
export interface ISelectableInputProps {
4+
checked?: boolean
5+
onChange?: () => void
6+
type: string
7+
}
8+
9+
export class SelectableInput extends React.Component<ISelectableInputProps, any> {
10+
private element: HTMLInputElement;
11+
12+
private saveRef(element: HTMLInputElement) {
13+
this.element = element;
14+
}
15+
16+
public componentDidMount() {
17+
this.element.checked = this.props.checked;
18+
this.element.addEventListener('change', this.props.onChange);
19+
}
20+
21+
public componentDidUpdate() {
22+
setTimeout(() => {
23+
if (this.element) {
24+
this.element.checked = this.props.checked;
25+
}
26+
}, 50);
27+
}
28+
29+
public render() {
30+
return <input type={this.props.type} ref={this.saveRef.bind(this)} />;
31+
}
32+
}

0 commit comments

Comments
 (0)