Created
October 19, 2017 06:57
-
-
Save potato4d/7f349deb8354603e25345d097a96fa57 to your computer and use it in GitHub Desktop.
Vue.jsのpropsカスタムバリデーターを使った堅牢なコンポーネント作成 ref: http://qiita.com/potato4d/items/1b92df0cbf9b0b6cf8d6
This file contains hidden or 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
export const accountKeys = [ | |
'id', | |
'name', | |
'bio' | |
]; |
This file contains hidden or 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
import { hasKeys } from 'path/to/ObjectTypeCheck'; | |
import { accountKeys } from './AccountInfo.type'; | |
export default { | |
props: { | |
account: { | |
validator(val) { | |
return hasKeys(val, accountKeys); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
./LikeButton.vue | |
./LikeButton.type.js |
This file contains hidden or 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
./AccountInfo.vue | |
./AccountInfo.type.js |
This file contains hidden or 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
export function hasKeys(obj, keys) { | |
return !keys.find((key)=>(!obj.hasOwnProperty(key))) | |
} |
This file contains hidden or 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
export default { | |
props: { | |
count: Number, | |
size: String | |
} | |
} |
This file contains hidden or 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
export default { | |
props: { | |
count: Number, | |
size: { | |
type: String, | |
validator (val) { | |
return ['small', 'medium'].includes(val) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
export default { | |
props: { | |
count: Number, | |
size: { | |
type: String, | |
validator (val) { | |
return ['small', 'medium'].includes(val) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
export default { | |
props: { | |
count: Number, | |
size: String | |
} | |
} |
This file contains hidden or 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
export default { | |
props: ['count', 'size'] | |
} |
This file contains hidden or 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
export default { | |
props: { | |
count: { | |
type: Number, | |
required: true | |
}, | |
size: { | |
type: String, | |
default: 'meduim', | |
validator (val) { | |
return ['small', 'medium'].includes(val) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment