|
| 1 | +import { |
| 2 | + defineComponent, |
| 3 | + h, |
| 4 | + inject, |
| 5 | + onMounted, |
| 6 | + onUpdated, |
| 7 | + onUnmounted, |
| 8 | + computed, |
| 9 | +} from '@vue/runtime-core' |
| 10 | +import type { PropType } from '@vue/runtime-core' |
| 11 | +import { colorize } from '../renderer/textColor' |
| 12 | +import type { ForegroundColorProp } from '../renderer/textColor' |
| 13 | +import { scheduleUpdateSymbol } from '../injectionSymbols' |
| 14 | + |
| 15 | +const FIGURES = { |
| 16 | + basic: '█', |
| 17 | + shade: '▓', |
| 18 | +} as const |
| 19 | + |
| 20 | +type FigureType = keyof typeof FIGURES |
| 21 | + |
| 22 | +export const TuiProgressBar = defineComponent({ |
| 23 | + name: 'TuiProgressBar', |
| 24 | + |
| 25 | + props: { |
| 26 | + color: { |
| 27 | + required: false, |
| 28 | + default: 'blue', |
| 29 | + type: String as PropType<ForegroundColorProp>, |
| 30 | + }, |
| 31 | + bgColor: { |
| 32 | + required: false, |
| 33 | + default: 'white', |
| 34 | + type: String as PropType<ForegroundColorProp>, |
| 35 | + }, |
| 36 | + width: { |
| 37 | + required: false, |
| 38 | + default: 25, |
| 39 | + type: Number, |
| 40 | + }, |
| 41 | + value: { |
| 42 | + required: true, |
| 43 | + type: Number, |
| 44 | + }, |
| 45 | + type: { |
| 46 | + required: false, |
| 47 | + type: String as PropType<FigureType>, |
| 48 | + default: 'basic', |
| 49 | + }, |
| 50 | + }, |
| 51 | + |
| 52 | + setup(props) { |
| 53 | + const scheduleUpdate = inject(scheduleUpdateSymbol)! |
| 54 | + |
| 55 | + onMounted(scheduleUpdate) |
| 56 | + |
| 57 | + onUpdated(scheduleUpdate) |
| 58 | + |
| 59 | + onUnmounted(scheduleUpdate) |
| 60 | + |
| 61 | + const content = computed(() => { |
| 62 | + const type = FIGURES[props.type] |
| 63 | + const w = Math.floor(props.value * (props.width / 100)) |
| 64 | + const bg = colorize(type, props.bgColor, 'foreground') |
| 65 | + const fg = colorize(type, props.color, 'foreground') |
| 66 | + return fg.repeat(w) + bg.repeat(props.width - w) |
| 67 | + }) |
| 68 | + |
| 69 | + return () => { |
| 70 | + return h('tui:text', content.value) |
| 71 | + } |
| 72 | + }, |
| 73 | +}) |
0 commit comments