[ES|QL] Index editor cell validations#224728
[ES|QL] Index editor cell validations#224728sddonne merged 6 commits intoelastic:feature/lookup-join-uxfrom
Conversation
stratoula
left a comment
There was a problem hiding this comment.
Just a code review, I didn't test
| const validateValue = (val: string): string | undefined => { | ||
| if (!columnType) return undefined; | ||
|
|
||
| let validation; | ||
| switch (columnType) { | ||
| case 'number': | ||
| validation = isNaN(Number(val)) | ||
| ? i18n.translate('indexEditor.cellValueInput.validation.number', { | ||
| defaultMessage: 'Value must be a number', | ||
| }) | ||
| : undefined; | ||
| break; | ||
| case 'boolean': | ||
| validation = | ||
| val !== 'true' && val !== 'false' | ||
| ? i18n.translate('indexEditor.cellValueInput.validation.boolean', { | ||
| defaultMessage: 'Value must be true or false', | ||
| }) | ||
| : undefined; | ||
| break; | ||
| default: | ||
| validation = undefined; | ||
| } | ||
| if (validation) { | ||
| notifications.toasts.addDanger({ | ||
| title: validation, | ||
| }); | ||
| } | ||
| return validation; | ||
| }; |
There was a problem hiding this comment.
Do we need this to be inside the react component? Why don't we declare it outside as a helper function?
| const validateValue = (val: string): string | undefined => { | |
| if (!columnType) return undefined; | |
| let validation; | |
| switch (columnType) { | |
| case 'number': | |
| validation = isNaN(Number(val)) | |
| ? i18n.translate('indexEditor.cellValueInput.validation.number', { | |
| defaultMessage: 'Value must be a number', | |
| }) | |
| : undefined; | |
| break; | |
| case 'boolean': | |
| validation = | |
| val !== 'true' && val !== 'false' | |
| ? i18n.translate('indexEditor.cellValueInput.validation.boolean', { | |
| defaultMessage: 'Value must be true or false', | |
| }) | |
| : undefined; | |
| break; | |
| default: | |
| validation = undefined; | |
| } | |
| if (validation) { | |
| notifications.toasts.addDanger({ | |
| title: validation, | |
| }); | |
| } | |
| return validation; | |
| }; | |
| const validateValue = (val: string, columnType?: string): string | undefined => { | |
| if (!columnType) { | |
| return undefined; | |
| } | |
| let errorMessage: string | undefined; | |
| switch (columnType) { | |
| case 'number': | |
| if (isNaN(Number(val))) { | |
| errorMessage = i18n.translate('indexEditor.cellValueInput.validation.number', { | |
| defaultMessage: 'Value must be a number', | |
| }); | |
| } | |
| break; | |
| case 'boolean': | |
| if (!['true', 'false'].includes(val)) { | |
| errorMessage = i18n.translate('indexEditor.cellValueInput.validation.boolean', { | |
| defaultMessage: 'Value must be true or false', | |
| }); | |
| } | |
| break; | |
| default: | |
| errorMessage = undefined; | |
| } |
| services: { notifications }, | ||
| } = useKibana<KibanaContextExtra>(); | ||
| const [editValue, setEditValue] = useState(value); | ||
| const [error, setError] = useState<string | undefined>(undefined); |
There was a problem hiding this comment.
nit
| const [error, setError] = useState<string | undefined>(undefined); | |
| const [error, setError] = useState<string | undefined>(); |
| const [error, setError] = useState<string | undefined>(undefined); | ||
|
|
||
| const columnType = useMemo(() => { | ||
| if (!columns || !columnName) return undefined; |
There was a problem hiding this comment.
| if (!columns || !columnName) return undefined; | |
| if (!columns || !columnName) return; |
| import { i18n } from '@kbn/i18n'; | ||
| import { DatatableColumnType } from '@kbn/expressions-plugin/common'; | ||
|
|
||
| export const validateCellValue = ( |
There was a problem hiding this comment.
I wonder if we should consider a different approach. What do you think about having a factory for the input component based on the data type? Each component could encapsulate its own validation logic, and potentially a different input UI in the future. For example, we could render EuiNumberInput for number, which provides a better UX out of the box
There was a problem hiding this comment.
This is not a bad idea ++
stratoula
left a comment
There was a problem hiding this comment.
I didnt test but it looks very nice now
💔 Build Failed
Failed CI StepsHistory
|
Summary
Related issue #207330
Adds simple type validations when editing/adding a new cell in the index editor.