Skip to content

Commit

Permalink
Merge pull request #193 from gcanti/187
Browse files Browse the repository at this point in the history
allow to set a default value in Android date picker template + other fix
  • Loading branch information
gcanti authored Jul 10, 2016
2 parents de3d306 + b74c9a8 commit 24afc1d
Show file tree
Hide file tree
Showing 5 changed files with 1,078 additions and 1,011 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
**Note**: Gaps between patch versions are faulty/broken releases.

## v0.5.2

- **New Feature**
- add support for unions, fix #118 (@gcanti)
- add support for lists, fix #80 (@gcanti)
- **Bug Fix**
- allow to set a default value in Android date picker template, fix #187

## v0.5.1

- **New Feature**
Expand Down
112 changes: 89 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,51 @@ var options = {

This will completely skip the rendering of the component, while the default value will be available for validation purposes.

# Unions

**Code Example**

```js
const AccountType = t.enums.of([
'type 1',
'type 2',
'other'
], 'AccountType')

const KnownAccount = t.struct({
type: AccountType
}, 'KnownAccount')

// UnknownAccount extends KnownAccount so it owns also the type field
const UnknownAccount = KnownAccount.extend({
label: t.String,
}, 'UnknownAccount')

// the union
const Account = t.union([KnownAccount, UnknownAccount], 'Account')

// the final form type
const Type = t.list(Account)

const options = {
item: [ // one options object for each concrete type of the union
{
label: 'KnownAccount'
},
{
label: 'UnknownAccount'
}
]
}
```

Generally `tcomb`'s unions require a `dispatch` implementation in order to select the suitable type constructor for a given value and this would be the key in this use case:

```js
// if account type is 'other' return the UnknownAccount type
Account.dispatch = value => value && value.type === 'other' ? UnknownAccount : KnownAccount
```

# Lists

You can handle a list with the `t.list` combinator:
Expand Down Expand Up @@ -996,9 +1041,40 @@ const Person = t.struct({
const Persons = t.list(Person);
```

# Managing unions
## Internationalization

You can override the default language (english) with the `i18n` option:

```js
const options = {
i18n: {
optional: ' (optional)',
required: '',
add: 'Add', // add button
remove: '', // remove button
up: '', // move up button
down: '' // move down button
}
};
```

## Buttons configuration

You can prevent operations on lists with the following options:

- `disableAdd`: (default `false`) prevents adding new items
- `disableRemove`: (default `false`) prevents removing existing items
- `disableOrder`: (default `false`) prevents sorting existing items

```js
const options = {
disableOrder: true
};
```

## List with Dynamic Items (Different structs based on selected value)

**Example**
Lists of different types are not supported. This is because a `tcomb`'s list, by definition, contains only values of the same type. You can define a union though:

```js
const AccountType = t.enums.of([
Expand All @@ -1011,33 +1087,23 @@ const KnownAccount = t.struct({
type: AccountType
}, 'KnownAccount')

const UnknownAccount = t.struct({
type: AccountType,
label: t.String
// UnknownAccount extends KnownAccount so it owns also the type field
const UnknownAccount = KnownAccount.extend({
label: t.String,
}, 'UnknownAccount')

// the union type
// the union
const Account = t.union([KnownAccount, UnknownAccount], 'Account')

// you must define a dispatch function returning the suitable type
// based on the current form value
Account.dispatch = value => {
return value && value.type === 'other' ? UnknownAccount : KnownAccount
}
// the final form type
const Type = t.list(Account)
```

const Type = Account
Generally `tcomb`'s unions require a `dispatch` implementation in order to select the suitable type constructor for a given value and this would be the key in this use case:

// options can be a list of options, one for each type of the union
const options = [
// options of the KnownAccount type
{
label: 'KnownAccount'
},
// options of theUnknownAccount type
{
label: 'UnknownAccount'
}
]
```js
// if account type is 'other' return the UnknownAccount type
Account.dispatch = value => value && value.type === 'other' ? UnknownAccount : KnownAccount
```

# Customizations
Expand Down
16 changes: 14 additions & 2 deletions lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ class Component extends React.Component {
this.setState({hasError: false});
}

pureValidate() {
return t.validate(this.getValue(), this.props.type, this.getValidationOptions());
}

validate() {
const result = t.validate(this.getValue(), this.props.type, this.getValidationOptions());
const result = this.pureValidate();
this.setState({hasError: !result.isValid()});
return result;
}
Expand Down Expand Up @@ -225,10 +229,19 @@ class Textbox extends Component {
return placeholder;
}

getKeyboardType() {
const keyboardType = this.props.options.keyboardType;
if (t.Nil.is(keyboardType) && this.typeInfo.innerType === t.Number) {
return 'numeric';
}
return keyboardType;
}

getLocals() {
const locals = super.getLocals();
locals.placeholder = this.getPlaceholder();
locals.onChangeNative = this.props.options.onChange;
locals.keyboardType = this.getKeyboardType();

[
'help',
Expand All @@ -237,7 +250,6 @@ class Textbox extends Component {
'autoFocus',
'blurOnSubmit',
'editable',
'keyboardType',
'maxLength',
'multiline',
'onBlur',
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/bootstrap/datepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function datepicker(locals) {
}
});
} else {
DatePickerAndroid.open({date: new Date()})
DatePickerAndroid.open({date: locals.value || new Date()})
.then(function (date) {
if (date.action !== DatePickerAndroid.dismissedAction) {
var newDate = new Date(date.year, date.month, date.day);
Expand Down
Loading

0 comments on commit 24afc1d

Please sign in to comment.