Skip to content

Commit

Permalink
feat: NumberField renders as text if readonly is true
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Apr 13, 2020
1 parent 57eb105 commit 83d5e7b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
44 changes: 36 additions & 8 deletions app/containers/Forms/EmissionGasFields.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import {FieldProps, IdSchema} from 'react-jsonschema-form';
import {FieldProps} from 'react-jsonschema-form';
import {Form, Col} from 'react-bootstrap';
import {JSONSchema6} from 'json-schema';
import NumberFormat from 'react-number-format';
import ErrorList from 'components/Forms/ErrorList';

const EmissionGasFields: React.FunctionComponent<FieldProps> = ({
Expand All @@ -19,7 +18,10 @@ const EmissionGasFields: React.FunctionComponent<FieldProps> = ({
uiSchema
}) => {
const {
properties: {annualEmission: annualEmissionSchema}
properties: {
annualEmission: annualEmissionSchema,
annualCO2e: annualCO2eSchema
}
} = schema as {properties: Record<string, JSONSchema6>};

const {
Expand Down Expand Up @@ -89,11 +91,37 @@ const EmissionGasFields: React.FunctionComponent<FieldProps> = ({
</ul>
</Col>
<Col md={3} style={{textAlign: 'center'}}>
<NumberFormat
thousandSeparator
displayType="text"
value={formData.annualCO2e}
/>
<FieldTemplate
hidden={false}
id="emissions.annualCO2e"
classNames="form-group field field-number"
label={null}
schema={annualEmissionSchema}
uiSchema={uiSchema}
formContext={formContext}
errors={
<ErrorList errors={errorSchema?.annualCO2e?.__errors as any} />
}
>
<registry.fields.NumberField
required
readonly
schema={annualCO2eSchema}
uiSchema={uiSchema}
formData={formData.annualCO2e}
autofocus={autofocus}
idSchema={idSchema.annualCO2e}
registry={registry}
errorSchema={errorSchema?.annualCO2e}
formContext={formContext}
disabled={disabled}
name="annualCO2e"
onBlur={null}
onChange={() => {
throw new Error('Annual CO2e should not be edited directly');
}}
/>
</FieldTemplate>
</Col>
</Form.Row>
<style jsx>{`
Expand Down
14 changes: 14 additions & 0 deletions app/containers/Forms/NumberField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ const NumberField: React.FunctionComponent<FieldProps> = ({
schema.minimum < 0 ||
schema.exclusiveMinimum < 0;

if (readonly) {
return (
<NumberFormat
thousandSeparator={!uiSchema?.['ui:no-seperator']}
id={idSchema.$id}
disabled={disabled}
decimalScale={4}
value={formData}
displayType="text"
/>
);
}

return (
<NumberFormat
thousandSeparator={!uiSchema?.['ui:no-seperator']}
Expand All @@ -64,6 +77,7 @@ const NumberField: React.FunctionComponent<FieldProps> = ({
className="form-control"
allowNegative={allowNegative}
decimalScale={4}
defaultValue={(schema as any).defaultValue}
value={formData}
onValueChange={({floatValue}) => onChange(floatValue)}
/>
Expand Down
6 changes: 4 additions & 2 deletions schema/data/prod/form_json/emission.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"annualEmission": {
"title": "Tonnes (CO2)",
"type": "number",
"defaultValue": 0
"defaultValue": 0,
"minimum": 0
},
"gwp": {
"title": "GWP",
Expand All @@ -52,7 +53,8 @@
"gasDescription": {
"type": "string"
}
}
},
"required": ["gasType", "annualEmission", "gwp", "annualCO2e"]
}
}
},
Expand Down

0 comments on commit 83d5e7b

Please sign in to comment.