Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i
- [Form action buttons](#form-action-buttons)
- [Help texts](#help-texts)
- [Auto focus](#auto-focus)
- [Textarea rows option](#textarea-rows-option)
- [Placeholders](#placeholders)
- [Form attributes](#form-attributes)
- [Advanced customization](#advanced-customization)
Expand Down Expand Up @@ -641,6 +642,20 @@ const uiSchema = {
}
```

### Textarea `rows` option

You can set initial height of a textarea widget by specifying `rows` option.

```js
const schema = {type: "string"};
const uiSchema = {
"ui:widget": "textarea",
"ui:options": {
rows: 15
}
}
```

### Placeholders

Text fields can benefit from placeholders by using the `ui:placeholder` uiSchema directive:
Expand Down Expand Up @@ -1185,7 +1200,7 @@ render((

### Custom error messages

Validation error messages are provided by the JSON Schema validation by default. If you need to change these messages or make any other modifications to the errors from the JSON Schema validation, you can define a transform function that receives the list of JSON Schema errors and returns a new list.
Validation error messages are provided by the JSON Schema validation by default. If you need to change these messages or make any other modifications to the errors from the JSON Schema validation, you can define a transform function that receives the list of JSON Schema errors and returns a new list.

```js
function transformErrors(errors) {
Expand Down
5 changes: 4 additions & 1 deletion playground/samples/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ module.exports = {
},
string: {
textarea: {
"ui:widget": "textarea"
"ui:widget": "textarea",
"ui:options": {
rows: 5
}
},
color: {
"ui:widget": "color"
Expand Down
12 changes: 9 additions & 3 deletions src/components/widgets/TextareaWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {PropTypes} from "react";
function TextareaWidget({
schema,
id,
options,
placeholder,
value,
required,
Expand All @@ -15,7 +16,7 @@ function TextareaWidget({
}) {
const _onChange = ({target: {value}}) => {
return onChange(value === "" ? undefined : value);
};
};
return (
<textarea
id={id}
Expand All @@ -26,20 +27,25 @@ function TextareaWidget({
disabled={disabled}
readOnly={readonly}
autoFocus={autofocus}
rows={options.rows}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to ensure that options is always an object before accessing this property. We may want to define {} a default value for the options arg.

onBlur={onBlur && (event => onBlur(id, event.target.value))}
onChange={_onChange} />
onChange={_onChange}/>
);
}

TextareaWidget.defaultProps = {
autofocus: false
autofocus: false,
options: {}
};

if (process.env.NODE_ENV !== "production") {
TextareaWidget.propTypes = {
schema: PropTypes.object.isRequired,
id: PropTypes.string.isRequired,
placeholder: PropTypes.string,
options: PropTypes.shape({
rows: PropTypes.number
}),
value: PropTypes.string,
required: PropTypes.bool,
autofocus: PropTypes.bool,
Expand Down
15 changes: 14 additions & 1 deletion test/StringField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("StringField", () => {

expect(onBlur.calledWith(input.id, "yo")).to.be.true;
});

it("should handle an empty string change event", () => {
const {comp, node} = createFormComponent({
schema: {type: "string"},
Expand Down Expand Up @@ -243,6 +243,19 @@ describe("StringField", () => {

expect(comp.state.formData).eql(undefined);
});

it("should render a textarea field with rows", () => {
const {node} = createFormComponent({
schema: {type: "string"},
uiSchema: {
"ui:widget": "textarea",
"ui:options": {rows: 20}
},
formData: "x",
});

expect(node.querySelector("textarea").getAttribute("rows")).eql("20");
});
});

describe("DateTimeWidget", () => {
Expand Down