lodash-es#padStart TypeScript Examples
The following examples show how to use
lodash-es#padStart.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimeSelect.tsx From UUI with MIT License | 4 votes |
TimeSelect = UUIFunctionComponent({
name: 'TimeSelect',
nodes: {
Root: 'div',
SelectZone: 'div',
Separator: 'div',
OptionList: 'div',
Option: 'div',
},
propTypes: TimeSelectPropTypes,
}, (props: TimeSelectFeatureProps, { nodes, NodeDataProps, ref }) => {
const {
Root, SelectZone, Separator,
OptionList, Option,
} = nodes
const allOptions = useMemo(() => {
return {
hours: range(0, 24),
minutes: range(0, 60),
seconds: range(0, 60),
}
}, [])
const activeOptionValue = {
hours: props.value.getHours(),
minutes: props.value.getMinutes(),
seconds: props.value.getSeconds(),
}
const [disableHandleScroll, setDisableHandleScroll] = useState(false)
const hourListRef = useRef<HTMLDivElement | null>(null)
const minuteListRef = useRef<HTMLDivElement | null>(null)
const secondListRef = useRef<HTMLDivElement | null>(null)
const getItemHeight = useCallback((target: HTMLElement) => {
const styles = window.getComputedStyle(target)
const optionHeightPx = styles.getPropertyValue('--option-height')
return Number(optionHeightPx.replace('px', ''))
}, [])
const scrollToValue = useCallback((value: Date, animate?: boolean) => {
setDisableHandleScroll(true)
const targetScrollTo = (ref: React.MutableRefObject<HTMLDivElement | null>, value: number, animate?: boolean) => {
const target = ref.current as HTMLElement
const itemHeight = getItemHeight(target)
target.scrollTo({ top: value * itemHeight, behavior: animate ? "smooth" : "auto" })
}
targetScrollTo(hourListRef, value.getHours(), animate)
targetScrollTo(minuteListRef, value.getMinutes(), animate)
targetScrollTo(secondListRef, value.getSeconds(), animate)
setTimeout(() => {
setDisableHandleScroll(false)
}, 500)
}, [getItemHeight])
useImperativeHandle(ref, () => {
return {
scrollToValue: scrollToValue,
}
})
const scrollTo = useCallback((target: HTMLElement, top: number) => {
target.scrollTo({ top, behavior: "smooth" })
}, [])
const debouncedScrollOnChange = useRef({
hours: debounce(scrollTo, 300),
minutes: debounce(scrollTo, 300),
seconds: debounce(scrollTo, 300),
})
const handleScroll = useCallback((type: TimeSelectType) => {
if (disableHandleScroll) return;
const options = allOptions[type]
return (event: React.UIEvent<HTMLDivElement, UIEvent>) => {
const target = event.target as HTMLElement
const itemHeight = getItemHeight(target)
const scrollTop = target.scrollTop
const currentIndex = Math.round((scrollTop) / itemHeight)
const newValue = options[currentIndex];
props.onChange(set(props.value, { [type]: newValue }))
debouncedScrollOnChange.current[type](target, currentIndex * itemHeight)
}
}, [allOptions, disableHandleScroll, getItemHeight, props])
return (
<Root>
{TimeSelectTypeArray.map((type, index) => {
return (
<React.Fragment key={type}>
{index !== 0 && (
<Separator>:</Separator>
)}
<OptionList
ref={[hourListRef, minuteListRef, secondListRef][index]}
key={`option-list-${type}`}
onScroll={handleScroll(type)}
>
{allOptions[type].map((option) => {
const active = activeOptionValue[type] === option
return (
<Option
{...NodeDataProps({
'active': active,
})}
key={`${type}-${option}`}
onClick={() => {
const newValue = set(props.value, { [type]: option })
props.onChange(newValue)
scrollToValue(newValue)
}}
>{padStart(String(option), 2, '0')}</Option>
)
})}
</OptionList>
</React.Fragment>
)
})}
<SelectZone />
</Root>
)
})