Ant Components

The Ant components list is dizzying. Sure, it contains the basics — modals, forms (inline and vertical), navigation menus, a grid system. But it also contains a ton of extras, such as a @mentioning system, a timeline, badges, a seriously nice table system, and other small fancy features, such as an involved address box (see the Habitual Residence field). Have a look — it has everything that a modern web application should, with a tasteful, minimalist aesthetic.

Design Principles

There’s a nice, concise section in the documentation on the guiding principles of Ant.Design. I found it a great read as it got me thinking a lot about UI/UX considerations, especially the “Provide an Invitation” section, where they discuss different ways of making interactions discoverable by a user. By the way, if anyone can recommend me a good book on UX, I would be grateful.

Grid System

The Ant layout system is comprised of a 24-aliquot (a great new word that I learned from the translated documentation — it means parts of a whole) grid and a separate Layout component than you can choose to use. The grid uses the familiar Row/Col system, but you can also specify a prop called flex which allows you to harness Flexbox properties to define a responsive UI. (See a previous blog post of mine for help grokking the Flex standard.)

Flexbox is now fully supported on just about every browser (with partial support on IE 11 as well as some older mobile browsers), so it should be fine to use. If your customer base is largely Internet Explorer users, which does happen in some industries or countries, you would be wise to abstain from using flex Rows or the Layout component, as Layout is built strictly on Flexbox.

Layout includes components for a Sider, Header, Content, and Footer. Again, these are strictly based on Flexbox, so there’s no choice here — but to be honest I’m not sure what these components give you on top of using the standard Row/Col grid system, aside from a couple extra props you can make use of and possibly some built-in design choices. All in all, it doesn’t seem to me to be hugely useful.

Grid Props

Col elements can be supplied with a span prop to define how many aliquots a column takes up and an offset prop to define an optional offset; Row can take a gutter prop to define space between columns in a row (in pixels, not aliquots).

Here’s a UI example from a side project of mine. It contains one row with two columns:

The code would look something like this:

Forms

Ant does not let you down as far as forms are concerned, with options for inline, horizontal, and vertical forms, amazing select boxes, and clear validation messages and icons. In fact, it goes a little overboard here. It allows you to wrap your entire form-rendering component in a higher-order component à la Form.create()(<Component />) to gain access to a built-in validator syntax and custom two-way-binding system (cue audible lip biting). You can then specify standard rules such as ‘required’, or supply custom validator methods. (What are Higher Order Components? Check out this excellent post by James K. Nelson.)

Do you need to use their HOC? Absolutely not, and I’m not sure you should. As I said above, going down that path could expose you to language risk should you encounter bugs and I don’t see why you would want to use a custom two-way binding data system anyway. But you could easily use the HOC and just not use the two-way data binding.

Au Naturel — Plain React Forms

So let’s go over how to use the Ant validation messages without using their higher-order component.

Ant gives us three props that we can supply to each Form.Item component to display validation messages or icons:

validateStatus — This determines the colour & icon scheme of the validation message (see photo above) — valid options are success, warning, error, and validating. help — The validation message to display. hasFeedback — This is one of them props that don’t require a value. Just include if you want to display the associated icon, and it defaults to true.

Prettiest validations that I’ve ever seen.

Here’s an example of a simple form element that displays a validation message:

Notice that I used the long-form Form.Item component name. You can make yourself a shortcut for this and any other Ant sub-components as follows:

const FormItem = Form.Item; // .. allows you to use:

<FormItem />

Form Validation using the Ant Higher-Order Component

Now what if we do want to make use of the Ant Form decorator? It’s fairly straightforward to implement. Create your React component class, and then pass it as an argument to Form.create() . The component can then be exported:

class SomeComponent extends React.Component {

render() {<place_form_here.. />}

} FancyFormComponent = Form.create()(SomeComponent); export { FancyFormComponent as default }; // imported as SomeComponent

Inside your form, decorate your Input fields using the getFieldDecorater method, which exposes a ton of extra props on your component. You can now manipulate form elements directly from the props (eek!).

This example in the documentation gives a thorough demonstration on using the complete higher-order component.

Interactive Components — Message (Alert)

Ant provides a number of other components that give web applications a high degree of interactivity. A great example is alerts — or messages, as they’re called in Ant. Adding an alert is as simple as calling message.success('Great! Item has been saved.') in your component. Message types include success , warning , or error . Just don’t forget to import message (lowercase) from ‘antd’.