* Saves single value to the data array. * * @param {number} row Visual row index. * @param {number|string} prop The column property. * @param {string} value The value to set.
(row: number, prop: string | number, value: unknown)
| 904 | * @param {string} value The value to set. |
| 905 | */ |
| 906 | set(row: number, prop: string | number, value: unknown) { |
| 907 | const physicalRow = this.hot!.toPhysicalRow(row); |
| 908 | let newValue = value; |
| 909 | let dataRow: Record<string | number, unknown> = this.dataSource![physicalRow] as Record<string | number, unknown>; |
| 910 | // TODO: To remove, use 'modifyData' hook instead (see below) |
| 911 | const modifiedRowData = this.hot!.runHooks('modifyRowData', physicalRow); |
| 912 | |
| 913 | dataRow = typeof modifiedRowData !== 'number' ? modifiedRowData as Record<string | number, unknown> : dataRow; |
| 914 | // |
| 915 | |
| 916 | if (this.hot!.hasHook('modifyData')) { |
| 917 | const valueHolder = createObjectPropListener(newValue); |
| 918 | |
| 919 | this.hot!.runHooks('modifyData', row, this.propToCol(prop), valueHolder, 'set'); |
| 920 | |
| 921 | if (valueHolder.isTouched()) { |
| 922 | newValue = valueHolder.value; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | const { dataDotNotation } = this.hot!.getSettings(); |
| 927 | |
| 928 | // try to set value under property `prop` (includes dot) |
| 929 | if (dataRow && hasOwnProperty(dataRow, prop)) { |
| 930 | dataRow[prop] = newValue; |
| 931 | |
| 932 | } else if (dataDotNotation && typeof prop === 'string' && prop.indexOf('.') > -1) { |
| 933 | let out: Record<string, unknown> = dataRow; |
| 934 | let i = 0; |
| 935 | let ilen; |
| 936 | |
| 937 | const sliced = prop.split('.'); |
| 938 | |
| 939 | for (i = 0, ilen = sliced.length - 1; i < ilen; i++) { |
| 940 | if (sliced[i] === '__proto__' || sliced[i] === 'constructor' || sliced[i] === 'prototype') { |
| 941 | // Security: prototype-polluting is not allowed |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | if (typeof out[sliced[i]] === 'undefined') { |
| 946 | out[sliced[i]] = {}; |
| 947 | } |
| 948 | out = out[sliced[i]] as Record<string, unknown>; |
| 949 | } |
| 950 | |
| 951 | out[sliced[i]] = newValue; |
| 952 | } else if (typeof prop === 'function') { |
| 953 | (prop as (row: unknown, value: unknown) => void)( |
| 954 | this.dataSource!.slice(physicalRow, physicalRow + 1)[0], newValue); |
| 955 | |
| 956 | } else { |
| 957 | if (prop === '__proto__' || prop === 'constructor' || prop === 'prototype') { |
| 958 | // Security: prototype-polluting is not allowed |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | dataRow[prop] = newValue; |
| 963 | } |