Skip to content

Commit 9e784b9

Browse files
committed
initial commit
0 parents  commit 9e784b9

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Form: Profile Name
2+
3+
Set a user name (modal form)
4+
5+
![screenshot.png](screenshot.png)
6+
7+
```jsx
8+
import ModalBtn from 'patchkit-modal/btn'
9+
import FormProfileName from 'patchkit-form-profile-name'
10+
11+
const onSubmit = (name, cb) => { console.log('submit', name); cb() }
12+
<ModalBtn className="fullheight" Form={FormProfileName} formProps={{currentValue: 'bob', className: 'text-center vertical-center', onSubmit: onSubmit}}>
13+
<a className="btn highlighted">Click to open</a>
14+
</ModalBtn>
15+
```

demo.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
import ModalBtn from 'patchkit-modal/btn'
3+
import FormProfileName from './index'
4+
5+
export default class FormProfileNameDemo extends React.Component {
6+
render() {
7+
const onSubmit = (name, cb) => { console.log('submit', name); cb() }
8+
return <div>
9+
<h1>patchkit-form-profile-name</h1>
10+
<section className="form-profile-name">
11+
<header>&lt;FormProfileName&gt;</header>
12+
<div className="content">
13+
<ModalBtn className="fullheight" Form={FormProfileName} formProps={{currentValue: 'bob', className: 'text-center vertical-center', onSubmit: onSubmit}}><a className="btn highlighted">Click to open</a></ModalBtn>
14+
</div>
15+
</section>
16+
</div>
17+
}
18+
}

index.jsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react'
2+
3+
export default class FormProfileName extends React.Component {
4+
static propTypes = {
5+
setIsValid: React.PropTypes.func.isRequired,
6+
onSubmit: React.PropTypes.func.isRequired,
7+
currentValue: React.PropTypes.string,
8+
className: React.PropTypes.string
9+
}
10+
11+
constructor(props) {
12+
super(props)
13+
this.state = this.validate(this.props.currentValue||'', true)
14+
}
15+
16+
componentDidMount() {
17+
this.validate(this.state.name) // emit isValid update
18+
}
19+
20+
onChangeName(e) {
21+
this.setState(this.validate(e.target.value))
22+
}
23+
24+
validate (name, supressEmit) {
25+
let badNameCharsRegex = /[^A-z0-9\._-]/
26+
const emit = (b) => { !supressEmit && this.props.setIsValid(b) }
27+
if (!name.trim()) {
28+
emit(false)
29+
return { error: false, isValid: false, name: name }
30+
} else if (badNameCharsRegex.test(name)) {
31+
emit(false)
32+
return {
33+
name: name,
34+
error: 'We\'re sorry, your name can only include A-z 0-9 . _ - and cannot have spaces.',
35+
isValid: false
36+
}
37+
} else if (name.slice(-1) == '.') {
38+
emit(false)
39+
return {
40+
name: name,
41+
error: 'We\'re sorry, your name cannot end with a period.',
42+
isValid: false
43+
}
44+
} else {
45+
emit(true)
46+
return {
47+
name: name,
48+
error: false,
49+
isValid: true
50+
}
51+
}
52+
}
53+
54+
submit(cb) {
55+
this.props.onSubmit(this.state.name, cb)
56+
}
57+
58+
render() {
59+
return <div className={this.props.className}>
60+
<h1><span>What would you like to be called?</span></h1>
61+
<form className="block" onSubmit={e=>e.preventDefault()}>
62+
<fieldset>
63+
<div>
64+
<label>
65+
<input type="text" onChange={this.onChangeName.bind(this)} value={this.state.name} />
66+
{ this.state.error ? <p className="error">{this.state.error}</p> : '' }
67+
</label>
68+
</div>
69+
</fieldset>
70+
</form>
71+
</div>
72+
}
73+
}

package.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "patchkit-form-profile-name",
3+
"version": "1.0.0",
4+
"description": "Set a user name (modal form)",
5+
"main": "index.jsx",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/patchkit/patchkit-form-profile-name.git"
12+
},
13+
"keywords": [
14+
"react"
15+
],
16+
"author": "Paul Frazee <[email protected]>",
17+
"license": "GPL-3.0",
18+
"bugs": {
19+
"url": "https://github.com/patchkit/patchkit-form-profile-name/issues"
20+
},
21+
"homepage": "https://github.com/patchkit/patchkit-form-profile-name#readme",
22+
"dependencies": {
23+
"react": "^0.14.6"
24+
},
25+
"devDependencies": {
26+
"babel": "^6.3.26",
27+
"babel-preset-es2015": "^6.3.13",
28+
"babel-preset-react": "^6.3.13",
29+
"babel-preset-stage-0": "^6.3.13",
30+
"babelify": "^7.2.0",
31+
"browserify": "^13.0.0",
32+
"patchkit-modal": "^1.0.3"
33+
},
34+
"browserify": {
35+
"transform": [
36+
[
37+
"envify",
38+
{
39+
"global": true
40+
}
41+
],
42+
[
43+
"babelify",
44+
{
45+
"presets": [
46+
"es2015",
47+
"stage-0",
48+
"react"
49+
]
50+
}
51+
]
52+
]
53+
}
54+
}

screenshot.png

29.6 KB
Loading

0 commit comments

Comments
 (0)