docs(PasswordInput): use composition api

This commit is contained in:
chenjiahan 2020-12-13 19:28:58 +08:00
parent b31d605ed4
commit e787fe731e
3 changed files with 129 additions and 83 deletions

View File

@ -33,11 +33,16 @@ app.use(NumberKeyboard);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
const value = ref('123');
const showKeyboard = ref(true);
return { return {
value: '123', value,
showKeyboard: true, showKeyboard,
}; };
}, },
}; };
@ -96,22 +101,27 @@ Use `info` to set info message, use `error-info` prop to set error message.
``` ```
```js ```js
import { ref, watch } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref('123');
value: '123', const errorInfo = ref('');
errorInfo: '', const showKeyboard = ref(true);
showKeyboard: true,
}; watch(value, (newVal) => {
}, if (newVal.length === 6 && newVal !== '123456') {
watch: { errorInfo.value = 'Password Mistake';
value(value) {
if (value.length === 6 && value !== '123456') {
this.errorInfo = 'Password Mistake';
} else { } else {
this.errorInfo = ''; errorInfo.value = '';
} }
}, });
return {
value,
errorInfo,
showKeyboard,
};
}, },
}; };
``` ```

View File

@ -37,11 +37,16 @@ app.use(NumberKeyboard);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
const value = ref('123');
const showKeyboard = ref(true);
return { return {
value: '123', value,
showKeyboard: true, showKeyboard,
}; };
}, },
}; };
@ -106,22 +111,27 @@ export default {
``` ```
```js ```js
import { ref, watch } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref('123');
value: '123', const errorInfo = ref('');
errorInfo: '', const showKeyboard = ref(true);
showKeyboard: true,
}; watch(value, (newVal) => {
}, if (newVal.length === 6 && newVal !== '123456') {
watch: { errorInfo.value = '密码错误';
value(value) {
if (value.length === 6 && value !== '123456') {
this.errorInfo = '密码错误';
} else { } else {
this.errorInfo = ''; errorInfo.value = '';
} }
}, });
return {
value,
errorInfo,
showKeyboard,
};
}, },
}; };
``` ```

View File

@ -52,54 +52,58 @@
/> />
</template> </template>
<script> <script lang="ts">
import { ComponentPublicInstance, reactive, ref, toRefs, watch } from 'vue';
import { useTranslate } from '@demo/use-translate';
const i18n = {
'zh-CN': {
info: '密码为 6 位数字',
showInfo: '提示信息',
addGutter: '格子间距',
errorInfo: '密码错误',
removeMask: '明文展示',
customLength: '自定义长度',
},
'en-US': {
info: 'Some tips',
showInfo: 'Show Info',
addGutter: 'Add Gutter',
errorInfo: 'Password Mistake',
removeMask: 'Remove Mask',
customLength: 'Custom Length',
},
};
export default { export default {
i18n: { setup() {
'zh-CN': { const t = useTranslate(i18n);
info: '密码为 6 位数字', const initialValue = {
showInfo: '提示信息', showInfo: '123',
addGutter: '格子间距', addGutter: '123',
errorInfo: '密码错误', basicUsage: '123',
removeMask: '明文展示', removeMask: '123',
customLength: '自定义长度', customLength: '123',
},
'en-US': {
info: 'Some tips',
showInfo: 'Show Info',
addGutter: 'Add Gutter',
errorInfo: 'Password Mistake',
removeMask: 'Remove Mask',
customLength: 'Custom Length',
},
},
data() {
return {
value: {
showInfo: '123',
addGutter: '123',
basicUsage: '123',
removeMask: '123',
customLength: '123',
},
current: 'basicUsage',
errorInfo: '',
}; };
},
watch: { const refMap = {
current(value) { showInfo: ref<ComponentPublicInstance>(),
if (value) { addGutter: ref<ComponentPublicInstance>(),
const el = this.$refs[value].$el; basicUsage: ref<ComponentPublicInstance>(),
const { top } = el.getBoundingClientRect(); removeMask: ref<ComponentPublicInstance>(),
window.scrollTo(0, window.pageYOffset + top); customLength: ref<ComponentPublicInstance>(),
} };
},
},
methods: { type ValueKeys = keyof typeof initialValue;
onInput(key) {
const { value, current } = this; const state = reactive({
value: initialValue,
current: 'basicUsage' as ValueKeys,
errorInfo: '',
});
const onInput = (key: ValueKeys) => {
const { value, current } = state;
const maxlegnth = current === 'customLength' ? 4 : 6; const maxlegnth = current === 'customLength' ? 4 : 6;
const newValue = (value[current] + key).slice(0, maxlegnth); const newValue = (value[current] + key).slice(0, maxlegnth);
@ -110,23 +114,45 @@ export default {
newValue.length === 6 && newValue.length === 6 &&
newValue !== '123456' newValue !== '123456'
) { ) {
this.errorInfo = this.t('errorInfo'); state.errorInfo = t('errorInfo');
} }
}, };
onDelete() {
const { value, current } = this; const onDelete = () => {
const { value, current } = state;
value[current] = value[current].slice(0, value[current].length - 1); value[current] = value[current].slice(0, value[current].length - 1);
if (current === 'showInfo') { if (current === 'showInfo') {
this.errorInfo = ''; state.errorInfo = '';
} }
}, };
watch(
() => state.current,
(value) => {
if (value) {
const vm = refMap[value].value;
if (vm) {
const { top } = vm.$el.getBoundingClientRect();
window.scrollTo(0, window.pageYOffset + top);
}
}
}
);
return {
...toRefs(state),
...refMap,
t,
onInput,
onDelete,
};
}, },
}; };
</script> </script>
<style lang="less"> <style lang="less">
.demo-password-input { .demo-password-input {
min-height: 140vh; min-height: 150vh;
} }
</style> </style>