import React, { useCallback, useContext, useState } from 'react'
import PropTypes from 'prop-types'
import { ColumnType, LayoutType } from './prop-types'
import {
ResizerContext,
START_RESIZING,
END_RESIZING,
RESIZE
} from './reducers/resizerReducer'
import { DEFAULT_MIN_WIDTH, ConfigContext } from './Table'
import { measureCols } from './utils'
import { TableDispatch, COLUMN_RESIZING } from './actions'
import './HeaderResizer.css'
const HeaderResizer = props => {
const {
index,
column,
layout: { rule }
} = props
const { id, minWidth = DEFAULT_MIN_WIDTH } = column
const [timeStamp, setTimeStamp] = useState(0)
const resizerDispatch = useContext(ResizerContext)
const dispatch = useContext(TableDispatch)
const { rowIdAttr, context } = useContext(ConfigContext)
const handleAutoresize = useCallback(
event => {
const { minWidth = DEFAULT_MIN_WIDTH } = column
const columns = [{ ...column, autoresize: true, index, minWidth }]
const section = event.target.closest('.rrt-section')
if (section) {
const metrics = measureCols(
context,
columns,
section,
section.classList.contains('rrt-section-scrollable') ? '' : rowIdAttr
)
metrics.forEach(metric => {
const {
column: { id, width }
} = metric
if (metric.width !== width) {
dispatch({ type: COLUMN_RESIZING, id, width: metric.width })
}
})
}
},
[rowIdAttr, index, context, dispatch]
)
const handleResize = useCallback(
event => {
event.stopPropagation()
event.preventDefault()
const table = event.target.closest('.rrt-container')
const { left } = table.getBoundingClientRect()
const x0 = event.clientX - left
let x1
let clampedWidth
Copyright 2020 Ulrich Gaal
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.