build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this component.

The framework calls this method when this component is inserted into the tree in a given BuildContext and when the dependencies of this component change (e.g., an InheritedComponent referenced by this component changes). This method can potentially be called in every frame and should not have any side effects beyond building a component.

The framework replaces the subtree below this component with the component returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the component returned by this method can update the root of the existing subtree, as determined by calling Component.canUpdate.

Typically implementations return a newly created constellation of components that are configured with information from this component's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this component is being built. For example, the context provides the set of inherited components for this location in the tree. A given component might be built with multiple different BuildContext arguments over time if the component is moved around the tree or if the component is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a component's build method is to depend on anything else, use a StatefulComponent instead.

See also:

Implementation

@override
Widget build(BuildContext context) {
  final hasError = error != null;

  final Map<String, String> sizeStyles = switch (size) {
    ComponentSize.sm => {
        'height': '36px',
        'padding': '0 12px',
        'font-size': '14px',
      },
    ComponentSize.md => {
        'height': '40px',
        'padding': '8px 12px',
        'font-size': '16px',
      },
    ComponentSize.lg => {
        'height': '44px',
        'padding': '0 16px',
        'font-size': '16px',
      },
  };

  final Widget selectElement = jaspr.Component.element(
    tag: 'select',
    id: id,
    classes: 'arcane-select',
    attributes: {
      if (name != null) 'name': name!,
      if (disabled) 'disabled': 'true',
      if (required) 'required': 'true',
    },
    styles: dom.Styles(raw: {
      ...sizeStyles,
      'padding-right': '36px',
      'font-family': 'inherit',
      'background-color': 'var(--background)',
      'border': hasError ? '1px solid var(--destructive)' : '1px solid var(--input)',
      'border-radius': '0.375rem',
      'color': 'var(--foreground)',
      'transition': 'color 150ms ease, border-color 150ms ease, box-shadow 150ms ease',
      'cursor': 'pointer',
      'appearance': 'none',
      'background-image':
          'url("data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' width=\'12\' height=\'12\' viewBox=\'0 0 12 12\'%3E%3Cpath fill=\'%2371717A\' d=\'M2.5 4.5L6 8l3.5-3.5\'/%3E%3C/svg%3E")',
      'background-repeat': 'no-repeat',
      'background-position': 'right 12px center',
      if (fullWidth) 'width': '100%',
      if (disabled) 'opacity': '0.5',
      if (disabled) 'cursor': 'not-allowed',
    }),
    events: {
      if (_onChange != null)
        'change': (e) {
          final target = e.target as dynamic;
          _onChange(target.value as String);
        },
    },
    children: [
      if (placeholder != null)
        jaspr.Component.element(
          tag: 'option',
          attributes: {'value': '', 'disabled': 'true', 'selected': 'true'},
          children: [jaspr.Component.text(placeholder!)],
        ),
      for (final opt in options)
        jaspr.Component.element(
          tag: 'option',
          attributes: {
            'value': opt.value,
            if (opt.disabled) 'disabled': 'true',
            if (value == opt.value) 'selected': 'true',
          },
          children: [jaspr.Component.text(opt.label)],
        ),
    ],
  );

  if (label != null || error != null) {
    return dom.div(
      classes: 'arcane-select-wrapper',
      styles: dom.Styles(raw: {
        'display': 'flex',
        'flex-direction': 'column',
        'gap': '0.25rem',
        if (fullWidth) 'width': '100%',
      }),
      [
        if (label != null)
          jaspr.Component.element(
            tag: 'label',
            attributes: id != null ? {'for': id!} : null,
            styles: const dom.Styles(raw: {
              'font-size': '1rem',
              'font-weight': '500',
              'color': 'var(--foreground)',
            }),
            children: [
              jaspr.Component.text(label!),
              if (required)
                const dom.span(
                  styles: dom.Styles(raw: {
                    'color': 'var(--destructive)',
                    'margin-left': '0.25rem',
                  }),
                  [jaspr.Component.text('*')],
                ),
            ],
          ),
        selectElement,
        if (error != null)
          dom.span(
            classes: 'arcane-select-error',
            styles: const dom.Styles(raw: {
              'font-size': '0.875rem',
              'color': 'var(--destructive)',
            }),
            [jaspr.Component.text(error!)],
          ),
      ],
    );
  }

  return selectElement;
}