forked from subschema/subschema
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
juspears
committed
Dec 1, 2015
1 parent
807b07a
commit d774273
Showing
4 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
var {decorators, PropTypes, tutils} = Subschema; | ||
var {provide} = decorators; | ||
var {extend} = tutils; | ||
|
||
//This adds it to the loader, loader.addType still works. | ||
@provide.type | ||
class SwitchButton extends React.Component { | ||
//Prevents form-control from being passed to className. | ||
static inputClassName = ' '; | ||
|
||
static propTypes = { | ||
//This tells subschema to not process e.target.value, but just take the value. | ||
onChange: PropTypes.valueEvent, | ||
//Normal React.PropTypes | ||
onText: React.PropTypes.string, | ||
offText: React.PropTypes.string, | ||
value: React.PropTypes.oneOfType([React.PropTypes.bool, React.PropTypes.oneOf(['on', 'off', 0, 1])]) //Values can be (true, 1, '1', 'ON') | ||
} | ||
|
||
static defaultProps = { | ||
onText: "ON", | ||
offText: "OFF" | ||
} | ||
//In case you have "special" value handling. | ||
isChecked(value) { | ||
return value === true || value === 1 || value === 'on'; | ||
} | ||
|
||
//This is bound to the object instance | ||
handleClick = (e)=> { | ||
//This updates the valueManager | ||
this.props.onChange(this.isChecked(this.props.value) ? '' : 'on'); | ||
} | ||
|
||
render() { | ||
var props = this.props; | ||
var isChecked = this.isChecked(props.value); | ||
|
||
//you prolly won't do it this way, but use classes instead, but the demo platform | ||
// has its limitations. | ||
var container = extend({}, styles.container, isChecked ? styles.on : styles.off); | ||
var button = extend({}, styles.button, isChecked ? styles.buttonOn : styles.buttonOff); | ||
|
||
return <div className={props.className} style={styles.fieldContainer}> | ||
<div style={container} onClick={this.handleClick}> | ||
<input name={props.name} type="hidden" value={this.props.value}/> | ||
{isChecked === true ? props.onText : props.offText} | ||
<span style={button}/> | ||
</div> | ||
</div> | ||
} | ||
|
||
} | ||
|
||
//Normally you would do this via CSS but the demo can't load css dynamically, so this a workaround. | ||
var styles = { | ||
fieldContainer: { | ||
display: 'block', | ||
width: '100%', | ||
height: '34px', | ||
padding: '6px 12px', | ||
fontSize: '14px', | ||
lineHeight: '1.42857143', | ||
color: '#555', | ||
backgroundColor: '#fff' | ||
}, | ||
container: { | ||
position: 'relative', | ||
borderRadius: "11px", | ||
backgroundColor: '#fff', | ||
border: 'inset 2px', | ||
boxSizing: 'border-box', | ||
display: 'inline-block' | ||
}, | ||
on: { | ||
color: 'white', | ||
backgroundColor: 'blue', | ||
paddingLeft: '20px', | ||
paddingRight: '6px', | ||
|
||
}, | ||
off: { | ||
paddingLeft: '6px', | ||
paddingRight: '20px' | ||
}, | ||
button: { | ||
top: 2, | ||
display: 'inline-block', | ||
height: '16px', | ||
width: '16px', | ||
boxSizing: 'border-box', | ||
borderRadius: '8px', | ||
border: '1px outset #fff', | ||
position: 'absolute', | ||
backgroundColor: '#ccc', | ||
transition: 'all .2s', | ||
|
||
}, | ||
buttonOn: { | ||
left: 1 | ||
}, | ||
buttonOff: { | ||
left: '100%', | ||
marginLeft: '-18px', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
description: 'Shows how to create a custom type', | ||
schema: { | ||
"schema": { | ||
"areYouSure": { | ||
"type": "SwitchButton", | ||
"onText": "On", | ||
"offText": "Off", | ||
"title": "Are you sure?" | ||
} | ||
}, | ||
}, | ||
data: { | ||
areYouSure: true | ||
}, | ||
errors: { | ||
name: [{ | ||
message: 'Name is already taken' | ||
}] | ||
}, | ||
setupTxt: require('!!raw!./CustomType-setup.js') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,4 +277,4 @@ describe('Form', function () { | |
expect(count).toBe(2); | ||
}) | ||
|
||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {React, into,TestUtils,expect,byTypes, byId, select, Simulate} from '../support'; | ||
import Subschema, {Form, types, ValueManager} from 'Subschema'; | ||
|
||
var Select = types.Select; | ||
var CustomTypeSetup = require('../../public/samples/CarMake-setup.js'); | ||
var CustomType = require('../../public/samples/CarMake.js'); | ||
|
||
describe('CustomType', function () { | ||
it('should render', function () { | ||
var schema = CustomType.schema; | ||
//loader, schema, Subschema, React | ||
var loader = Subschema.loaderFactory([Subschema.DefaultLoader]); | ||
expect(CustomTypeSetup).toExist('CarMake-setup should load'); | ||
var valueManager = ValueManager(); | ||
CustomTypeSetup(loader, schema, Subschema, React, valueManager); | ||
|
||
var form = into(<Form schema={schema} valueManager={valueManager}/>); | ||
|
||
|
||
}) | ||
}); |