Skip to content

Commit

Permalink
fix(CellEditContainer, Grid): Fix related to selection on edit
Browse files Browse the repository at this point in the history
Using method select instead of setSelectionRange for select all as first method throws error for
input type other than text or textarea
  • Loading branch information
pdudhat committed Oct 7, 2020
1 parent 187e18a commit d02b404
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/CellEditContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class CellEditContainer extends React.Component {
// console.log(this.node.focus)
if (this.node && this.node.focus) {
this.node.focus()
if (this.node.setSelectionRange) {
this.node.setSelectionRange(0, this.node.value.length)
if (this.node.select) {
this.node.select()
// this.node.setSelectionRange(0, this.node.value.length)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ class Grid extends React.PureComponent {
width: header.width,
[COL_IDENT_ATTRIBUTE]: header.ident,
onClick: this.props.sortEnabled ? this.columnHeaderClick : undefined,
sortOrder: this.props.sortEnabled ? sortOrderOf(header)(this.state.sortOptions) : undefined,
sortOrder: this.props.sortEnabled ? sortOrderOf(header)(this.sortOptions()) : undefined,
'data-column-index': index,
...rest,
})
Expand Down
5 changes: 3 additions & 2 deletions src/RowEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ class RowEditor extends React.Component {
if (!prevProps.isEditing && this.props.isEditing) {
if (this.focusNode) {
if (this.focusNode) this.focusNode.focus()
if (this.focusNode.setSelectionRange) {
this.focusNode.setSelectionRange(0, this.focusNode.value.length)
if (this.focusNode.select) {
this.focusNode.select()
// this.focusNode.setSelectionRange(0, this.focusNode.value.length)
}
}
// eslint-disable-next-line react/no-did-update-set-state
Expand Down
2 changes: 1 addition & 1 deletion src/stories/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const unitDataFormatter = ({ value }) => unitMap[value] || value

/* prettier-ignore */
export const headers = [
dateCol({ ident: 'transDate', display: 'Trans-Date', width:120, isKey:true, }),
dateCol({ ident: 'transDate', display: 'Trans-Date', width:140, isKey:true, }),
strCol({ ident: 'unitId', display: 'Unit', width:180, isKey:true, dataFormatter:unitDataFormatter,choices:unitChoices
, acceptRawText : true , setInvalidMessage : ({value}) => !unitMap[value] && value !== 'prakash' && 'Please Enter Valid Data.' }),
intCol({ ident: 'he', display: 'HE', width: 40,isKey:true, numFormat:"0", }),
Expand Down
23 changes: 22 additions & 1 deletion src/stories/virtualized/virtualized.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Grid, {
VirtualizedCell,
} from '../../index'
import { createData, headers } from '../data'
import { CellInputEditor } from '../../Components'

const customizedCellRender = params => {
const { gridToolProps, reactVirtualizedProps, ...rest } = params
Expand Down Expand Up @@ -54,9 +55,29 @@ const customizedCellRender = params => {
return defaultVirtualizedCellRender(params)
}

export const dateInputCellEditRender = ({ getInputProps }) => (
<CellInputEditor type="date" {...getInputProps({ refKey: 'innerRef' })} />
)

const data = createData(200)
storiesOf('Virtualized grid', module)
.add('Basic', () => <Grid data={data} headers={headers} render={virtualizedGridRenderer()} />)
.add('Basic', () => (
<Grid
data={data}
headers={headers}
render={virtualizedGridRenderer({
cellRender: props => {
const type = props.gridToolProps.headers[props.reactVirtualizedProps.columnIndex].type
return defaultVirtualizedCellRender({
...props,
editRender: (type === 'date-time' || type === 'date') && dateInputCellEditRender,
})
},
})}
editMode="cell"
isEditable={() => true}
/>
))
.add('Fixed Col and Free edit', () => (
<GridToolContext.Provider value={{ columnHeaderProps: { backgroundColor: 'pink' } }}>
<Grid
Expand Down
2 changes: 1 addition & 1 deletion src/virtualized/virtualizedRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Provider } from './VirtualizedContext'
import { defaultCellRender, cellRenderWrapper, defaultRowHeaderRender } from './cellRender'
import GridToolsContext from '../context'

const totalColWidth = cols => cols.map(c => c.width).reduce((c1, c2) => c1 + c2)
const totalColWidth = cols => cols.map(c => c.width).reduce((c1, c2) => c1 + c2, 0)

const colWidthOf = cols => ({ index }) => cols[index].width

Expand Down

0 comments on commit d02b404

Please sign in to comment.