mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.6 KiB
82 lines
2.6 KiB
3 years ago
|
import { UITypes } from 'nocodb-sdk'
|
||
|
import { isEmail, isValidURL } from '~/helpers'
|
||
|
|
||
3 years ago
|
const booleanOptions = [
|
||
|
{ checked: true, unchecked: false },
|
||
|
{ x: true, '': false },
|
||
|
{ yes: true, no: false },
|
||
|
{ y: true, n: false },
|
||
|
{ 1: true, 0: false },
|
||
|
{ '[x]': true, '[]': false, '[ ]': false },
|
||
|
{ '☑': true, '': false },
|
||
|
{ '✅': true, '': false },
|
||
|
{ '✓': true, '': false },
|
||
|
{ '✔': true, '': false },
|
||
|
{ enabled: true, disabled: false },
|
||
|
{ on: true, off: false },
|
||
3 years ago
|
{ done: true, '': false },
|
||
|
{ true: true, false: false }
|
||
3 years ago
|
]
|
||
|
const aggBooleanOptions = booleanOptions.reduce((obj, o) => ({ ...obj, ...o }), {})
|
||
3 years ago
|
|
||
|
const getColVal = (row, col = null) => {
|
||
|
return row && col ? row[col] : row
|
||
|
}
|
||
|
|
||
|
export const isCheckboxType = (values, col = null) => {
|
||
3 years ago
|
let options = booleanOptions
|
||
|
for (let i = 0; i < values.length; i++) {
|
||
3 years ago
|
const val = getColVal(values[i], col)
|
||
|
|
||
|
if (val === null || val === undefined || val.toString().trim() === '') {
|
||
|
continue
|
||
|
}
|
||
|
|
||
3 years ago
|
options = options.filter(v => val in v)
|
||
|
if (!options.length) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return options
|
||
|
}
|
||
|
export const getCheckboxValue = (value) => {
|
||
|
return value && aggBooleanOptions[value]
|
||
|
}
|
||
3 years ago
|
|
||
|
export const isMultiLineTextType = (values, col = null) => {
|
||
|
return values.some(r =>
|
||
|
(getColVal(r, col) || '').toString().match(/[\r\n]/) ||
|
||
|
(getColVal(r, col) || '').toString().length > 255)
|
||
|
}
|
||
|
|
||
|
export const extractMultiOrSingleSelectProps = (colData) => {
|
||
|
const colProps = {}
|
||
3 years ago
|
if (colData.some(v => v && (v || '').toString().includes(','))) {
|
||
3 years ago
|
let flattenedVals = colData.flatMap(v => v ? v.toString().trim().split(/\s*,\s*/) : [])
|
||
|
const uniqueVals = flattenedVals = flattenedVals
|
||
|
.filter((v, i, arr) => i === arr.findIndex(v1 => v.toLowerCase() === v1.toLowerCase()))
|
||
|
if (flattenedVals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(flattenedVals.length / 2)) {
|
||
|
colProps.uidt = UITypes.MultiSelect
|
||
|
colProps.dtxp = `'${uniqueVals.join("','")}'`
|
||
|
}
|
||
|
} else {
|
||
3 years ago
|
const uniqueVals = colData.map(v => (v || '').toString().trim()).filter((v, i, arr) => i === arr.findIndex(v1 => v.toLowerCase() === v1.toLowerCase()))
|
||
3 years ago
|
if (colData.length > uniqueVals.length && uniqueVals.length <= Math.ceil(colData.length / 2)) {
|
||
|
colProps.uidt = UITypes.SingleSelect
|
||
|
colProps.dtxp = `'${uniqueVals.join("','")}'`
|
||
|
}
|
||
|
}
|
||
|
return colProps
|
||
|
}
|
||
|
|
||
|
export const isDecimalType = colData => colData.some((v) => {
|
||
|
return v && parseInt(+v) !== +v
|
||
|
})
|
||
|
|
||
|
export const isEmailType = colData => !colData.some((v) => {
|
||
|
return v && !isEmail(v)
|
||
|
})
|
||
|
export const isUrlType = colData => !colData.some((v) => {
|
||
|
return v && !isValidURL(v)
|
||
|
})
|