Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improvement] Table : adds number as a cell type #4135

Merged
merged 2 commits into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions frontend/src/Editor/Components/Table/columns/index.jsx
Expand Up @@ -151,6 +151,70 @@ export default function generateColumnsData({
}
return <span style={cellStyles}>{cellValue}</span>;
}
case 'number': {
const textColor = resolveReferences(column.textColor, currentState, '', { cellValue, rowData });

const cellStyles = {
color: textColor ?? '',
};

if (column.isEditable) {
const validationData = validateWidget({
validationObject: {
minValue: {
value: column.minValue,
},
maxValue: {
value: column.maxValue,
},
},
widgetValue: cellValue,
currentState,
customResolveObjects: { cellValue },
});

const { isValid, validationError } = validationData;
console.log('validationData', column.minValue, column.maxValue, validationData);
const cellStyles = {
color: textColor ?? '',
};

return (
<div>
<input
type="number"
style={{ ...cellStyles, maxWidth: width, minWidth: width - 10 }}
onKeyDown={(e) => {
if (e.key === 'Enter') {
if (e.target.defaultValue !== e.target.value) {
handleCellValueChange(
cell.row.index,
column.key || column.name,
Number(e.target.value),
cell.row.original
);
}
}
}}
onBlur={(e) => {
if (e.target.defaultValue !== e.target.value) {
handleCellValueChange(
cell.row.index,
column.key || column.name,
Number(e.target.value),
cell.row.original
);
}
}}
className={`form-control-plaintext form-control-plaintext-sm ${!isValid ? 'is-invalid' : ''}`}
defaultValue={cellValue}
/>
<div className="invalid-feedback">{validationError}</div>
</div>
);
}
return <span style={cellStyles}>{cellValue}</span>;
}
case 'text': {
return (
<textarea
Expand Down
33 changes: 33 additions & 0 deletions frontend/src/Editor/Inspector/Components/Table.jsx
Expand Up @@ -170,6 +170,7 @@ class TableComponent extends React.Component {
options={[
{ name: 'Default', value: 'default' },
{ name: 'String', value: 'string' },
{ name: 'Number', value: 'number' },
{ name: 'Text', value: 'text' },
{ name: 'Badge', value: 'badge' },
{ name: 'Multiple badges', value: 'badges' },
Expand Down Expand Up @@ -314,6 +315,38 @@ class TableComponent extends React.Component {
</div>
)}

{column.columnType === 'number' && column.isEditable && (
<div>
<div className="hr-text">{this.props.t('widget.Table.validation', 'Validation')}</div>
<div className="field mb-2">
<label className="form-label">{this.props.t('widget.Table.minValue', 'Min value')}</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={column.minLength}
theme={this.props.darkMode ? 'monokai' : 'default'}
mode="javascript"
lineNumbers={false}
placeholder={''}
onChange={(value) => this.onColumnItemChange(index, 'minValue', value)}
componentName={this.getPopoverFieldSource(column.columnType, 'minValue')}
/>
</div>
<div className="field mb-2">
<label className="form-label">{this.props.t('widget.Table.maxValue', 'Max value')}</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={column.maxLength}
theme={this.props.darkMode ? 'monokai' : 'default'}
mode="javascript"
lineNumbers={false}
placeholder={''}
onChange={(value) => this.onColumnItemChange(index, 'maxValue', value)}
componentName={this.getPopoverFieldSource(column.columnType, 'maxValue')}
/>
</div>
</div>
)}

{column.columnType === 'toggle' && (
<div>
<div className="field mb-2">
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/_helpers/utils.js
Expand Up @@ -248,6 +248,8 @@ export function validateWidget({ validationObject, widgetValue, currentState, cu
const regex = validationObject?.regex?.value;
const minLength = validationObject?.minLength?.value;
const maxLength = validationObject?.maxLength?.value;
const minValue = validationObject?.minValue?.value;
const maxValue = validationObject?.maxValue?.value;
const customRule = validationObject?.customRule?.value;

const validationRegex = resolveWidgetFieldValue(regex, currentState, '', customResolveObjects);
Expand Down Expand Up @@ -278,6 +280,26 @@ export function validateWidget({ validationObject, widgetValue, currentState, cu
}
}

const resolvedMinValue = resolveWidgetFieldValue(minValue, currentState, undefined, customResolveObjects);
if (resolvedMinValue !== undefined) {
if (widgetValue < parseInt(resolvedMinValue)) {
return {
isValid: false,
validationError: `Minimum value is ${resolvedMinValue}`,
};
}
}

const resolvedMaxValue = resolveWidgetFieldValue(maxValue, currentState, undefined, customResolveObjects);
if (resolvedMaxValue !== undefined) {
if (widgetValue > parseInt(resolvedMaxValue)) {
return {
isValid: false,
validationError: `Maximum value is ${resolvedMaxValue}`,
};
}
}

const resolvedCustomRule = resolveWidgetFieldValue(customRule, currentState, false, customResolveObjects);
if (typeof resolvedCustomRule === 'string') {
return { isValid: false, validationError: resolvedCustomRule };
Expand Down